spring cloud feign 负载均衡
Feign 负载均衡
首先引入依赖,依赖根据版本号来决定
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在application上加入@EnableFeignClients
接下来创建一个Client接口,并加入@FeignClient("user"),@Component 两个注解,这里调用的是user服务的接口,这里的接口路径均对用user服务中的接口路径。
接下來修改application文件配置
@ComponentScan("com.frt")
指定扫描路径从中找出标识了需要装配的类自动装配到spring的bean容器中。
@EnableFeignClients(basePackages = "com.frt.ubnuser.client")
指定feginclient包路径
接下来进入controller层注入Userclient并引入依赖
yml可选配置参数
feign:
hystrix:
enabled: true //feign开启hystrix支持
hystrix:
command:
HelloClient#toHello()://这个不配默认是全局
execution:
isolation:
thread:
timeoutInMilliseconds: 500 //线程超时,调用Fallback方法
circuitBreaker:
requestVolumeThreshold: 3 //10秒内出现3个以上请求(已临近阀值),并且出错率在50%以上,开启断路器.断开服务,调用Fallback方法