关于feign:Feign的基础使用模板

8次阅读

共计 2987 个字符,预计需要花费 8 分钟才能阅读完成。

Feign 是一个申明式的 http 客户端,官网地址:https://github.com/OpenFeign/feign
其作用就是帮忙咱们优雅的实现 http 申请的发送

一 根本应用

1 在消费者中引入依赖

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

2 减少启动注解

在消费者启动类上增加 @EnableFeignClients 注解,提供者可不增加

3 编写 Feign 接口

@FeignClient("userservice") // 服务名称
public interface UserClient {@GetMapping("/hello/feignTest/{id}") // 地址必须与服务中的地址统一
     String feignTest(@PathVariable("id") String id);
}

// 提供者中代码

@RestController
@RequestMapping("/hello")
public class Hello {@GetMapping("/hello")
    private String hello() {return "userHello";}

    @GetMapping("feignTest/{id}")
    private String feignTest(@PathVariable("id") String id){return "feignTest, ID:"+id;}
}

4 注入调用

// 消费者中注入调用

@RestController
@RequestMapping("/hello")
public class Hello {

    @Autowired
    private UserClient userClient;

    @GetMapping("/hello")
    private String hello() {return "orderHello";}

    @GetMapping("/feign/{id}")
    private String feign(@PathVariable("id")String id ){return userClient.feignTest(id);
    }

}

二 自定义配置

Feign 能够反对很多的自定义配置,如下表所示:

类型 作用 阐明
feign.Logger.Level 批改日志级别 蕴含四种不同的级别:NONE、BASIC、HEADERS、FULL
feign.codec.Decoder 响应后果的解析器 http 近程调用的后果做解析,例如解析 json 字符串为 java 对象
feign.codec.Encoder 申请参数编码 将申请参数编码,便于通过 http 申请发送
feign. Contract 反对的注解格局 默认是 SpringMVC 的注解
feign. Retryer 失败重试机制 申请失败的重试机制,默认是没有,不过会应用 Ribbon 的重试

个别只会批改日志的配置

feign 日志级别:

  • none(默认) 无日志
  • basic 记录申请发送、完结工夫、耗时等根本信息
  • headers basic 的信息 + 申请头、响应头信息
  • full headers+ 申请体、响应体

自定义的形式分为代码和配置文件(都是在消费者模块中增加)

配置文件形式:

# 针对单个服务
feign:  
  client:
    config: 
      userservice: # 针对某个微服务的配置
        loggerLevel: FULL #  日志级别 
# 针对所有服务
feign:  
  client:
    config: 
      default: # 这里用 default 就是全局配置,如果是写服务名称,则是针对某个微服务的配置
        loggerLevel: FULL #  日志级别

代码形式:

public class DefaultFeignConfiguration  {
    @Bean
    public Logger.Level feignLogLevel(){return Logger.Level.BASIC; // 日志级别为 BASIC}
}

针对单个服务则在单个服务的 @FeignClient 中增加 configuration = xxx.class

@FeignClient(value = "userservice", configuration = DefaultFeignConfiguration .class)

针对所有服务则在启动类 @EnableFeignClients 中增加 defaultConfiguration = xxx.class

@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration .class)

三 Feign 应用优化

1 优化日志级别

优化日志级别为 none 或 basic

2 http 客户端优化

feign 自身只是近程调用的申明,底层还是须要 http 客户端进行近程调用

feign 的客户端有:

  • URLConnection:默认实现,不反对连接池
  • Apache HttpClient:反对连接池
  • OKHttp:反对连接池

只须要替换成 带连接池 的 HttpClient 或 OKHttp 则能够晋升性能

以 HttpClient 为例

1 在消费者中引入依赖

<!--httpClient 的依赖 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>

2 批改配置文件中 feign 相干的配置

feign:
  client:
    config:
      default: # default 全局的配置
        loggerLevel: BASIC # 日志级别,BASIC 就是根本的申请和响应信息
  httpclient:
    enabled: true # 开启 feign 对 HttpClient 的反对
    max-connections: 200 # 最大的连接数
    time-to-live: 900 # 连贯最大闲置工夫,单位为秒,缺省值是 900 秒(15 分钟)max-connections-per-route: 50 # 每个门路的最大连接数

四 Feign 我的项目中的应用

我的项目中应用办法个别分为 继承 或者 独自抽离成一个模块

1 继承

让 Feign 接口和服务提供者继承或实现同一个接口,达到两边的统一性(紧耦合, 耦合度高)

2 独自抽离成一个模块

把 feign 相干的代码 (feign 接口、配置文件、依赖、相干实体类) 抽离成一个独自的模块,要应用其中的 feign 接口间接引入该模块

引入 feign 模块的形式可能会导致消费者无奈注入对应的 feign 接口类
解决办法:

  • 指定要加载的 Feign 接口类:在启动类的 @EnableFeignClients 中增加 clients = {xxxx.class,xxx.class}
  • 指定 Feign 应该扫描的包:在启动类的 @EnableFeignClients 中增加 basePackages = “xxx.xxx.xxx.xxx”

// 样例
指定须要加载的 Client 接口:

@EnableFeignClients(clients = {UserClient.class})

指定 Feign 应该扫描的包:

@EnableFeignClients(basePackages = "com.ray.feignapi.clients")

正文完
 0