OpenFeign学习
OpenFeign学习
概述
openFeign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可!
为什么需要OpenFeign?
实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用,因此,Feign在此基础做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置他,即可完成对服务提供方的绑定,简化了使用Ribbon自动封装服务调用客户端的开发量。
OpenFeign使用
依赖添加
<!--openFeign依赖添加-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--Eureka Client依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--内置loadbalancer,非ribbon-->
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yaml添加
server:
port: 80
spring:
application:
name: openFeign-service-order
eureka:
client:
#true表示向注册中心注册服务
register-with-eureka: true
fetch-registry: true #检索服务、不开启OpenFeign识别不了服务
service-url:
#设置Eureka Server交互的地址查询,服务查询和服务注册都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
添加OpenFeign接口
@Component //扫描
//标记为Feign接口,value为提供者的服务名称
@FeignClient(value="PAYMENT-SERVICE")
public interface OpenFeignService {
/**
* 调用提供者的信息
* @param id 主键标识
* @return
*/
@GetMapping("/payment/paymentById/{id}")
public AjaxResult paymentById(@PathVariable("id") String id);
}
控制层调用
@RestController
@RequestMapping("/order")
public class OpenFeignController {
@Autowired
private OpenFeignService openFeignService;
@RequestMapping("/list/{id}")
public AjaxResult list(@PathVariable String id){
return openFeignService.paymentById(id);
}
}
OpenFeign超时测试及配置
服务提供者超时方法
@RequestMapping("/timeout")
public AjaxResult timeout(){
try {
//停止6秒
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
return AjaxResult.success("返回成功");
}
服务消费者定义OpenFeign接口方法
@Component //扫描
//标记为Feign接口,value为提供者的服务名称
@FeignClient(value="PAYMENT-SERVICE")
public interface OpenFeignService {
/**
* 调用提供者的信息
* @param id 主键标识
* @return
*/
@GetMapping("/payment/paymentById/{id}")
public AjaxResult paymentById(@PathVariable("id") String id);
/**
* 测试超时
* @return
*/
@RequestMapping("/payment/timeout")
public AjaxResult timeout();
}
控制层调用
@RestController
@RequestMapping("/order")
public class OpenFeignController {
@Autowired
private OpenFeignService openFeignService;
@RequestMapping("/list/{id}")
public AjaxResult list(@PathVariable String id){
return openFeignService.paymentById(id);
}
@RequestMapping("/timeout")
public AjaxResult timeout(){
return openFeignService.timeout();
}
}
客户端报错:java.net.ConnectException: Connection refused: connect ==》超时
解决方案
配置文件添加以下内容
feign:
client:
config:
default:
connectTimeout: 2000 #指的是建立连接所用时间,适用于网络状况正常情况下,两端连接所用时间
readTimeout: 10000 #指的是建立连接后从服务器读取可用资源所用时间
OpenFeign日志功能
定义配置类
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL; //全面日志、更多选项直接看源码
}
}
配置文件开启日志
logging:
level:
#标记Feign注解的接口
com.lwp.service.OpenFeignService: debug
过程日志
正常日志
[OpenFeignService#paymentById] ---> GET http://PAYMENT-SERVICE/payment/paymentById/111 HTTP/1.1
[OpenFeignService#paymentById] ---> END HTTP (0-byte body)
[OpenFeignService#paymentById] <--- HTTP/1.1 200 (1015ms)
[OpenFeignService#paymentById] connection: keep-alive
[OpenFeignService#paymentById] content-type: application/json
[OpenFeignService#paymentById] date: Fri, 06 Aug 2021 16:38:32 GMT
[OpenFeignService#paymentById] keep-alive: timeout=60
[OpenFeignService#paymentById] transfer-encoding: chunked
[OpenFeignService#paymentById]
[OpenFeignService#paymentById] {"msg":"操作成功","code":200,"data":{"id":"111","serial":"1234245345"}}
[OpenFeignService#paymentById] <--- END HTTP (75-byte body)
超时日志
[OpenFeignService#timeout] ---> GET http://PAYMENT-SERVICE/payment/timeout HTTP/1.1
[OpenFeignService#timeout] ---> END HTTP (0-byte body)
[OpenFeignService#timeout] <--- ERROR SocketTimeoutException: Read timed out (5237ms)
[OpenFeignService#timeout] java.net.SocketTimeoutException: Read timed out
代码参考
openFeign-order-80

浙公网安备 33010602011771号