我们以前利用 RestTemplate 发起远程调用的代码:
- 代码可读性差,编程体验不统一
- 参数复杂URL难以维护
Feign 是一个声明式的 http 客户端,官方地址:https://github.com/OpenFeign/feign
其作用就是帮助我们优雅的实现 http 请求的发送(Feign内部集成了ribbon,实现了负载均衡),解决上面提到的问题。
1、Feign使用
引入依赖
我们在 order-service 引入 feign 依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
添加注解
在 order-service (消费端)启动类添加注解开启 Feign
请求接口
在 order-service 中新建一个接口文件,内容如下
package cn.itcast.order.client;
import cn.itcast.order.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("userservice")
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
此接口文件就是指定访问userservice服务的请求接口
@FeignClient("userservice")
:其中参数填写的是微服务名
@GetMapping("/user/{id}")
:其中参数填写的是请求路径
这个客户端主要是基于 SpringMVC 的注解 @GetMapping
来声明远程调用的信息
Feign 可以帮助我们发送 http 请求,无需自己使用 RestTemplate 来发送了。
测试调用:
@Autowired
private UserClient userClient;
public Order queryOrderAndUserById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
// 使用feign远程调用
User user = userClient.findById(order.getUserId());
// 3. 将用户信息封装进订单
order.setUser(user);
// 4.返回
return order;
}
2、性能优化
Feign 底层发起 http 请求,依赖于其它的框架。其底层客户端实现有:
- URLConnection:默认实现,不支持连接池
- Apache HttpClient :支持连接池
- OKHttp:支持连接池
因此提高 Feign 性能的主要手段就是使用连接池代替默认的 URLConnection
另外,日志级别应该尽量用 basic/none,可以有效提高性能。
这里我们用 Apache 的HttpClient来演示连接池。
在 order-service 的 pom 文件中引入 HttpClient 依赖
<!--httpClient的依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
配置连接池
在 order-service 的 application.yml 中添加配置
feign:
client:
config:
default: # default全局的配置
loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息
httpclient:
enabled: true # 开启feign对HttpClient的支持
max-connections: 200 # 最大的连接数
max-connections-per-route: 50 # 每个路径的最大连接数
在 FeignClientFactoryBean 中的 loadBalance 方法中打断点
Debug 方式启动 order-service 服务,可以看到这里的 client,底层就是 HttpClient