我试验用的这个声明式服务调用由3部分组成,注册中心eureka,客户端client,声明式服务调用feignclient。
注册中心不多说,搭建好启动就可以。然后在client和feignclient中配置注册地址即可。
client就是正常的springcloud+springboot写的一套数据库增删改查,这些。
然后就是feignclient了:
还是直接上代码吧:
import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "management", fallback = ManageMentFeignClient.ManageMentHystrixFallback.class) public interface ManageMentFeignClient { @RequestMapping(value = "/api/management/findUserName",method = RequestMethod.POST) ResponseInfo findUserName(@RequestBody RequestInfo<UserLoginDTO> id); @RequestMapping(value = "/api/management/deleteUserLoginById",method = RequestMethod.POST) public ResponseInfo<Integer> deleteUserLoginById(@RequestBody RequestInfo<Integer> id) ; @RequestMapping(value = "/api/management/insertTest1",method = RequestMethod.POST) public ResponseInfo<Integer> insertTest1(@RequestBody RequestInfo<UserLoginDTO> dto) ; @RequestMapping(value = "/api/management/updateTest1",method = RequestMethod.POST) public ResponseInfo<Integer> updateTest1(@RequestBody RequestInfo<UserLoginDTO> dto) ; /** * 配置熔断器 */ @Component class ManageMentHystrixFallback implements ManageMentFeignClient {//这个类在上面那个类里面 private static final Logger LOGGER = LoggerFactory.getLogger(ManageMentHystrixFallback.class); private <T> ResponseInfo<T> defalutEntityErrorInfo(Class<T> usersinfoDTOClass) { return new ResponseInfo(false, "熔断器中断", createEmptyInstance(usersinfoDTOClass)); } private <T> T createEmptyInstance(Class<T> usersinfoDTOClass) { T t = null; try { t = usersinfoDTOClass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return t; } @Override public ResponseInfo findUserName(RequestInfo<UserLoginDTO> id) { ManageMentHystrixFallback.LOGGER.info("Management client findUserName 获取用户信息发生异常"); return defalutEntityErrorInfo(UserLoginDTO.class); } @Override public ResponseInfo<Integer> deleteUserLoginById(RequestInfo<Integer> id) { ManageMentHystrixFallback.LOGGER.info("Management client deleteUserLoginById 删除用户信息发生异常"); return defalutEntityErrorInfo(Integer.class); } @Override public ResponseInfo<Integer> insertTest1(RequestInfo<UserLoginDTO> dto) { ManageMentHystrixFallback.LOGGER.info("Management client insertTest1 添加用户信息发生异常"); return defalutEntityErrorInfo(Integer.class); } @Override public ResponseInfo<Integer> updateTest1(RequestInfo<UserLoginDTO> dto) { ManageMentHystrixFallback.LOGGER.info("Management client updateTest1 修改用户信息发生异常"); return defalutEntityErrorInfo(Integer.class); } } } |
对了,主类需要添加此注解:
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients
然后是配置文件中的东西:
#命令执行超时时间,默认1000ms hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 2000 #设置熔断时间 hystrix.command.default.execution.isolation.strategy: SEMAPHORE hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests: 200000 hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests: 200000 #执行是否启用超时,默认启用true hystrix.command.default.execution.timeout.enabled: true #开启熔断功能 hystrix.command.LocationCircuitBreaker.execution.isolation.semaphore.maxConcurrentRequests: 200000 hystrix.command.LocationCircuitBreaker.fallback.isolation.semaphore.maxConcurrentRequests: 200000 eureka.default.semaphore.maxSemaphores: 30000 hystrix.command.default.execution.isolation.thread.interruptOnTimeout: false |
上面这个配置是我的项目中使用的配置,下面这个配置仅供参考:
#hello-service.ribbon.ConnectTimeout=500 #hello-service.ribbon.ReadTimeout=1000 #hello-service.ribbon.OkToRetryOnAllOperations=true #hello-service.ribbon.MaxAutoRetriesNextServer=2 #hello-service.ribbon.MaxAutoRetries=1 # 开启Hystrix功能(不要和上面的配置一起使用) feign.hystrix.enabled=true # 设置熔断超时时间 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000 #开启熔断功能 hystrix.command.default.execution.timeout.enabled=true #全局配置 #hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000 #这算是针对方法级别的Hystrix配置会共用,也叫指定命令配置,与上面相比,差别在default变为了方法名hello #hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=5000 |
然后在service中注入,在controller中调用即可实现声明式服务调用。
@FeignClient注解中的name属性值即是client服务在eureka中的名称,fallback即是熔断器要执行的方法。然后下面接口类的内容也就类似于将client服务中的controller层中的方法拷贝过来。
@FeignClient(name = "management", fallback = ManageMentFeignClient.ManageMentHystrixFallback.class)
注意:@RequestMapping注解中的value属性值需要为client中接口的类上面的路径加上响应方法上面的路径。
@RequestMapping(value = "/api/management/updateTest1",method = RequestMethod.POST)
对了,断路器还有其他几种方式配置呢,这里只是其中一种。