# spring-cloud-openfeign-async
**Repository Path**: RemoteControl/spring-cloud-openfeign
## Basic Information
- **Project Name**: spring-cloud-openfeign-async
- **Description**: spring-cloud-openfeign-async 支持hc5异步通讯
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: 3.1.x
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 4
- **Forks**: 2
- **Created**: 2023-05-19
- **Last Updated**: 2024-03-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
////
DO NOT EDIT THIS FILE. IT WAS GENERATED.
Manual changes to this file will be lost when it is generated again.
Edit the files in the src/main/asciidoc/ directory instead.
////
image::https://github.com/spring-cloud/spring-cloud-openfeign/workflows/Build/badge.svg?branch=main&style=svg["Build",link="https://github.com/spring-cloud/spring-cloud-openfeign/actions"]
:doctype: book
:idprefix:
:idseparator: -
:toc: left
:toclevels: 4
:tabsize: 4
:numbered:
:sectanchors:
:sectnums:
:icons: font
:hide-uri-scheme:
:docinfo: shared,private
== 异步模块
=== 3.0/3.1版添加模块 spring-cloud-openfeign-core-asyn 以支持异步,3.0/3.1版本openfeign支持异步
=== 使用方法:
- pom.xml引入(务必在spring-cloud-starter-openfeign之前):
[source,xml]
----
org.springframework.cloud
spring-cloud-openfeign-core-asyn
3.0.8-SNAPSHOT
compile
io.github.openfeign
feign-hc5
----
- 注解:@EnableFeignClientsWithAsync() 启动openfeign
- 配置添加(3.1版本不要配置 httpclient.hc5、okhttp、httpclient,否则可能启动失败):
----
feign:
client:
#启动异步(默认是同步的)
asyn-enabled: true
httpclient:
hc5-async:
#启用hc5的真异步通讯(默认是基于线程的伪异步)
enabled: true
----
- 具体接口添加即可,如下,如果是同步的一样可以用,只是纯同步
----
@RequestMapping(value ="/system/sysParam/paramValueAndDelaySecond/{paramName}", method = RequestMethod.GET)
CompletableFuture> paramValueAndDelaySecondAsyn(@PathVariable("paramName") String paramName, @RequestParam("seconds") Integer seconds);
----
=== 已支持http2 性能应该能更上一层楼(异步长连接)
- 自建添加http2 client:
----
@Bean
public CloseableHttpAsyncClient httpAsyncClient5http2(FeignHttpClientProperties httpClientProperties) {
log.info("CloseableHttpAsyncClient http2 1.0!");
IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofMilliseconds(250)) // 1.1
.setSelectInterval(TimeValue.ofMilliseconds(50)) // 1.2
.setSoTimeout(Timeout.of(httpClientProperties.getHc5().getSocketTimeout(),
httpClientProperties.getHc5().getSocketTimeoutUnit()))
.build();
httpAsyncClient5 = HttpAsyncClients.customHttp2()
.disableCookieManagement()
.disableAutomaticRetries()
.useSystemProperties()
.setIOReactorConfig(ioReactorConfig)
.setDefaultRequestConfig(RequestConfig.custom()
.setRedirectsEnabled(httpClientProperties.isFollowRedirects())
.setConnectTimeout(100, TimeUnit.MILLISECONDS)
.build())
.build();
httpAsyncClient5.start();
return httpAsyncClient5;
}
----
- 后期通过属性支持支持http2
=== 以及准备把异步模块完全抽离成独立项目
=== 打包:mvn clean install -Dpmd.skip=true -Dcheckstyle.skip=true
=== 注意事项
- 异步导致的结果是发送也是异步(接口返回其实请求还未发送完)
- RequestConfig 中 connectionRequestTimeout,影响异步发送超时(目前设置在connectTimeout,即设置ConnectTimeout即可)
- 影响并发的吞吐量的maxConnPerRoute,maxConnTotal(http1),当设置为1的场景下,基本上就是一条条的发送接受(实际感受到的瞬发,但是结果是依次累加实际返回)
- http2吞吐量不受maxConnPerRoute,maxConnTotal影响,但是使劲发可能会存在上游拥堵
=== spring-cloud-openfeign 其他模块以、功能、描述见:
https://github.com/spring-cloud/spring-cloud-openfeign[Spring Cloud Openfeign]