1. 基本使用与原理
1.1 OpenFeign使用
- 添加 OpenFeign 依赖
org.springframework.cloud:spring-cloud-starter-openfeign - 添加启动 OpenFeign 配置注解
@EnableFeignClients 定义接口并使用 @FeignClient 注解指定需要请求的服务
@FeignClient(name = "provider")public interface EchoService {@GetMapping("/echo")public String echo();}
注入接口并进行调用 ```java @Autowired private EchoService echoService;
@GetMapping(“/hello”) public String hello() { return echoService.echo(); }
<a name="d4uZ2"></a>
### 1.2 JAX-RS支持
1. OpenFeign 接口支持 JAX-RS 注解需要添加依赖 `io.github.openfeign:feign-jaxrs`、`javax.ws.rs:jsr311-api:1.1.1`
1. 修改 OpenFeign 接口使用 JAX-RS 注解
```java
@FeignClient(name = "provider", configuration = OpenFeignConfig.class)
public interface EchoService {
@GET
@Path("/echo")
public String echo();
}
配置 Contract 实现类
@Configuration public class OpenFeignConfig { @Bean public Contract feignContract() { return new JAXRSContract(); } }1.3 基本原理
OpenFeign 声明的接口底层都会把接口解析成方法元数据 MethodMetadata ,再通过动态代理生成接口的代理,并基于 MethodMetadata 进行 Rest 调用。
2. 注解参数说明
2.1 @EnableFeignClients
- value、basePackages、basePackageClasses、clients:扫描 @FeignClient 注解修饰的类
-
2.2 @FeignClient
configuration:配置服务配置类
- value、name:配置服务名
- qualifiers:设置 @FeignBean 注解修饰的 Bean 名称
- url:绝对路径,用于替换服务名(优先生效)
- decode404:表示状态码为 404 时是否进行解码,默认 false 不解码进行报错
- fallback:发生异常时配置的回滚类,需要实现对应接口
- fallbackFactory:发生异常调用,实现 FallbackFactory 接口中的回滚方法
- path:请求通用路径
- primary:表示当前 FeignClient 生成的 Bean 是否是 primary,默认 true
- contextId:FeignContext 中 Map 的 Key ,默认使用 name 属性作为 Key
3. 案例
3.1 设置Token请求头
实现 RequestInterceptor 拦截器接口
public class FeignRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); assert attributes != null; HttpServletRequest request = attributes.getRequest(); // 设置请求头 Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String value = request.getHeader(name); requestTemplate.header(name, value); } } // 设置请求体,这里主要是为了传递 access_token Enumeration<String> parameterNames = request.getParameterNames(); StringBuilder body = new StringBuilder(); if (parameterNames != null) { while (parameterNames.hasMoreElements()) { String name = parameterNames.nextElement(); String value = request.getParameter(name); // 将 Token 加入请求头 if ("access_token".equals(name)) { requestTemplate.header("authorization", "Bearer " + value); } // 其它参数加入请求体 else { body.append(name).append("=").append(value).append("&"); } } } // 设置请求体 if (body.length() > 0) { // 去掉最后一位 & 符号 body.deleteCharAt(body.length() - 1); requestTemplate.body(Request.Body.create(body.toString(), Charset.defaultCharset())); } } }配置类中添加配置
@Configuration public class OpenFeignConfig { @Bean public RequestInterceptor requestInterceptor() { return new FeignRequestInterceptor(); } }配置添加到 @FeignClient 中
@FeignClient(name = "provider", configuration = OpenFeignConfig.class)
