OpenFeign
SpringCloud OpenFiegn官网→
官网
1 OpenFeign是什么
Feign是一个声明式 Web 服务客户端。它使编写 Web 服务客户端更容易。要使用 Feign,请创建一个接口并对其进行注释。它具有可插入的注释支持,包括 Feign 注释和 JAX-RS 注释。Feign 还支持可插拔的编码器和解码器。Spring Cloud 添加了对 Spring MVC 注释的支持,并支持使用HttpMessageConvertersSpring Web 中默认使用的注释。Spring Cloud 集成了 Eureka、Spring Cloud CircuitBreaker 以及 Spring Cloud LoadBalancer,在使用 Feign 时提供负载均衡的 http 客户端。
2 使用步骤
2.1 新建cloud-consumer-feign-order
2.2 修改pom.xml文件
<!--OpenFeign组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.3 写application.yml文件
server:
port: 80
#spring cloud 2021必须配datasource
spring:
application:
name: cloud-feign-consumer
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
#mysql5.x的没有cj
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root123
eureka:
client:
#表示是否将自己注册进Eurekaserver默认为true。
register-with-eureka: false
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
# defaultZone: http://localhost:7001/eureka #Eureka服务端的地址(单机版)
defaultZone: http://eureka-7010.com:7010/eureka, http://eureka-7011.com:7011/eureka/ #Eureka服务端的地址(集群版)
2.4 主启动类
主启动类添加 @EnableFeignClients 注解
@SpringBootApplication
@EnableFeignClients(basePackages = "com.tl.springcloud.service")
public class ConsumerFeignMain {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignMain.class);
}
}
2.5 编写业务类
2.5.1 业务逻辑接口+@FeignClient配置调用provider服务
@FeignClient("CLOUD-PAYMENT-SERVICE")
public interface PaymentClient {
@GetMapping("/payment/get/{id}")
CommonResult getPaymentById(@PathVariable("id") Long id);
}
2.5.2 控制层Controller
@RestController
public class ConsumerFeignController {
@Autowired
PaymentClient paymentClient;
@GetMapping("/consumer/feign/get/{id}")
public CommonResult getPaymentByFeign(@PathVariable("id") Long id){
CommonResult payment = paymentClient.getPaymentById(id);
return payment;
}
}
2.5 测试
2.6 总结
3 超时设置
open feign默认超时时间
public static final int DEFAULT_CONNECTION_TIMEOUT = 2000;
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
public static class OkHttp {
/**
* {@link OkHttpClient} read timeout; defaults to 60 seconds.
*/
private Duration readTimeout = Duration.ofSeconds(60);
public Duration getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
}
}
超时报错提示
There was an unexpected error (type=Internal Server Error, status=500).
Read timed out executing GET http://CLOUD-PAYMENT-SERVICE/payment/timeout
feign.RetryableException: Read timed out executing GET http://CLOUD-PAYMENT-SERVICE/payment/timeout
超时控制设置
feign:
client:
config:
default:
connectTimeout: 1000
readTimeout: 1000
loggerLevel: basic
4日志增强
4.1 日志增强分类
- NONE:没有日志(默认)。
- BASIC:只记录请求方法和URL、响应状态码和执行时间。
- HEADERS:记录基本信息以及请求和响应标头。
- FULL:记录请求和响应的标头、主体和元数据。
4.2 日志级别设置步骤
4.2.1 修改yml文件
logging:
level:
com.tl.springcloud.service.PaymentClient: debug
4.2.2 新建Java配置文件
@Configuration
public class FeignConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
4.2.3 测试控制台效果