Java中实现接口的调用
- Httpclient:Apache Jakarta Common 下的子项目
- Okhttp:由 Square 公司贡献
- HttpURLConnection:HttpURLConnection 是 Java 的标准类
- RestTemplate:RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户
Feign的架构
Spring Cloud Alibaba 整合 Feign
依赖导入
yml配置 ```yaml server: port: 8055<!-- nacos服务注册与发现 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- openfeign 远程调用 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
spring: application: name: mall-user-feign-demo
配置nacos注册中心
cloud: nacos: discovery: server-addr: 139.198.123.65:8848
启动feign
```java
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DruidDataSourceAutoConfigure.class
})
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
编写调用接口@FeignClient
@FeignClient(value = "mall-order", path = "/order")
public interface OrderFeignService {
@RequestMapping("/findOrderByUserId/{userId}")
public R findOrderByUserId(@PathVariable("userId") Integer userId);
}
发起调用
@RestController
public class UserController {
@Autowired
private OrderFeignService orderFeignService;
@GetMapping(value = "/findOrderByUserId/{id}")
public R findOrderByUserId(@PathVariable("id") Integer id) {
//feign调用
R result = orderFeignService.findOrderByUserId(id);
return result;
}
}
Spring Cloud Feign自定义配置
日志配置
日志级别:
- NONE:性能最好,适用于生产模式。不记录任何日志(默认值)。
- BASIC:适用于生产最终问题。仅记录url,响应状态,方法,时间。
- HEADERS:在BASIC的基础上,记录请求与响应的header。
- FULL:适用于开发与测试定位问题。记录请求与响应的header,body和元数据。
全局配置
@Configuration public class FeignConfig { @Bean public Logger.Level feignLoggerLevel(){ return Logger.Level.BASIC; } }yml指定服务配置
logging: level: com.tong.feign: debug feign: client: config: mall-order: loggerLevel: FULL通过拦截器实现认证
Feign 中我们可以直接配置 Basic 认证
使用的是base64加密,不安全@Configuration public class FeignConfig { @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){ return new BasicAuthRequestInterceptor("tong","123456"); } }自定义拦截器实现认证逻辑
自定义拦截器
public class FeignAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { //业务处理 String access_token = UUID.randomUUID().toString(); requestTemplate.header("Authorization", access_token); } }全局配置
@Configuration public class FeignConfig { @Bean public FeignAuthRequestInterceptor feignAuthRequestInterceptor(){ return new FeignAuthRequestInterceptor(); } }yml配置
feign: client: config: mall-order: #对的服务 requestInterceptors[0]: #配置拦截器 com.tong.interceptor.FeignAuthRequestInterceptor超时时间配置
全局配置
@Configuration public class FeignConfig { @Bean public Request.Options options() { Request.Options options = new Request.Options(5, TimeUnit.SECONDS, 1, TimeUnit.SECONDS, true); return options; } }yml指定服务配置
feign: client: config: mall-order: #对的服务 # 连接超时时间,默认2s connectTimeout: 5000 # 请求处理超时时间,默认5s readTimeout: 10000客户端组件配置
配置Apache HttpClient
引入依赖
yml配置<!-- Apache HttpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.7</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> <version>10.1.0</version> </dependency>feign: httpclient: enabled: true #可以不配置默认为true配置 OkHttp
导入依赖
yml配置<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> </dependency>feigh: okhttp: enabled: trueGZIP 压缩配置
配置GZIP可以减少网络开销,提升接口性能
yml配置
只有feing的http client不是okhttp3.OkHttpClient的时候才生效
feign:
compression:
request:
enabled: true
#配置压缩类型
mime-types: text/xml,application/xml,application/json
#最小压缩值
min-request-size: 2048
response:
enabled: true
编码器解码器配置
Feign 中提供了自定义的编码解码器设置,同时也提供了多种编码器的实现,比如 Gson、Jaxb、Jackson。我们可以用不同的编码解码器来处理数据的传输。如果你想传输 XML 格式的数据,可以自定义 XML 编码解码器来实现获取使用官方提供的 Jaxb。
springcloud中没有发现实现对应的编码解码器,使用默认的就行。
代码
gitee:https://gitee.com/tongtonghushen/spring-cloud-alibaba-tong.git commit_id:325654f942d2725fd4e64148602fb31aefa3feb7
