Java實(shí)現(xiàn)一個簡單的長輪詢的示例代碼
分析一下長輪詢的實(shí)現(xiàn)方式
現(xiàn)在各大中間件都使用了長輪詢的數(shù)據(jù)交互方式,目前比較流行的例如Nacos的配置中心,RocketMQ Pull(拉模式)消息等,它們都是采用了長輪詢方的式實(shí)現(xiàn)。就例如Nacos的配置中心,如何做到服務(wù)端感知配置變化實(shí)時(shí)推送給客戶端的呢?
長輪詢與短輪詢
說到長輪詢,肯定存在和它相對立的,我們暫且叫它短輪詢吧,我們簡單介紹一下短輪詢:
短輪詢也是拉模式。是指不管服務(wù)端數(shù)據(jù)有無更新,客戶端每隔定長時(shí)間請求拉取一次數(shù)據(jù),可能有更新數(shù)據(jù)返回,也可能什么都沒有。如果配置中心使用這樣的方式,會存在以下問題:
由于配置數(shù)據(jù)并不會頻繁變更,若是一直發(fā)請求,勢必會對服務(wù)端造成很大壓力。還會造成推送數(shù)據(jù)的延遲,比如:每10s請求一次配置,如果在第11s時(shí)配置更新了,那么推送將會延遲9s,等待下一次請求;
無法在推送延遲和服務(wù)端壓力兩者之間中和。降低輪詢的間隔,延遲降低,壓力增加;增加輪詢的間隔,壓力降低,延遲增高。
長輪詢為了解決短輪詢存在的問題,客戶端發(fā)起長輪詢,如果服務(wù)端的數(shù)據(jù)沒有發(fā)生變更,會hold住請求,直到服務(wù)端的數(shù)據(jù)發(fā)生變化,或者等待一定時(shí)間超時(shí)才會返回。返回后,客戶端再發(fā)起下一次長輪詢請求監(jiān)聽。
這樣設(shè)計(jì)的好處:
- 相對于低延時(shí),客戶端發(fā)起長輪詢,服務(wù)端感知到數(shù)據(jù)發(fā)生變更后,能立刻返回響應(yīng)給客戶端。
- 服務(wù)端的壓力減小,客戶端發(fā)起長輪詢,如果數(shù)據(jù)沒有發(fā)生變更,服務(wù)端會hold住此次客戶端的請求,hold住請求的時(shí)間一般會設(shè)置到30s或者60s,并且服務(wù)端hold住請求不會消耗太多服務(wù)端的資源。
下面借用圖片來說明一下流程:

- 首先客戶端發(fā)起長輪詢請求,服務(wù)端收到客戶端的請求,這時(shí)會掛起客戶端的請求,如果在服務(wù)端設(shè)計(jì)的30s之內(nèi)都沒有發(fā)生變更,服務(wù)端會響應(yīng)回客戶端數(shù)據(jù)沒有變更,客戶端會繼續(xù)發(fā)送請求。
- 如果在30s之內(nèi)服務(wù)數(shù)據(jù)發(fā)生了變更,服務(wù)端會推送變更的數(shù)據(jù)到客戶端。
配置中心長輪詢設(shè)計(jì)

