spring中WebClient如何設置連接超時時間以及讀取超時時間
前言
在Spring WebFlux中,WebClient 提供了一種靈活的方式來配置連接超時時間和讀取超時時間。你可以使用 reactor.netty.http.client.HttpClient 來進行這些配置。以下是如何設置連接超時和讀取超時的示例代碼:
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import java.time.Duration;
public class WebClientConfig {
public WebClient createWebClient() {
HttpClient httpClient = HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 連接超時
.responseTimeout(Duration.ofMillis(10000)); // 讀取超時
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
}
詳細說明
導入所需包:
org.springframework.web.reactive.function.client.WebClientreactor.netty.http.client.HttpClientjava.time.Duration
創(chuàng)建HttpClient:
- 使用
HttpClient.create()創(chuàng)建一個新的HttpClient實例。 - 使用
option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)設置連接超時時間為5000毫秒(5秒)。 - 使用
responseTimeout(Duration.ofMillis(10000))設置讀取超時時間為10000毫秒(10秒)。
配置WebClient:
- 使用
WebClient.builder()創(chuàng)建一個新的WebClient.Builder實例。 - 使用
clientConnector(new ReactorClientHttpConnector(httpClient))將自定義的HttpClient設置為WebClient的客戶端連接器。 - 使用
build()方法構(gòu)建WebClient實例。
完整的示例類
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;
import io.netty.channel.ChannelOption;
import java.time.Duration;
public class WebClientConfig {
public WebClient createWebClient() {
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(10))
.addHandlerLast(new WriteTimeoutHandler(10)));
HttpClient httpClient = HttpClient.from(tcpClient);
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
}
解釋
- TcpClient:用于配置底層的TCP連接參數(shù),包括連接超時。
- ReadTimeoutHandler 和 WriteTimeoutHandler:用于設置讀取和寫入的超時時間(以秒為單位)。
通過上述配置,你可以成功地設置 WebClient 的連接超時時間和讀取超時時間。在生產(chǎn)環(huán)境中,可以根據(jù)需要調(diào)整這些超時時間,以適應不同的網(wǎng)絡條件和服務端性能。
HttpClient httpClient = HttpClient.create() .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 連接超時 .responseTimeout(Duration.ofMillis(10000)); // 讀取超時 這一段 HttpClient 沒有這個方法的啊,尤其是option
對于 Spring WebFlux 中的 WebClient 配置連接超時和讀取超時,最新版本的 reactor.netty.http.client.HttpClient 可能不再直接支持 option 方法。你可以通過 TcpClient 來設置這些超時選項。以下是使用 TcpClient 和 HttpClient 來設置連接超時和讀取超時的完整示例:
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import java.time.Duration;
public class WebClientConfig {
public WebClient createWebClient() {
// 配置連接超時和讀取超時
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 連接超時
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(10)) // 讀取超時
.addHandlerLast(new WriteTimeoutHandler(10))); // 寫入超時
// 使用自定義的 TcpClient 創(chuàng)建 HttpClient
HttpClient httpClient = HttpClient.from(tcpClient)
.responseTimeout(Duration.ofMillis(10000)); // 讀取超時
// 創(chuàng)建 WebClient 并設置自定義的 HttpClient
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
}
關(guān)鍵點解釋
TcpClient:
TcpClient.create()創(chuàng)建一個新的TcpClient實例。option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)設置連接超時時間為 5000 毫秒(5 秒)。doOnConnected配置在連接建立后添加ReadTimeoutHandler和WriteTimeoutHandler,分別設置讀取超時和寫入超時。
HttpClient:
HttpClient.from(tcpClient)使用配置好的TcpClient創(chuàng)建一個HttpClient實例。responseTimeout(Duration.ofMillis(10000))設置讀取超時時間為 10000 毫秒(10 秒)。
WebClient:
- 使用
WebClient.builder()創(chuàng)建一個新的WebClient.Builder實例。 - 使用
clientConnector(new ReactorClientHttpConnector(httpClient))將自定義的HttpClient設置為WebClient的客戶端連接器。 - 使用
build()方法構(gòu)建WebClient實例。
完整示例類
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import java.time.Duration;
public class WebClientConfig {
public WebClient createWebClient() {
// 配置連接超時和讀取超時
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 連接超時
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(10)) // 讀取超時
.addHandlerLast(new WriteTimeoutHandler(10))); // 寫入超時
// 使用自定義的 TcpClient 創(chuàng)建 HttpClient
HttpClient httpClient = HttpClient.from(tcpClient)
.responseTimeout(Duration.ofMillis(10000)); // 讀取超時
// 創(chuàng)建 WebClient 并設置自定義的 HttpClient
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
}
通過這種方式,你可以設置 WebClient 的連接超時時間和讀取超時時間。注意,這里的讀取超時是通過 ReadTimeoutHandler 配置的,它會在指定時間內(nèi)沒有數(shù)據(jù)讀取時觸發(fā)超時。
SPring 5.2.8 版本,使用WebClient 如何設置連接超時時間,和讀取超時時間
在Spring 5.2.8版本中,WebClient 的配置可以通過 TcpClient 和 HttpClient 來設置連接超時和讀取超時。以下是一個完整的示例,展示如何在Spring 5.2.8中配置 WebClient 的連接超時時間和讀取超時時間。
示例代碼
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.tcp.TcpClient;
import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import java.time.Duration;
public class WebClientConfig {
public WebClient createWebClient() {
// 配置 TcpClient
TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 連接超時
.doOnConnected(connection ->
connection.addHandlerLast(new ReadTimeoutHandler(10)) // 讀取超時
.addHandlerLast(new WriteTimeoutHandler(10))); // 寫入超時
// 配置 HttpClient
HttpClient httpClient = HttpClient.from(tcpClient)
.responseTimeout(Duration.ofMillis(10000)); // 讀取超時
// 配置 WebClient
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
}
詳細說明
導入所需的包:
org.springframework.web.reactive.function.client.WebClientreactor.netty.http.client.HttpClientreactor.netty.tcp.TcpClientio.netty.channel.ChannelOptionio.netty.handler.timeout.ReadTimeoutHandlerio.netty.handler.timeout.WriteTimeoutHandlerorg.springframework.http.client.reactive.ReactorClientHttpConnectorjava.time.Duration
配置 TcpClient:
TcpClient.create():創(chuàng)建一個新的TcpClient實例。option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000):設置連接超時時間為5000毫秒(5秒)。doOnConnected:配置在連接建立后添加ReadTimeoutHandler和WriteTimeoutHandler,分別設置讀取超時和寫入超時。
配置 HttpClient:
HttpClient.from(tcpClient):使用配置好的TcpClient創(chuàng)建一個HttpClient實例。responseTimeout(Duration.ofMillis(10000)):設置讀取超時時間為10000毫秒(10秒)。
配置 WebClient:
WebClient.builder():創(chuàng)建一個新的WebClient.Builder實例。clientConnector(new ReactorClientHttpConnector(httpClient)):將自定義的HttpClient設置為WebClient的客戶端連接器。build():構(gòu)建WebClient實例。
通過以上配置,你可以在Spring 5.2.8中成功地設置 WebClient 的連接超時時間和讀取超時時間。注意,這里的讀取超時是通過 ReadTimeoutHandler 配置的,它會在指定時間內(nèi)沒有數(shù)據(jù)讀取時觸發(fā)超時。
spring 操作Redis中的Zset ,我要獲取大于某個分值的所有數(shù)據(jù),如何操作
在Spring Data Redis中,你可以使用RedisTemplate或ReactiveRedisTemplate來操作Redis中的Zset,并獲取大于某個分值的所有數(shù)據(jù)。下面是如何使用這兩種方式來實現(xiàn)這一需求的示例。
使用RedisTemplate
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import java.util.Set;
@Service
public class RedisZSetService {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public Set<ZSetOperations.TypedTuple<String>> getZSetByMinScore(String key, double minScore) {
ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();
return zSetOps.rangeByScoreWithScores(key, minScore, Double.MAX_VALUE);
}
}
使用ReactiveRedisTemplate
如果你使用的是Spring WebFlux,并希望使用反應式編程模型,你可以使用ReactiveRedisTemplate。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
@Service
public class ReactiveRedisZSetService {
@Autowired
private ReactiveRedisTemplate<String, String> reactiveRedisTemplate;
public Flux<ZSetOperations.TypedTuple<String>> getZSetByMinScore(String key, double minScore) {
ZSetOperations<String, String> zSetOps = reactiveRedisTemplate.opsForZSet();
return zSetOps.rangeByScoreWithScores(key, minScore, Double.MAX_VALUE);
}
}
使用示例
假設你有一個RedisZSetService實例,以下是如何使用它來獲取大于某個分值的所有數(shù)據(jù):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import java.util.Set;
@RestController
public class RedisController {
@Autowired
private RedisZSetService redisZSetService;
@GetMapping("/getZSetByMinScore")
public Set<TypedTuple<String>> getZSetByMinScore(@RequestParam String key, @RequestParam double minScore) {
return redisZSetService.getZSetByMinScore(key, minScore);
}
}
詳細解釋
RedisTemplate 和 ZSetOperations:
RedisTemplate<String, String>是Spring提供的用于與Redis進行同步操作的模板類。ZSetOperations<String, String>是專門用于操作有序集合(ZSet)的接口。rangeByScoreWithScores(key, minScore, Double.MAX_VALUE)方法用于獲取有序集合中分值在minScore和Double.MAX_VALUE之間的所有元素及其分值。
ReactiveRedisTemplate 和 ZSetOperations:
ReactiveRedisTemplate<String, String>是Spring提供的用于與Redis進行異步操作的模板類。rangeByScoreWithScores(key, minScore, Double.MAX_VALUE)方法返回一個Flux<ZSetOperations.TypedTuple<String>>,用于異步流處理。
控制器層的使用:
RedisController類提供一個RESTful接口,調(diào)用RedisZSetService獲取大于某個分值的所有數(shù)據(jù)。
通過這些配置和代碼,你可以在Spring應用程序中方便地操作Redis中的有序集合,并獲取大于某個分值的所有數(shù)據(jù)。
return zSetOps.rangeByScoreWithScores(key, minScore, Double.MAX_VALUE); 使用Double.Max 是否影響性能呢,
在使用 Double.MAX_VALUE 作為 Redis ZSet 操作的最大值時,一般情況下性能不會受到顯著影響。Redis 是一個高性能的內(nèi)存數(shù)據(jù)庫,處理范圍查詢時會利用跳表(skip list)數(shù)據(jù)結(jié)構(gòu)的特性來快速定位和遍歷元素。因此,對于大多數(shù)應用場景,使用 Double.MAX_VALUE 作為上限并不會顯著影響性能。
然而,在極端情況下,如果有非常多的元素在這個范圍內(nèi),查詢的性能可能會受到影響。為了解決這種潛在的性能問題,可以采取以下措施:
分頁查詢:如果查詢的結(jié)果集可能非常大,可以使用分頁(通過
ZRangeByScore和LIMIT參數(shù))來逐步獲取結(jié)果,避免一次性返回大量數(shù)據(jù)。索引優(yōu)化:確保 Redis 服務器有足夠的內(nèi)存,并且沒有進行其他阻塞操作,可以提升查詢性能。
限制結(jié)果集:如果知道合理的最大值范圍,可以使用一個具體的最大值而不是
Double.MAX_VALUE。
具體示例:分頁查詢
以下是使用分頁查詢的示例代碼:
使用RedisTemplate進行分頁查詢
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import java.util.Set;
@Service
public class RedisZSetService {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public Set<ZSetOperations.TypedTuple<String>> getZSetByMinScoreWithPagination(String key, double minScore, long offset, long count) {
ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();
return zSetOps.rangeByScoreWithScores(key, minScore, Double.MAX_VALUE, offset, count);
}
}
使用ReactiveRedisTemplate進行分頁查詢
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
@Service
public class ReactiveRedisZSetService {
@Autowired
private ReactiveRedisTemplate<String, String> reactiveRedisTemplate;
public Flux<ZSetOperations.TypedTuple<String>> getZSetByMinScoreWithPagination(String key, double minScore, long offset, long count) {
ZSetOperations<String, String> zSetOps = reactiveRedisTemplate.opsForZSet();
return zSetOps.rangeByScoreWithScores(key, minScore, Double.MAX_VALUE, offset, count);
}
}
使用示例
假設你有一個 RedisZSetService 實例,以下是如何使用它來進行分頁查詢:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import java.util.Set;
@RestController
public class RedisController {
@Autowired
private RedisZSetService redisZSetService;
@GetMapping("/getZSetByMinScoreWithPagination")
public Set<TypedTuple<String>> getZSetByMinScoreWithPagination(
@RequestParam String key,
@RequestParam double minScore,
@RequestParam long offset,
@RequestParam long count) {
return redisZSetService.getZSetByMinScoreWithPagination(key, minScore, offset, count);
}
}
關(guān)鍵點解釋
分頁查詢:
rangeByScoreWithScores(key, minScore, Double.MAX_VALUE, offset, count)方法用于獲取有序集合中分值在minScore和Double.MAX_VALUE之間的所有元素及其分值,并且從offset開始獲取count個元素。
性能優(yōu)化:
- 分頁查詢避免一次性返回大量數(shù)據(jù),可以提升查詢性能,特別是在數(shù)據(jù)量很大的情況下。
通過以上方式,可以更有效地處理大數(shù)據(jù)量的查詢,提升應用的響應速度和性能。
總結(jié)
到此這篇關(guān)于spring中WebClient如何設置連接超時時間以及讀取超時時間的文章就介紹到這了,更多相關(guān)spring WebClient設置連接超時時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談java調(diào)用Restful API接口的方式
這篇文章主要介紹了淺談java調(diào)用Restful API接口的方式,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
Java CompletableFuture 異步超時實現(xiàn)深入研究
這篇文章主要為大家介紹了Java CompletableFuture 異步超時實現(xiàn)深入研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
SpringBoot實現(xiàn)分布式驗證碼登錄方案小結(jié)
驗證碼登錄作為一種有效的防護手段,可以防止惡意gongji、暴力pojie等,本文主要介紹了SpringBoot實現(xiàn)分布式驗證碼登錄方案小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-12-12
第一次使用Android Studio時你應該知道的一切配置(推薦)
這篇文章主要介紹了第一次使用Android Studio時你應該知道的一切配置(推薦) ,需要的朋友可以參考下2017-09-09

