一、OpenFeign
Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:@FeignClient注解。
Feign有可插拔的注解,包括@Feign注解和JAX-RS注解。Feign也支持编码器和解码器,Spring Cloud Open Feign对Feign进行增强支持Spring MVC注解,可以像Spring Web一样使用HttpMessageConverters等。
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
功能可插拔的注解支持,包括Feign注解和JAX-RS注解。支持可插拔的HTTP编码器和解码器(Gson,Jackson,Sax,JAXB,JAX-RS,SOAP)。
支持Hystrix和它的Fallback。支持Ribbon的负载均衡。支持HTTP请求和响应的压缩。
灵活的配置:基于 name 粒度进行配置支持多种客户端:JDK URLConnection、apache httpclient、okhttp,ribbon)支持日志支持错误重试url支持占位符可以不依赖注册中心独立运行。
1)name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
value 同name
2): contextId:当name相同时,存在冲突,此时可以通过contextId来处理。
比如:
@FeignClient(value = "${feign.cms.service.name}", name = "${feign.cms.service.name}",contextId = "CmsConfigClient")
public interface CmsConfigClient extends ICmsConfigController {
}
二、cloud-consumer-feign-order80
pom.xml
<!-- 引入eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- open feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false #是否将自己注册到注册中心,集群必须设置为true配合ribbon
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
#设置feign客户端超时时间,5秒钟
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
# feign日志以什么级别监控哪个接口
logging:
level:
com.springcloud.service.PaymentFeignService: debug
主启动类OrderFeignMain80
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
/*Feign是一个声明式WebService客户端,使用feign能让编写WebService客户端更加简单,
*使用方式-定义一个服务接口然后在上面添加注解
* Feign可以与Eureka和Ribbon结合使用以支持负载均衡
*/
SpringApplication.run(OrderFeignMain80.class,args);
}
}
OpenFeign日志增强
openfeign提供了日志打印功能。
Logger有四种类型:NONE(默认)、BASIC、HEADERS、FULL,通过注册Bean来设置日志记录级别
package com.atguigu.springcloud.config;
import feign.Logger;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
OrderFeignController
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderFeignController {
@Autowired
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return paymentFeignService.getPaymentById(id);
}
超时控制
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeOut(){
//客户端默认等待1秒钟
return paymentFeignService.paymentFeignTimeOut();
}
}
PaymentFeignService
package com.atguigu.springcloud.service;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") //8001微服务名称
//使用方式:微服务调用接口+注解 即->提供方8001微服务调用接口PaymentService+@FeignClient
public interface PaymentFeignService {
//这是8001微服务接口地址
@GetMapping("/payment/get/{id}")
CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeOut();
}
三、测试
依次启动7001、7002、8001、8002、OrderFeignMain80。
启动后访问http://eureka7001.com:7001/得到注册的服务状况
3.1 测试负载均衡
访问http://localhost:80/consumer/payment/get/1会轮询落在8001和8002服务上
3.2 测试超时控制
访问8001,正常访问
第一次访问http://localhost:80/consumer/payment/feign/timeout,正常
过几秒钟再访问,已经无法访问
解决超时方案
yml配置中添加
# 设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
# 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
ReadTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
源码下载地址
参考文章
https://blog.youkuaiyun.com/weixin_45821811/article/details/117401512