Feign高级功能详解
注意:
本篇Feign的高级功能实现请参考上一篇博客.Feign的高级功能实现以上一篇博客代码为基础,我们主要修改service_feign_consumer
(服务消费者)客户端代码.
https://mp.csdn.net/postedit/95957488
一. Feign的高级应用Feign的高级应用大致有如下几条:
-
1️⃣. feign开启Gzip压缩;
-
2️⃣. feign开启日志功能;
-
3️⃣. feign替换JDK默认的URLConnection为okhttp;
-
4️⃣. feign超时设置;
-
5️⃣. feign使用hystrix进行熔断、降级处理.
Spring Cloud Feign支持对请求与响应的压缩,以提高通信效率,在服务消费者配置文件开启压缩支持和压缩文件的类型即可.
在service_feign_consumer的application.yml文件中添加配置.
feign:
#feign开启Gzip压缩,Feign支持对请求与响应的压缩,以提高通信效率,在服务消费者配置文件开启压缩支持和压缩文件的类型即可
#请求与响应的压缩
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
2. feign开启日志功能
feign开启日志可以分为2步,首先是在配置文件中编辑,然后使用编写Java配置类.
2.1 在application.yml中设置日志输出级别#设置日志输出级别
logging:
level:
com.syc.cloud.service.FeignHelloService: info
2.2 创建FeignLogConfig日志配置类
创建FeignLogConfig类,添加一个LoggerBean.
package com.syc.cloud.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* feign开启日志:
* feign开启日志有两种方式,一种是使用配置文件,一种是使用java代码.
* 下面将介绍代码配置方式:
* 创建FeignLogConfig类,添加一个LoggerBean.
*/
@Configuration
public class FeignLogConfig {
/**
* 日志level有4个级别:
* 1.NONE,不记录任何日志;
* 2.BASIC,仅记录请求方法,URL以及响应状态码和执行时间;
* 3.HEADRES,除了BASIC以外的还会记录请求和响应的头信息;
* 4.FULL,所有.
*/
@Bean
Logger.Level feignLogger(){
return Logger.Level.FULL;
}
}
3. feign替换JDK默认的URLConnection为okhttp
使用okhttp,能提高qps,因为okhttp有连接池和超时时间进行调优.
3.1 在服务消费者中,添加feign-okhttp依赖
io.github.openfeign
feign-okhttp
3.2 在配置文件中,禁用默认的http,启用okhttp.
feign:
#禁用默认的http,启用okhttp
httpclient:
enabled: false
okhttp:
enabled: true
3.3 创建OkHttpConfig类,添加okhttp的bean
package com.syc.cloud.config;
import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
/**
* 配置okhttp与连接池,ConnectionPool默认创建5个线程,保持5分钟长连接
*/
@Configuration
@ConditionalOnClass(Feign.class)
public class OkHttpConfig {
@Bean
public okhttp3.OkHttpClient okHttpClient() {
return new okhttp3.OkHttpClient.Builder()
//设置连接超时
.connectTimeout(10, TimeUnit.SECONDS)
//设置读超时
.readTimeout(10, TimeUnit.SECONDS)
//设置写超时
.writeTimeout(10, TimeUnit.SECONDS)
//是否自动重连
.retryOnConnectionFailure(true)
//保持5分钟长连接
.connectionPool(new ConnectionPool(10, 5L, TimeUnit.MINUTES))
.build();
}
}
4. feign超时设置
# hystrix的超时时间
# 超时时间设置为10秒,服务提供者睡眠时间大于10秒,服务消费者就会触发hystrix,进行熔断保护.
hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 10000
#ribbon的超时时间
ribbon:
ReadTimeout: 10000
ConnectTimeout: 10000
#Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开,需要在配置文件中配置打开它.
#feign启用hystrix,才能熔断、降级.
feign:
hystrix:
enabled: true
5. feign使用hystrix进行熔断、降级处理
上面feign的超时时间设置为10秒,当服务提供者睡眠时间大于10秒,服务消费者调用服务提供者的方法,就会触发hystrix,进行熔断保护.
5.1 改造服务提供者,让服务睡眠60秒package com.syc.cloud.web;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@RequestMapping("/hello")
public String sayHi(@RequestParam String name) {
try {
//睡眠60秒,测试feign的熔断、降级.
Thread.sleep(60 * 1000);
}catch (Exception e){
e.printStackTrace();
}
return "Hello," + name + "! This is " + port;
}
}
5.2 改造服务消费者,添加feign的熔断、降级方法
feign的hystrix熔断降级很容易实现,只要在FeignClient的fallback回滚方法中指定那个实现类即可.
package com.syc.cloud.service;
import com.syc.cloud.hystrix.FeignHelloServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "eureka-provider",fallback = FeignHelloServiceHystrix.class)
public interface FeignHelloService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String sayHello(@RequestParam(value = "name") String name);
}
5.3 FeignHelloServiceHystrix代码
package com.syc.cloud.hystrix;
import com.syc.cloud.service.FeignHelloService;
import org.springframework.stereotype.Component;
/**
* feign熔断器处理
*/
@Component
public class FeignHelloServiceHystrix implements FeignHelloService {
@Override
public String sayHello(String name) {
return "Hello "+name+". Please check your network!!!";
}
}

server:
port: 8765
spring:
application:
name: eureka-feign
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# hystrix的超时时间
# 超时时间设置为10秒,服务提供者睡眠时间大于10秒,服务消费者就会触发hystrix,进行熔断保护.
hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 10000
# ribbon的超时时间
ribbon:
ReadTimeout: 10000
ConnectTimeout: 10000
#Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开,需要在配置文件中配置打开它.
#feign启用hystrix,才能熔断、降级.
feign:
hystrix:
enabled: true
#feign开启Gzip压缩,Feign支持对请求与响应的压缩,以提高通信效率,在服务消费者配置文件开启压缩支持和压缩文件的类型即可
#请求与响应的压缩
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
#禁用默认的http,启用okhttp
httpclient:
enabled: false
okhttp:
enabled: true
#设置日志输出级别
logging:
level:
com.syc.cloud.service.FeignHelloService: info
7. 此时目录结构