OpenFeign

1.OpenFeign知识

1.1 OpenFeign介绍

         OpenFeign 是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加容易。它是基于Feign库发展而来,Feign最初是由Netflix开发的,后来被贡献给了开源社区,并被Spring Cloud集成和扩展,形成了今天我们所使用的OpenFeign组件,他在早期是网飞公司提供的Feign因为维护去了所以Spring官网为其提供了OpenFeign;

1.2 基础概念

OpenFeign支持多种HTTP请求如:get,post,put,delete,path等多种方式...,

他也提供注解方式来减少开发人员的压力,只需要添加OpenFeign依赖就可以了:

 <!--openFeign-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>

还有就是在启动类上添加注解启动OpenFeign客户端:

@EnableFeignClients

在接口上添加OpenFeign提供的注解来定义创建客户端的接口

@FeignClient(value="接口名")

主要优势:

  1. 简化HTTP Web服务客户端的开发
  2. 支持多种HTTP客户端,集成Spring Cloud
  3. 声明式REST客户端,支持自动编码和解码
  4. 可插拔的注解支持,可定制性
  5. 内置负载均衡能力

2.OpenFeign使用

2.1 添加依赖

2.2 开启OpenFeign

2.3 编写OpenFeign客户端

@FeignClient("item-service")
public interface ItemClient {

    @GetMapping("/items")
    List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids);
}

2.4修改查询代码

导入ItemClient 并使用其中的方法就可以代替掉复杂的方法;

3.OpenFeign连接池

3.1 OpenFeign连接的方式

我使用的是OKHttp,但他有以下三种:

  1. HttpURLConnection : 默认实现,不支持连接池
  2. Apache HttpClient : 支持连接池
  3. OKHttp : 支持连接池 

3.2 添加依赖

<!--OK http 的依赖 -->
<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-okhttp</artifactId>
</dependency>

3.3 开启连接池功能

feign:
  okhttp:
    enabled: true # 开启OKHttp功能

添加完以上配置就欧克了,重新启动就可以看到使用到了连接池,这样性能等就会得到相对于的提高!

4.OpenFeign日志

4.1 定义日志级别

首先在配置包中创建配置类:DefaultFeignConfig代码如下:

public class DefaultFeignConfig {
    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }
}

4.2 配置

对于日志它有两种分别是全局和局部的两个方式:

4.2.1 全局

在 @EnableFeignClients 中配置,针对所有 FeignClient 生效。

@EnableFeignClients(defaultConfiguration = DefaultFeignConfig.class)

4.2.2 局部

在某个 FeignClient 中配置,只对当前 FeignClient 生效

@FeignClient(value = "item-service", configuration = DefaultFeignConfig.class)
最后运行结果如下:
17:35:32:148 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] ---> GET http://item-service/items?ids=100000006163 HTTP/1.1
17:35:32:148 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] ---> END HTTP (0-byte body)
17:35:32:278 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] <--- HTTP/1.1 200  (127ms)
17:35:32:279 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] connection: keep-alive
17:35:32:279 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] content-type: application/json
17:35:32:279 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] date: Fri, 26 May 2023 09:35:32 GMT
17:35:32:279 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] keep-alive: timeout=60
17:35:32:279 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] transfer-encoding: chunked
17:35:32:279 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] 
17:35:32:280 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] [{"id":100000006163,"name":"巴布豆(BOBDOG)柔薄悦动婴儿拉拉裤XXL码80片(15kg以上)","price":67100,"stock":10000,"image":"https://m.360buyimg.com/mobilecms/s720x720_jfs/t23998/350/2363990466/222391/a6e9581d/5b7cba5bN0c18fb4f.jpg!q70.jpg.webp","category":"拉拉裤","brand":"巴布豆","spec":"{}","sold":11,"commentCount":33343434,"isAD":false,"status":2}]
17:35:32:281 DEBUG 18620 --- [nio-8082-exec-1] com.hmall.api.client.ItemClient          : [ItemClient#queryItemByIds] <--- END HTTP (369-byte body)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值