长轮询和长连接
这是两个容易弄混的概念,直到今天我才算弄清楚
1 长连接
其实长连接是很常见的,只是当时不知道它叫长连接。像是很多rpc框架里都会有心跳检测功能,以防止客户端实际已经断开连接,但由于网络故障客户端的tcp链接已经断开了,但是服务端没有收到四次挥手,服务端无法断开。其实就是检测心跳,每次定时任务检查上次收到心跳包的时间距离当前的时间跨度是否大于了 设置的 时间长度。如果满足了断开条件就调用socket的close方法
2 长轮询
长轮询和长连接不同之处是,它不会一直发心跳来保持这个连接。而是满足某种条件之后,再重新发起连接。或者超时(业务定义的超时,而非tcp的超时)。
两者的区别,用一句话来说就是 长连接 一直都是同一个 socket。而长轮询是一个连接结束后,再次发起一个新的连接,以此来保持监听的持续性。
下面以代码为例说明下长轮询
客户端
package com.alibaba.demo; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import java.io.BufferedReader; import java.io.InputStreamReader; @Slf4j public class ConfigClient { private CloseableHttpClient httpClient; private RequestConfig requestConfig; public ConfigClient() { this.httpClient = HttpClientBuilder.create().build(); // ① httpClient 客户端超时时间要大于长轮询约定的超时时间 this.requestConfig = RequestConfig.custom().setSocketTimeout(40000).build(); } @SneakyThrows public void longPolling(String url, String dataId) { String endpoint = url + "?dataId=" + dataId; HttpGet request = new HttpGet(endpoint); request.setConfig(requestConfig); 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(); System.out.println("dataId: [{}] changed, receive configInfo: {}"); longPolling(url, dataId); break; } // ② 304 响应码标记配置未变更 case 304: { System.out.println("longPolling dataId: [{}] once finished, configInfo is unchanged, longPolling again"); longPolling(url, dataId); break; } default: { throw new RuntimeException("unExcepted HTTP status code"); } } } public static void main(String[] args) { // httpClient 会打印很多 debug 日志,关闭掉 ConfigClient configClient = new ConfigClient(); // ③ 对 dataId: user 进行配置监听 configClient.longPolling("http://127.0.0.1:8080/listener", "user"); } }
服务端
package com.alibaba.demo; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import com.google.common.util.concurrent.ThreadFactoryBuilder; import lombok.Data; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.AsyncContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Collection; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; @RestController @Slf4j @SpringBootApplication public class ConfigServer { @Data private static class AsyncTask { // 长轮询请求的上下文,包含请求和响应体 private AsyncContext asyncContext; // 超时标记 private boolean timeout; public AsyncTask(AsyncContext asyncContext, boolean timeout) { this.asyncContext = asyncContext; this.timeout = timeout; } } // guava 提供的多值 Map,一个 key 可以对应多个 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); // 配置监听接入点 @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); // 维护 dataId 和异步请求上下文的关联 dataIdContext.put(dataId, asyncTask); // 启动定时器,30s 后写入 304 响应 timeoutChecker.schedule(() -> { if (asyncTask.isTimeout()) { dataIdContext.remove(dataId, asyncTask); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); asyncContext.complete(); } }, 30000, TimeUnit.MILLISECONDS); } // 配置发布接入点
// 如果此时有配置的变更,直接调用complete() 返回http的响应给客户端
@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); } }
上述代码的意思是,模拟nacos或 apollo的 客户端监听配置变化。这两款最流行的配置中心都是采用拉模式获取变更的配置的。
其中 AsyncContext asyncContext = request.startAsync(request, response); 是servlet 3.0的新方法。
调用 startAsync 会将线程还给tomcat,AsyncContext 持有该连接的request和reponse。
posted on 2022-07-27 17:17 MaXianZhe 阅读(1616) 评论(0) 编辑 收藏 举报