SpringCloudAlibaba+Nacos整合OpenFeign
目录
- 1、如何使用
- 2、调用超时配置
- 3、日志打印配置
- 4、指定配置文件
- 5、整合Sentinel降级容错
- 6、异常处理器
Feign是Spring Cloud提供的一个声明式的伪Http客户端, 它使得调用远程服务就像调用本地服务一样简单, 只需要创建一个接口并添加一个注解即可。
Nacos很好的兼容了Feign, 在Nacos下使用Fegin默认就实现了负载均衡的效果。
1、如何使用
以下是feign的基本使用方式
1、pom依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
2、开启注解
@SpringBootApplication// 注册中心@EnableDiscoveryClient// 开启Fegin@EnableFeignClients public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication .class, args); }}
3、创建Feign接口
在wage
服务中,有一个定义的Controller
接口如下:
@RestControllerpublic class WageController { @GetMapping("list") public void listUser(){ System.out.println("工资列表"); }}
在User
服务中,配置Feign
如下:
// 声明调用的提供者的name@FeignClient("wage-service") public interface WageServiceFeign { //指定调用提供者的哪个方法 //@FeignClient+@GetMapping 就是一个完整的请求路径 http://wage-service/wage/list //此处定义方式和wage服务中controller接口定义方式一致 //注意:如果是传参的Get请求需要在参数前加入@RequestParam注解 @GetMapping(value = "/wage/list") void findWage();}
4、调用
在User服务中,调用Wage服务。
@Servicepulic class UserService{@Resourceprivate WageServiceFeign wageServiceFeign;public void test(){wageServiceFeign.findWage();}}
2、调用超时配置
如果我们觉得默认的feign
请求和响应时间,通过以下配置可以进行修改:
feign: client: config: default: # 建立链接的超时时长 connectTimeout: 5000 # 读取超时时长 readTimeout: 5000
在上面的配置中,通过default
表示对所有的feign请求设置统一的超时时长,如果想要对某个服务请求的单独设置不同的超时时长,采用以下方式:
feign: client: config: # 被调用服务的名称 wage-service: # 建立链接的超时时长 connectTimeout: 5000 # 读取超时时长 readTimeout: 5000
3、日志打印配置
在开发中如果想要看到Feign请求过程的日志记录,默认情况下Feign的日志是没有开启的,通过配置 application.yml
开启日志打印:
feign: client: config: default: # 设置日志级别 loggerLevel: basiclogging: level: # Feign接口所在的包 com.test.feign.wage: debug
在上面的配置中,通过default
表示对所有的feign请求设置统一的日志级别,如果想要对某个服务请求的单独设置不同的日志级别,采用以下方式:
feign: client: config: # 被调用服务的名称 wage-service: # 设置日志级别 loggerLevel: basiclogging: level: # Feign接口所在的包 com.test.feign.wage: debug
feign日志记录说明:
-
none:不记录任何日志(默认值)
-
basic:仅记录请求方法、URL、响应状态代码以及执行时间
-
headers:在
basic
级别的基础上,还记录了请求和响应的header -
full:记录请求和响应的header、body和元数据。
效果:
4、指定配置文件
我们可以自定义一个配置类,并且将Feign指向自定义的配置类,比如:我们可以在类中配置header传递,也可以进行其他操作。
配置类如下:
@Configurationpublic class FeignConfiguration implements RequestInterceptor { /** * 设置header传递,将当前服务的header信息传递到下游服务 * * @param template */ @Override public void apply(RequestTemplate template) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes != null) { HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); // 跳过 content-length if ("content-length".equals(name)) { continue; } template.header(name, values).header("test", "test"); } } } }}
修改@FeignClient
@FeignClient(value = "user-service",configuration = {FeignConfiguration.class})public interface TestFeign { ....}
5、整合Sentinel降级容错
pom
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
yaml
# feign启用sentinelfeign: sentinel:enabled: true
Fallback容错类
@Componentpublic class TestServiceFallbackFactory implements FallbackFactory<TestServiceFeign> { @Override public WageServiceFeign create(Throwable throwable) { // 可以将异常继续抛出,也可以实现自定义的内容 throw new RuntimeException(throwable.getMessage()); }}
修改@FeignClient
//加入fallbackFactory指向容错类@FeignClient(value = "user-service",fallbackFactory = TestServiceFallbackFactory .class),configuration = {FeignConfiguration.class})public interface TestFeign { ....}
6、异常处理器
我们可以实现异常拦截,在feign抛出异常后进行一些自定义处理
定义FeignErrorDecoder:
@Configurationpublic class FeignErrorDecoder extends ErrorDecoder.Default { private final Logger log = LoggerFactory.getLogger(FeignErrorDecoder.class); @Override public Exception decode(String methodKey, Response response) { Exception exception = super.decode(methodKey, response); // 如果是RetryableException,则返回继续重试 if (exception instanceof RetryableException) { return exception; } else { // 可以自定义一些逻辑,比如抛出一个其他的(统一个异常) exception = new RuntimeException("Feign调用出错了"); } return exception; }}
修改@FeignClient
在configuration
中加入FeignErrorDecoder
类
@FeignClient(value = "user-service",configuration = {FeignConfiguration.class, FeignErrorDecoder.class})public interface TestFeign { ....}
与Fallback容错类关系:
如果同时配置FeignErrorDecoder
与FallbackFactory
,那么异常会先进入到FeignErrorDecoder
然后再FallbackFactory
中。