SpringCloud之Feign发送Http请求

http客户端Feign

Feign的介绍
Feign是一个声明式的http客户端,官方地址:https:/github.com/OpenFeign/feign
其作用就是帮助我们优雅的实现http请求的发送。

使用步骤

使用Feign的步骤如下:

  1. 引入依赖:

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
  2. 在order-server的启动类添加注解开启Feign的功能:

    @EnableFeignclients
    @MapperScan("cn.itcast.order.mapper")
    @SpringBootApplication
    public class OrderApplication{
         
    	public static void main(String[]args){
         
    		SpringApplication.run(OrderApplication.class,args);
    	}
    }
    
  3. 编写Feign客户端:

    //在消费者服务中编写,其中@Feignclient的值为提供者的服务名
    @Feignclient("user-server")
    public interface Userclient{
         
    	@GetMapping("/user/{id}")
    	User findById(@PathVariable("id")Long id);
    }
    

    主要是基于SpringMVC的注解来声明远程调用的信息,比如:

    • 服务名称:user-server
    • 请求方式:GET
    • 请求路径:/user/{id}
    • 请求参数:Long id
    • 返回值类型:User

自定义Feign的配置

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日志有两种方式:
方式一:配置文件方式
①全局生效:

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

②局部生效:

feign:
	client:
		config:
			user-server: #这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
				LoggerLevel: FULL #日志级别		

配置Feign日志的方式二:

java代码方式,需要先声明一个Bean:

public class FeignClientConfiguration{
   
	@Bean
	public Logger.Level feignLogLevel(){
   
		return Logger.Level.BASIC;
	}
}	

①而后如果是全局配置,则把它放到@EnableFeignClients这个注解中:

@EnableFeignClLients(defaultConfiguration = FeignclientConfiguration.class)

②如果是局部配置,则把它放到@FeignClient这个注解中:

@Feignclient(value="user-server",configuration=FeignclientConfiguration.class)

Feign的性能优化

Feign底层的客户端实现:

  • URLConnection:默认实现,不支持连接池
  • Apache HttpClient:支持连接池
  • OKHttp:支持连接池

因此优化Feign的性能主要包括:

  1. 使用连接池代替默认的URLConnection
  2. 日志级别,最好用basic或none
Feign的性能优化-连接池配置

Feign添加HttpClient的支持:
引入依赖:

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

配置连接池:

feign:
	client:
		config:
			default: #default全局的配置
				LoggerLevel: BASIC #日志级别,BASIC.就是基本的情求和响应信息
	httpclient:
		enabled: true #开启feign对HttpClient的支特
		max-connections: 200 #最大的连接数
		max-connections-per-route: 50 #每个路径的最大连接数

Feign的最佳实践

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

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

实现最佳实践方式二的步骤如下:

  1. 首先创建一个module,命名为feign-api,然后引入feignl的starter依赖
  2. 将order-service中编写的UserClient、User、DefaultFeignConfiguration都复制到feign-api项目中
  3. 在order-service中引入feign-api的依赖
  4. 修改order-service中的所有与上述三个组件有关的import部分,改成导入feign-api中的包
  5. 重启测试

当定义的FeignClient不在SpringBootApplication的扫描包范围时,这些FeignClient无法使用。有两种方式解决:
方式一:指定FeignClient所在包

@EnableFeignclients(basePackages = "cn.itcast.feign.clients")

方式二:指定FeignClient:字节码

@EnableFeignClients(clients = {
   Userclient.class})

相关推荐

  1. RestTemplate发送https请求

    2024-02-17 06:22:02       54 阅读
  2. Linux发送HTTP请求

    2024-02-17 06:22:02       33 阅读
  3. linux发送http请求命令

    2024-02-17 06:22:02       57 阅读
  4. QT-发送HTTP请求/QNetworkAccessManager

    2024-02-17 06:22:02       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-17 06:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-17 06:22:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-17 06:22:02       82 阅读
  4. Python语言-面向对象

    2024-02-17 06:22:02       91 阅读

热门阅读

  1. 设备驱动开发_2

    2024-02-17 06:22:02       46 阅读
  2. SpringBoot后端Long数据传到前端js精度损失问题

    2024-02-17 06:22:02       57 阅读
  3. 力扣:122. 买卖股票的最佳时机 II

    2024-02-17 06:22:02       51 阅读
  4. 【前端工程化面试题】说一下 webpack 的构建流程

    2024-02-17 06:22:02       66 阅读
  5. 使用 C++23 从零实现 RISC-V 模拟器(6):权限支持

    2024-02-17 06:22:02       44 阅读
  6. python自动定时任务schedule库的使用方法

    2024-02-17 06:22:02       54 阅读