目录:
- 搭建Spring-Cloud-Feign-Client
- Feign 配置压缩
- Feign 日志配置
Spring Cloud Feign整合了Spring Cloud Ribbon 和Spring Cloud Hystrix,而且还提供了一种比Ribbon更简单的服务调用方式 ——— 声明式服务调用。在Spring Cloud Feign中编写服务调用代码非常简单。
搭建Spring-Cloud-Feign-Client
基于Spring Cloud Ribbon(负载均衡)博文的代码,搭建Spring-Cloud-Feign-Client
1.依赖导入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
- 启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignClientApplication {
public static void main(String[] args){
SpringApplication.run(FeignClientApplication.class,args);
}
}
- 项目配置: application.yml
server:
port: 8771
spring:
application:
name: spring-cloud-feign-client
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
4 定义Feign请求接口
这里我们配合前面Eureka的例子,做一次远程调用
@FeignClient(value = "spring-cloud-ribbon-server")
public interface EurekaFeignService {
@GetMapping("/users/hello")
String hello();
}
@FeignClient(value = "spring-cloud-ribbon-server")里面配置的value 就是服务的名称
注意:
- 如果你在项目里面设置了统一的请求路径(server.servlet.context-path),需要将@FeignClient注解调整@FeignClient(value = "fw-register-eureka-client",path = "xxx")
- Feign 里面定义的接口,有多个@RequestParam,但只能有不超过一个@RequestBody
- 在定义接口的时候,如果返回的是用户自定义的实体,建议抽取出来,在Controller中实现接口,将抽取出来的接口单独打包,需要调用的项目依赖此包即可,每个项目不用重新定义一遍
5 定义控制层
@RestController
public class EurekaFeignController {
@Autowired
private EurekaFeignService eurekaFeignService;
@GetMapping("/hello")
public String hello() {
return eurekaFeignService.hello();
}
}
6 启动项目测试:
- 启动spring-cloud-eureka-server
- 启动端口号为8773,8774的spring-cloud-ribbon-server
- 启动spring-cloud-feign-client
访问:http://127.0.0.1:8771/hello
Feign 配置压缩
Spring Cloud feign支持对请求和响应进行gzip压缩,以减少通信过程中的性能损耗,只有超过这个大小的请求才会对其进行压缩:
添加配置信息:application.yml
feign:
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
Feign 配置日志
Feign提供了日志打印的功能,Feign的日志级别分为四种:
- NONE: 不记录任何信息。
- BASIC: 仅记录请求方法、URL以及响应状态码和执行时间。
- HEADERS: 除了记录BASIC级别的信息之外,还会记录请求和响应的头信息。
- FULL: 记录所有请求与响应的明细,包括头信息、请求体、元数据等。
日志级别默认为NONE,要改变级别可以在入口类中定义一个日志配置Bean:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignClientApplication {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
public static void main(String[] args){
SpringApplication.run(FeignClientApplication.class,args);
}
}
然后在yml中配置Feign客户端的日志级别为debug,Feign日志记录仅响应debug级别:
logging:
level:
top:
lconcise:
feign:
client:
service: debug
重启项目访问:http://127.0.0.1:8771/hello,可以看到控制台打印日志如下:
2020-02-14 11:51:56.863 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] <--- HTTP/1.1 200 (498ms)
2020-02-14 11:51:56.864 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] connection: keep-alive
2020-02-14 11:51:56.864 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] content-length: 31
2020-02-14 11:51:56.864 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] content-type: text/plain;charset=UTF-8
2020-02-14 11:51:56.864 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] date: Fri, 14 Feb 2020 03:51:56 GMT
2020-02-14 11:51:56.864 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] keep-alive: timeout=60
2020-02-14 11:51:56.864 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello]
2020-02-14 11:51:56.867 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] Hello Spring-Cloud-Feign-Client
2020-02-14 11:51:56.868 DEBUG 16764 --- [nio-8771-exec-1] t.l.f.client.service.EurekaFeignService : [EurekaFeignService#hello] <--- END HTTP (31-byte body)
源码:https://github.com/lbshold/springboot/tree/master/Spring-Boot-Ribbon-Demo
参考博文:https://mrbird.cc/Spring-Cloud-Feign.html
https://www.kancloud.cn/xuyisu/springcloud_springboot/1451300