【springCloudAlibaba-学习笔记】03-openFeign
springCloudAlibaba-学习笔记
Declarative REST Client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations
-
Feign:假装,伪装。
OpenFeign可以将提供者提供的Restful服务伪装为接口进行消费,消费者只需要使用feign接口 + 注解的方式即可直接调用提供者提供的Restful服务,而无需在使用RestTemplate -
JAX-RS:Java api extensions of RESTful web services。
OpenFeign总结:
- 只涉及consumer,和provider无关。用于消费端调用提供端
- 仅仅是一个伪客户端,不会对请求做任何的处理
- 是通过注解方式实现Restful请求的
OpenFeign 与 Ribbon
OpenFeign具有负载均衡功能,其可以对指定的微服务采用负载均衡方式进行消费、访问。老版本的OpenFeign默认采用Ribbon负载均衡器。但是由于Netflix不再维护Ribbon,所有Springcloud2021.x开始集成OpenFeign中已彻底丢弃Ribbon,而是采用SpringCloud自行研发的SpringCloud Loadbalancer作为负载均衡器。
- 添加依赖
xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
- 定义接口,controller调用
java
/**
* 描述: feign接口, 参见提供端controller
* @author Vic.xu
* @date 2023-10-20 16:43
*/
@FeignClient(value = "depart-provider", path = "/provider/depart")
public interface DepartService {
@PostMapping("/save")
boolean save(@RequestBody Depart depart);
@DeleteMapping("/delete/{id}")
boolean delete(@PathVariable("id") int id);
@PutMapping("/update")
boolean update(@RequestBody Depart depart);
@GetMapping("/get/{id}")
Depart get(@PathVariable("id") int id);
@GetMapping("/list")
List<Depart> list();
}
- 开启
@EnableFeignClients
properties
# default is global, can be changed to a real feignName
spring.cloud.openfeign.client.config.default.connectTimeout=5000
spring.cloud.openfeign.client.config.default.readTimeout=1
feign对请求和响应进行压缩
properties
spring.cloud.openfeign.compression.request.enabled=true
spring.cloud.openfeign.compression.request.mime-types=text/xml,application/xml,application/json
spring.cloud.openfeign.compression.request.min-request-size=2048
OpenFeign 底层实现技术选型
- 默认
httpclient:spring.cloud.openfeign.httpclient.enabled=true - 支持httpclient5:
spring.cloud.openfeign.httpclient.hc5.enabled=false - 也支持
okhttp:spring.cloud.openfeign.okhttp.enabled=false
feign 的远程调用底层实现技术默认采用的是jdk的URLConnection,同时还支持Httpclient和Okhttp。
由于jdk的URLConnection不支持连接池,通信效率很低,所以生产中是不会使用该默认实现的,所在在spring cloud OpenFeign中直接将默认实现该为HttpClient,同是也支持OkHttp.
负载均衡以及策略更换
-
ClientfeignClient: If Spring Cloud LoadBalancer is on the classpath,FeignBlockingLoadBalancerClientis used. If none of them is on the classpath, the default feign client is used. -
加上
LoadBalancer依赖后默认负载均衡(轮询的方式)
eg: 配置随机策略:
-
启动类上加:
@LoadBalancerClients(defaultConfiguration = DepartConfig.class) -
编写随机策略:
javapublic class DepartConfig { private static final Logger LOGGER = LoggerFactory.getLogger(DepartConfig.class); /** * 配置随机均衡策略 */ @Bean public ReactorLoadBalancer<ServiceInstance> randomLoadbalancer(Environment environment, LoadBalancerClientFactory factory){ //获取负载均衡客户端名称,即提供者服务名称 String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); ObjectProvider<ServiceInstanceListSupplier> lazyProvider = factory.getLazyProvider(name, ServiceInstanceListSupplier.class); LOGGER.info("获取负载均衡客户端名称,即提供者服务名称:{}", name); //从所有provider实例中指定名称的实例列表中随机选择一个实例 // 参数1:获取指定名称的所有provider实例列表 //参数2:指定要获取的provider实例名称 return new RandomLoadBalancer(lazyProvider, name); } } -
ReactorLoadBalancer相关实现类:随机、轮询、nacos
GitHub 登录
Gitee 登录
QQ 登录