http客户端Feign

Feign的简介:

        Feign是一个声明式的http客户端。作用就是帮助我们发送http请求。

Feign的使用:

        1.引入依赖

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

        2.在启动类上添加@EnableFeignClientd,目的是开启关于Feign的注解

        3.定义和使用Feign客户端

 这里的内容要跟被调用服务的Contorller中的一致。

Feign的日志级别的配置

1全局

feign:
  client:
    config: 
      default: #表示为全局配置
        loggerLevel: FULL

2局部

efeign:
  client:
    config:
      serverName: #表示为给指定的服务器配置日志级别
        loggerLevl: FULL

Feign的性能优化

原因:Feign的底层实现默认不支持连接池的

日志级别最好用Basic或none

        1.性能优化配置连接池

        引入依赖

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>

        配置连接池

feign:
  client:
    config:
      default: #表示为全局配置
        loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息 
  httpclient:
    enabled: true
    max-connections: 200 # 最大的连接数
    max-connections-per-route: 50 # 每个路径的最大连接数

Feign的实践方式

        继承方式:给消费者的FeignClient和提供者的Controller定义统一的父接口作为标准。

        抽取方式:将FeignClient抽取为独立的模块,并把接口有关的POJO、默认的Feign配置都放到这个模块中,提供给所有消费者使用。(主要)

注:抽取后,需要在启动类上添加@EnableFeignClients(basePackage=“包的位置”)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Feign客户端是一个轻量级的HTTP客户端,它使用注解方式定义API接口,并且能够与Spring Cloud等微服务框架无缝集成。下面是实现Feign客户端的步骤: 1. 添加依赖 在Maven或Gradle中添加Feign客户端的依赖。 Maven: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` Gradle: ```groovy implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' ``` 2. 定义API接口 使用Feign注解定义调用的API接口,例如: ```java @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/example") String getExample(); @PostMapping("/example") void postExample(@RequestBody Example example); } ``` 3. 注入Feign客户端 在需要调用API的代码中注入Feign客户端,例如: ```java @Service public class ExampleService { private final ExampleServiceClient exampleServiceClient; public ExampleService(ExampleServiceClient exampleServiceClient) { this.exampleServiceClient = exampleServiceClient; } public String getExample() { return exampleServiceClient.getExample(); } public void postExample(Example example) { exampleServiceClient.postExample(example); } } ``` 4. 配置Feign客户端 可以在配置文件中配置Feign客户端的一些属性,例如: ```yaml example-service: url: http://example-service connectTimeout: 5000 readTimeout: 5000 ``` 以上就是实现Feign客户端的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值