重庆企业网站备案要多久时间,泸州做网站的公司有哪些,wordpress自动发布网站,wordpress指定分类文章目录地址#xff1a;
SpringCloudAlibaba整合-CSDN博客 这里只关注代码部分#xff0c;至于sentinel服务UI的实用#xff0c;后面可以补上 这里做一个改造#xff1a; 因为sentinel可以和openfeign结合使用#xff0c;为微服务做熔断降级#xff1b; 为了方便微服务之间…目录地址
SpringCloudAlibaba整合-CSDN博客 这里只关注代码部分至于sentinel服务UI的实用后面可以补上 这里做一个改造 因为sentinel可以和openfeign结合使用为微服务做熔断降级 为了方便微服务之间的调用把远程调用接口移动到api模块 所以把order中的openfeign和loadbalancer依赖放到api的pom中并且把order中Remote接口移动到api中后续order只需要引入api的依赖调用api中的接口即可 1.在api中引入sentinel和openfeign
!--openfeign--
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId
/dependency!--loadbalancer--
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId
/dependency!--sentinel--
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependency
2.把接口从order转移到api并修改
FeignClient(namemy-user, fallbackFactory UserServiceFactory.class)
public interface RemoteUserService {RequestMapping(/user/listAll)public ListUser listAll();GetMapping(/user/getById)public User getById(RequestParam(id) Integer id);
}
api模块添加UserServiceFactory类
Component
public class UserServiceFactory implements FallbackFactoryRemoteUserService {Overridepublic RemoteUserService create(Throwable cause) {return new RemoteUserService() {Overridepublic ListUser listAll() {return null;}Overridepublic User getById(Integer id) {System.out.println(user服务异常);return null;}};}
}
3.注意要把UserServiceFactory放到META-INF/spring.factories文件中
org.springframework.boot.autoconfigure.EnableAutoConfiguration\
com.test.api.remote.fallbackFactory.UserServiceFactory,\
com.test.api.remote.fallbackFactory.ProductServiceFactory
4.order引入api依赖并在启动类添加注解扫描feignClient接口
EnableFeignClients(basePackages {com.test.api.remote})
5.order的配置文件添加
feign:sentinel:enabled: true
6.重启服务访问接口正常访问
此时停止user服务再次访问order接口返回null后台日志打印“user服务异常”