上面我們已經(jīng)介紹了整個思路,下面我們用代碼實(shí)現(xiàn)一下:
- 首先客戶端發(fā)送一個HTTP請求到服務(wù)端;服務(wù)端會開啟一個異步線程,如果一直沒有數(shù)據(jù)變更會掛起當(dāng)前請求(一個 Tomcat 也就 200 個線程,長輪詢也不應(yīng)該阻塞 Tomcat 的業(yè)務(wù)線程,所以需要配置中心在實(shí)現(xiàn)長輪詢時(shí)往往采用異步響應(yīng)的方式來實(shí)現(xiàn),而比較方便實(shí)現(xiàn)異步 HTTP 的常見手段便是 Servlet3.0 提供的 AsyncContext 機(jī)制。)
- 在服務(wù)端設(shè)置的超時(shí)時(shí)間內(nèi)仍然沒有數(shù)據(jù)變更,那就返回客戶端一個沒有變更的標(biāo)識。例如響應(yīng)304狀態(tài)碼;
- 在服務(wù)端設(shè)置的超時(shí)時(shí)間內(nèi)有數(shù)據(jù)變更了,就返回客戶端變更的內(nèi)容;
配置中心長輪詢實(shí)現(xiàn)
下面用代碼實(shí)現(xiàn)長輪詢:
客戶端實(shí)現(xiàn)
@Slf4j
public class ConfigClientWorker {
?
private final CloseableHttpClient httpClient;
?
private final ScheduledExecutorService executorService;
?
public ConfigClientWorker(String url, String dataId) {
this.executorService = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable);
thread.setName("client.worker.executor-%d");
thread.setDaemon(true);
return thread;
});
?
// ① httpClient 客戶端超時(shí)時(shí)間要大于長輪詢約定的超時(shí)時(shí)間
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(40000).build();
this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
?
executorService.execute(new LongPollingRunnable(url, dataId));
}
?
class LongPollingRunnable implements Runnable {
?
private final String url;
private final String dataId;
?
public LongPollingRunnable(String url, String dataId) {
this.url = url;
this.dataId = dataId;
}
?
@SneakyThrows
@Override
public void run() {
String endpoint = url + "?dataId=" + dataId;
log.info("endpoint: {}", endpoint);
HttpGet request = new HttpGet(endpoint);
CloseableHttpResponse response = httpClient.execute(request);
switch (response.getStatusLine().getStatusCode()) {
case 200: {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent()));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
response.close();
String configInfo = result.toString();
log.info("dataId: [{}] changed, receive configInfo: {}", dataId, configInfo);
break;
}
// ② 304 響應(yīng)碼標(biāo)記配置未變更
case 304: {
log.info("longPolling dataId: [{}] once finished, configInfo is unchanged, longPolling again", dataId);
break;
}
default: {
throw new RuntimeException("unExcepted HTTP status code");
}
}
executorService.execute(this);
}
}
?
public static void main(String[] args) throws IOException {
?
new ConfigClientWorker("http://127.0.0.1:8080/listener", "user");
System.in.read();
}
}- httpClient 客戶端超時(shí)時(shí)間要大于長輪詢約定的超時(shí)時(shí)間,不然還沒等到服務(wù)端返回,客戶端自己就超時(shí)了。
- 304 響應(yīng)碼標(biāo)記配置未變更;
- http://127.0.0.1:8080/listener 是服務(wù)端地址;
服務(wù)端實(shí)現(xiàn)
@RestController
@Slf4j
@SpringBootApplication
public class ConfigServer {
?
@Data
private static class AsyncTask {
// 長輪詢請求的上下文,包含請求和響應(yīng)體
private AsyncContext asyncContext;
// 超時(shí)標(biāo)記
private boolean timeout;
?
public AsyncTask(AsyncContext asyncContext, boolean timeout) {
this.asyncContext = asyncContext;
this.timeout = timeout;
}
}
?
// guava 提供的多值 Map,一個 key 可以對應(yīng)多個 value
private Multimap<String, AsyncTask> dataIdContext = Multimaps.synchronizedSetMultimap(HashMultimap.create());
?
private ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("longPolling-timeout-checker-%d")
.build();
private ScheduledExecutorService timeoutChecker = new ScheduledThreadPoolExecutor(1, threadFactory);
?
// 配置監(jiān)聽接入點(diǎn)
@RequestMapping("/listener")
public void addListener(HttpServletRequest request, HttpServletResponse response) {
?
String dataId = request.getParameter("dataId");
?
// 開啟異步!?。?
AsyncContext asyncContext = request.startAsync(request, response);
AsyncTask asyncTask = new AsyncTask(asyncContext, true);
?
// 維護(hù) dataId 和異步請求上下文的關(guān)聯(lián)
dataIdContext.put(dataId, asyncTask);
?
// 啟動定時(shí)器,30s 后寫入 304 響應(yīng)
timeoutChecker.schedule(() -> {
if (asyncTask.isTimeout()) {
dataIdContext.remove(dataId, asyncTask);
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
// 標(biāo)志此次異步線程完成結(jié)束?。?!
asyncContext.complete();
}
}, 30000, TimeUnit.MILLISECONDS);
}
?
// 配置發(fā)布接入點(diǎn)
@RequestMapping("/publishConfig")
@SneakyThrows
public String publishConfig(String dataId, String configInfo) {
log.info("publish configInfo dataId: [{}], configInfo: {}", dataId, configInfo);
Collection<AsyncTask> asyncTasks = dataIdContext.removeAll(dataId);
for (AsyncTask asyncTask : asyncTasks) {
asyncTask.setTimeout(false);
HttpServletResponse response = (HttpServletResponse)asyncTask.getAsyncContext().getResponse();
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(configInfo);
asyncTask.getAsyncContext().complete();
}
return "success";
}
?
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}- 客戶端請求過來,首先開啟一個異步線程
request.startAsync(request, response);保證不占用Tomcat線程。此時(shí)Tomcat線程以及釋放。配合asyncContext.complete()使用。 dataIdContext.put(dataId, asyncTask);會將 dataId 和異步請求上下文給關(guān)聯(lián)起來,方便配置發(fā)布時(shí),拿到對應(yīng)的上下文Multimap<String, AsyncTask> dataIdContext它是一個多值 Map,一個 key 可以對應(yīng)多個 value,你也可以理解為Map<String,List<AsyncTask>>timeoutChecker.schedule()啟動定時(shí)器,30s 后寫入 304 響應(yīng)@RequestMapping("/publishConfig"),配置發(fā)布的入口。配置變更后,根據(jù) dataId 一次拿出所有的長輪詢,為之寫入變更的響應(yīng)。asyncTask.getAsyncContext().complete();表示這次異步請求結(jié)束了。
啟動配置監(jiān)聽
先啟動 ConfigServer,再啟動 ConfigClient。30s之后控制臺打印第一次超時(shí)之后收到服務(wù)端304的狀態(tài)碼
16:41:14.824 [client.worker.executor-%d] INFO cn.haoxiaoyong.poll.ConfigClientWorker - longPolling dataId: [user] once finished, configInfo is unchanged, longPolling again
請求一下配置發(fā)布,請求localhost:8080/publishConfig?dataId=user&configInfo=helloworld
服務(wù)端打印日志:
2022-08-25 16:45:56.663 INFO 90650 --- [nio-8080-exec-2] cn.haoxiaoyong.poll.ConfigServer : publish configInfo dataId: [user], configInfo: helloworld
到此這篇關(guān)于Java實(shí)現(xiàn)一個簡單的長輪詢的示例代碼的文章就介紹到這了,更多相關(guān)Java長輪詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA修改內(nèi)存大小,使得idea運(yùn)行更流暢
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA修改內(nèi)存大小,使得idea運(yùn)行更流暢的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
利用Java實(shí)現(xiàn)圖片轉(zhuǎn)化為ASCII圖的示例代碼
本文將詳細(xì)講解如何利用 Java 實(shí)現(xiàn)圖片轉(zhuǎn)化為 ASCII 圖,從項(xiàng)目背景與意義、相關(guān)技術(shù)知識,到系統(tǒng)需求與架構(gòu)設(shè)計(jì),再到詳細(xì)實(shí)現(xiàn)思路、完整代碼和代碼解讀,最后對項(xiàng)目進(jìn)行總結(jié)與展望,需要的朋友可以參考下2025-03-03
Spring:@Async注解和AsyncResult與CompletableFuture使用問題
這篇文章主要介紹了Spring:@Async注解和AsyncResult與CompletableFuture使用問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
JPA如何設(shè)置表名和實(shí)體名,表字段與實(shí)體字段的對應(yīng)
這篇文章主要介紹了JPA如何設(shè)置表名和實(shí)體名,表字段與實(shí)體字段的對應(yīng),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java版C語言版簡單使用靜態(tài)語言實(shí)現(xiàn)動態(tài)數(shù)組的方法
本文給大家分享java版和C語言版簡單使用靜態(tài)語言實(shí)現(xiàn)動態(tài)數(shù)組的方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友參考下吧2017-10-10
java實(shí)現(xiàn)Yaml轉(zhuǎn)Json示例詳解
這篇文章主要為大家介紹了java實(shí)現(xiàn)Yaml轉(zhuǎn)Json示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Java+Selenium實(shí)現(xiàn)控制瀏覽器的啟動選項(xiàng)Options
這篇文章主要為大家詳細(xì)介紹了如何使用java代碼利用selenium控制瀏覽器的啟動選項(xiàng)Options的代碼操作,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-01-01

