1.远程调用方式
1.1 OkHttp
okhttp:OkHttp是一个高效的HTTP客户端,允许所有同一个主机地址的请求共享同一个socket连接;连接池减少请求延时;透明的GZIP压缩减少响应数据的大小;缓存响应内容,避免一些完全重复的请求
当网络出现问题的时候OkHttp依然坚守自己的职责,它会自动恢复一般的连接问题,如果你的服务有多个IP地址,当第一个IP请求失败时,OkHttp会交替尝试你配置的其他IP,OkHttp使用现代TLS技术(SNI, ALPN)初始化新的连接,当握手失败时会回退到TLS 1.0。
使用:它的请求/响应 API 使用构造器模式builders来设计,它支持阻塞式的同步请求和带回调的异步请求。
private String post(String url, String json) throws IOException {
OkHttpClient client = new OkHttpClient();
client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
1.2 restTemplate
我们来看用restTemplate远程调用实现
private void handleCartItems(List<CartV0> Vos) {
// 1.获取商品id
Set<Long> itemIds = vos.stream().map(CartVo::getItemId).collect(Collectors.toset());
//2.查询商品
//2.1.发现item-service服务的实例列表
List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
//2.2.负载均衡,挑选一个实例
ServiceInstance instance = instances.get(RandomUtil.randomInt(instances.size()));
// 2.3.发送请求,查询商品
ResponseEntity<List<ItemDTO>> response = restTemplate.exchange(
instance.getUri() + "/querySkuByIds?ids={ids}",// 请求路径
HttpMethod.GET,//请求方式
null,// 请求实体
new ParameterizedTypeReference<List<ItemDTO>>() {
},// 返回值类型
CollUtils.join(itemIds, ",")// 请求参数
);
//2.4.处理结果
List<SkuDTo> items = null;
if (response.getStatusCode().isSuccessful()) {
// 查询成功,获取响应结果
items = response.getBody();
}
...
}
这两种调用方式,与原本的本地方法调用差异太大。编程时如果同时有远程调用和本地调用,体验感有点差。
因此,我们必须想办法改变远程调用的开发模式,让远程调用像本地方法调用一样简单。而这就要用到OpenFeign组件了。
2.OpenFeign 快速入门
远程调用的关键点就在于四个:
- 请求方式
- 请求路径
- 请求参数
- 返回值类型
所以,OpenFeign 就利用SpringMVC的相关注解来声明上述4个参数,然后基于动态代理帮我们生成远程调用的代码,而无需我们手动再编写,非常方便。
2.1 引入依赖
在 order-service服务的pom.xml中引入OpenFeign的依赖和loadBalancer依赖:
<!--openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--负载均衡器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
2.2 启用OpenFeign
接下来,我们在order-service的 OrderServiceApplication 启动类上添加注解,启动OpenFeign功能:
@MapperScan("com.mall.cloud.order.mapper")
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
2.3 编写OpenFeign客户端
在order-service中,定义一个新的接口,编写Feign客户端,其中代码如下:
@FeignClient("product-service")
public interface SkuFeignClient {
@GetMapping("/querySkuByIds")
List<SkuDTO> querySkuByIds(@RequestParam("ids") Collection<Long> ids);
}
有了上述信息,OpenFeign就可以利用动态代理帮我们实现这个方法,并且向 http://product-service/querySkuByIds
发送一个GET请求,携带ids为请求参数,并自动将返回值处理为List<SkuDTO>
。
我们只需要直接调用这个方法,即可实现远程调用了。
2.4 使用FeignClient
最后,我们在 order-service的OrderServiceImpl中改造代码,直接调用ItemClient的方法:
public class OrderServiceImpl implements OrderService{
@Autowired
private SkuFeignClient skuFeignClient;
private void handleCartItems(List<CartV0> Vos) {
// 1.获取商品id
Set<Long> itemIds = vos.stream().map(CartVo::getItemId).collect(Collectors.toset());
//2.查询商品
List<SkuDTo> items = skuFeignClient.querySkuByIds(itemIds);
...
}
}
feign替我们完成了服务拉取、负载均衡、发送http请求的所有工作,是不是看起来优雅多了。
而且,这里我们不再需要RestTemplate了,还省去了RestTemplate的注册。
3.连接池
Feign底层发起http请求,依赖于其它的框架。其底层支持的http客户端实现包括:
- HttpURLConnection:默认实现,不支持连接池
- Apache HttpClient :支持连接池
- OKHttp:支持连接池
因此我们通常会使用带有连接池的客户端来代替默认的HttpURLConnection。比如,我们使用OK Http.
3.1 引入依赖
在order-service的pom.xml中引入依赖:
<!--OK http 的依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
3.2 开启连接池
在order-service的application.yml配置文件中开启Feign的连接池功能:
feign:
okhttp:
enabled: true # 开启OKHttp功能
重启服务,连接池就生效了。
3.3验证
我们可以打断点验证连接池是否生效,在 org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient
中的execute方法中打断点,
Debug方式启动 order-service,请求一次查询我的购物车方法,进入断点.
可以发现这里底层的实现已经改为OkHttpClient.
4.日志配置
OpenFeign只会在FeignClient所在包的日志级别为DEBUG时,才会输出日志。而且其日志级别有4级:
- NONE:不记录任何日志信息,这是默认值。
- BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
- HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
- FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。
Feign默认的日志级别就是NONE,所以默认我们看不到请求日志。
4.1 定义日志级别
定义DefaultFeignConfig.java
import feign.Logger;
import org.springframework.context.annotation.Bean;
public class DefaultFeignConfig {
@Bean
public Logger.Level feignLogLevel(){
return Logger.Level.FULL;
}
}
4.2 配置
接下来,要让日志级别生效,还需要配置这个类。有两种方式:
- 局部生效:在某个FeignClient中配置,只对当前FeignClient生效
@FeignClient(value = "item-service", configuration = DefaultFeignConfig.class)
- 全局生效:在@EnableFeignClients中配置,针对某个项目中引入的所有FeignClient生效。
@EnableFeignClients(defaultConfiguration = DefaultFeignConfig.class)
评论区