feign

作用

主要替代RestTemplate
image
Feign是一个声明式的客户端,作用是帮助我们优雅的实现http请求的发送,解决上面的问题

定义和使用Feign客户端

步骤如下:

  1. 引入依赖:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 在需要调用其他微服务的启动类中添加注解开启Feign功能:
    image
  2. 编写Feign客户端:
    image
  3. 使用Feign
    image

Feign自定义配置

image
操作:
image
image

Feign性能优化:

image
操作:

  1. 引入依赖
<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-httpclient</artifactId>
</dependency>
  1. 配置连接池
feign:
  client:
    config:
	  default:    #全局的配置
	    loggerLevel: BASIC # 日志级别,basic即基本的请求和响应信息
  httpclient:
    enable: true   # 开启feign对httpclient的支持
	max-connections: 200 # 最大连接数
	max-connections-per-route: 50 # 每个路径最大连接数

Feign实现最佳实践

image
这种方式不推荐
image
image
实现:
image

posted @ 2023-02-28 18:22  HS文  阅读(39)  评论(0)    收藏  举报