一、何为OpenFeign?
Feign是一个远程调用组件,集成了ribbon和hystrix。
Feign由Netflix提供,并于 2016年7月提供给spring cloud 社区,并更名为OpenFeign。
二、为何使用OpenFeign?
Feign把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。
并且,Feign集成了ribbon和hystrix,十分方便,其中ribbon是客户端负载均衡组件,Hystrix是容错组件用于解决数据库雪崩问题,解决手段主要有:线程隔离(线程池隔离、信号量隔离)、服务熔断。
三、OpenFeign使用入门
1、导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、启动类增加@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // 开启feign客户端,无需配置熔断器和负载均衡注解
public class SpringConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConsumerApplication.class, args);
}
}
3、Feign的客户端
@FeignClient(value = "spring-provider") // 标注该类是一个feign接口
public interface ProviderOpenfeign {
@GetMapping("/sms/provider/{id}")
String provider(@PathVariable("id") String id);
}
- 这是一个接口,Feign会通过动态代理,帮我们生成实现类。这点跟mybatis的mapper很像;
@FeignClient
,声明这是一个Feign客户端,类似@Mapper
注解。同时通过value
属性指定服务名称;- 接口中定义的方法,完全采用SpringMVC的注解,Feign会根据注解帮我们生成URL,并访问获取结果;
- 一个服务只能被一个类绑定,不能让多个类绑定同一个远程服务,否则,会在启动项目是出现『已绑定』异常。
改造原来的调用逻辑,调用ProviderOpenfeign接口:
@Controller
public class ConsumerController {
@Autowired
private ProviderOpenfeign providerOpenfeign;
@RequestMapping("/consumerOpenfeign/{id}")
public String consumerOpenfeign(@RequestParam("id") String id){
String consumer = providerOpenfeign.provider(id);
return "consumerOpenfeign " + consumer;
}
}
四、 Hystrix支持
OpenFeign中集成的Hystrix默认是关闭的;我们需要在配置文件中手动开启
feign:
hystrix:
enabled: true # 开启Feign的熔断功能
client:
config:
default:
connectTimeout: 4000 #设置feign超时连接时长
readTimeout: 4000 #设置feign请求的超时时长 4s之后 提供方没有响应 直接降级
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 6000
strategy: THREAD
timeout:
enabled: true
说明:
connectTimeout是feign连接某个服务的超时时间,
readTimeout是feign请求某个接口的超时时长,
timeoutInMilliseconds是hystrix熔断的时间,
Hystrix的超时时间是站在命令执行时间来看的和Feign设置的超时时间在设置上并没有关联关系。Hystrix不仅仅可以封装Http调用,还可以封装任意的代码执行片段。Hystrix是从命令对象的角度去定义,某个命令执行的超时时间,超过此此时间,命令将会直接熔断,假设hystrix 的默认超时时间设置了3000(3秒),而feign 设置的是4秒,那么Hystrix会在3秒到来时直接熔断返回,不会等到feign的4秒执行结束,如果hystrix的超时时间设置为6s,而feign 设置的是4秒,那么最终要以feign的时间4s为准,你可以理解为以时间短的为准,这两段都要配置,不能不配hystrix的配置,否则feign配置的readTimeout不生效。
五、Hystrix使用
Feign中的Fallback降级配置不像hystrix中那样了。
1)首先,我们要定义一个类ProviderOpenfeignFallback,实现刚才编写的ProviderOpenfeign,作为fallback的处理类
@Component
public class ProviderOpenfeignFallback implements ProviderOpenfeign {
@Override
public User provider(String id) {
return "服务器繁忙,请稍后再试!";
}
}
2)然后在ProviderOpenfeign中,指定刚才编写的实现类
@FeignClient(value = "spring-provider",fallback = ProviderOpenfeignFallback.class) // 标注该类是一个feign接口
public interface ProviderOpenfeign {
@GetMapping("/provider/{id}")
String provider(@PathVariable("id") String id);
}
3)修改消费方controller的consumerOpenfeign方法,注释掉@HystrixCommand注解和@DefaultProperties(defaultFallback="fallback")进行测试。
@RestController
//@DefaultProperties(defaultFallback = "fallbackMethod")
public class ConsumerController {
@Autowired
private ProviderOpenfeign providerOpenfeign;
@RequestMapping(value = "/consumerOpenfeign/{id}")
//@HystrixCommand
public String consumerOpenfeign(@PathVariable String id){
String consumer = providerOpenfeign.provider(id);
return "consumerOpenfeign consumer " + consumer;
}
}
4)重启测试: