nacos example(官方demo)
public static void main(String[] args) throws NacosException, InterruptedException {
String serverAddr = "localhost:8848";
String dataId = "test";
String group = "DEFAULT_GROUP";
Properties properties = new Properties();
properties.put("serverAddr", serverAddr);
//1、创建一个ConfigService
ConfigService configService = NacosFactory.createConfigService(properties);
//2、从服务端获取数据
String content = configService.getConfig(dataId, group, 5000);
System.out.println(content);
//3、添加监听器
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("receive:" + configInfo);
}
@Override
public Executor getExecutor() {
return null;
}
});
...
}
请求数据简单流程图
创建ConfigService
static ConfigService createConfigService(
NacosConfigProperties nacosConfigProperties) {
if (Objects.isNull(service)) {
synchronized (NacosConfigManager.class) {
try {
if (Objects.isNull(service)) {
service = NacosFactory.createConfigService(
nacosConfigProperties.assembleConfigServiceProperties());
}
}
catch (NacosException e) {
log.error(e.getMessage());
throw new NacosConnectionFailureException(
nacosConfigProperties.getServerAddr(), e.getMessage(), e);
}
}
}
return service;
}
public NacosConfigService(Properties properties) throws NacosException {
ValidatorUtils.checkInitParam(properties);
String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
if (StringUtils.isBlank(encodeTmp)) {
this.encode = Constants.ENCODE;
} else {
this.encode = encodeTmp.trim();
}
//根据配置初始化namespace
initNamespace(properties);
//用来跟server发http请求的
this.agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
this.agent.start();
//这里是客户端的重点
this.worker = new ClientWorker(this.agent, this.configFilterChainManager, properties);
}
//----------------------------------✂-----------------------------------------
// ClientWorker里面主要是初始化两个线程池
// executor线程池中延迟10ms启动了一个线程去执行checkConfigInfo()方法
public ClientWorker(final HttpAgent agent, final ConfigFilterChainManager configFilterChainManager, final Properties properties) {
this.agent = agent;
this.configFilterChainManager = configFilterChainManager;
// Initialize the timeout parameter
init(properties);
//用来执行定时任务
this.executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.Worker." + agent.getName());
t.setDaemon(true);
return t;
}
});
//长轮训
this.executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.Worker.longPolling." + agent.getName());
t.setDaemon(true);
return t;
}
});
//创建一个定时延时线程,在上一个任务完成后延迟10S进行
this.executor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
//这个方法是核心,上一个任务完成后隔10s会调用一次这个方法
checkConfigInfo();
} catch (Throwable e) {
LOGGER.error("[" + agent.getName() + "] [sub-check] rotate check error", e);
}
}
}, 1L, 10L, TimeUnit.MILLISECONDS);
}
//----------------------------------✂-----------------------------------------
//listener调用该方法
public void checkConfigInfo() {
// Dispatch taskes. 分发任务
int listenerSize = cacheMap.get().size();
// Round up the longingTaskCount. 向上取整
int longingTaskCount = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize());
if (longingTaskCount > currentLongingTaskCount) {
for (int i = (int) currentLongingTaskCount; i < longingTaskCount; i++) {
// The task list is no order.So it maybe has issues when changing.
//任务是无序的,变化过程中可能会有问题,执行LongPollingRunnable.run()
executorService.execute(new LongPollingRunnable(i));
}
currentLongingTaskCount = longingTaskCount;
}
}
//----------------------------------✂-----------------------------------------
class LongPollingRunnable implements Runnable {
@Override
public void run() {
//初始化一个cacheData
List<CacheData> cacheDatas = new ArrayList<CacheData>();
List<String> inInitializingCacheList = new ArrayList<String>();
try {
// check failover config
// cacheMap:增加监听时,将具体的监听对应的data封装成CacheData缓存进cacheMap
// 在封装CacheData时,会计算出CacheData对应的taskId
for (CacheData cacheData : cacheMap.get().values()) {
if (cacheData.getTaskId() == taskId) {
cacheDatas.add(cacheData);
try {
// 检查内存中的缓存信息与文件中的缓存信息是否一致,并进行相应处理
checkLocalConfig(cacheData);
if (cacheData.isUseLocalConfigInfo()) {
// 监听对象创建时,会在对象中缓存一个当前的信息值
// 判断监听的信息是否好内存中缓存的一致,不一致就回调监听中的回调的方法
cacheData.checkListenerMd5();
}
} catch (Exception e) {
LOGGER.error("get local config info error", e);
}
}
}
// check server config
// 长轮询
// 通过长轮询的方式,调用服务端的listener接口,从服务端获取变化的配置
// 默认是30s超时
List<String> changedGroupKeys = checkUpdateDataIds(cacheDatas, inInitializingCacheList);
if (!CollectionUtils.isEmpty(changedGroupKeys)) {
LOGGER.info("get changedGroupKeys:" + changedGroupKeys);
}
// 获得变化的配置后,从server端查询最新的配置信息,并更新到本地的缓存文件中
for (String groupKey : changedGroupKeys) {
String[] key = GroupKey.parseKey(groupKey);
String dataId = key[0];
String group = key[1];
String tenant = null;
if (key.length == 3) {
tenant = key[2];
}
try {
String[] ct = getServerConfig(dataId, group, tenant, 3000L);
CacheData cache = cacheMap.get().get(GroupKey.getKeyTenant(dataId, group, tenant));
cache.setContent(ct[0]);
if (null != ct[1]) {
cache.setType(ct[1]);
}
} catch (NacosException ioe) {...}
}
for (CacheData cacheData : cacheDatas) {
if (!cacheData.isInitializing() || inInitializingCacheList
.contains(GroupKey.getKeyTenant(cacheData.dataId, cacheData.group, cacheData.tenant))) {
cacheData.checkListenerMd5();
cacheData.setInitializing(false);
}
}
inInitializingCacheList.clear();
executorService.execute(this);
} catch (Throwable e) {
executorService.schedule(this, taskPenaltyTime, TimeUnit.MILLISECONDS);
}
}
}
List<String> checkUpdateDataIds(List<CacheData> cacheDatas, List<String> inInitializingCacheList) throws IOException {
StringBuilder sb = new StringBuilder();
for (CacheData cacheData : cacheDatas) {
if (!cacheData.isUseLocalConfigInfo()) {
sb.append(cacheData.dataId).append(WORD_SEPARATOR);
sb.append(cacheData.group).append(WORD_SEPARATOR);
if (StringUtils.isBlank(cacheData.tenant)) {
sb.append(cacheData.getMd5()).append(LINE_SEPARATOR);
} else {
sb.append(cacheData.getMd5()).append(WORD_SEPARATOR);
sb.append(cacheData.getTenant()).append(LINE_SEPARATOR);
}
if (cacheData.isInitializing()) {
// It updates when cacheData occours in cacheMap by first time.
inInitializingCacheList
.add(GroupKey.getKeyTenant(cacheData.dataId, cacheData.group, cacheData.tenant));
}
}
}
boolean isInitializingCacheList = !inInitializingCacheList.isEmpty();
//拼装所有的节点
return checkUpdateConfigStr(sb.toString(), isInitializingCacheList);
}
//----------------------------------✂-----------------------------------------
List<String> checkUpdateConfigStr(String probeUpdateString, boolean isInitializingCacheList) throws IOException {
List<String> params = new ArrayList<String>(2);
params.add(Constants.PROBE_MODIFY_REQUEST);
params.add(probeUpdateString);
List<String> headers = new ArrayList<String>(2);
headers.add("Long-Pulling-Timeout");
headers.add("" + timeout);
// told server do not hang me up if new initializing cacheData added in
if (isInitializingCacheList) {
headers.add("Long-Pulling-Timeout-No-Hangup");
headers.add("true");
}
if (StringUtils.isBlank(probeUpdateString)) {
return Collections.emptyList();
}
try {
// 这里的调用就是所谓的长轮询
// url: http://ip:port/v1/cs/configs/listener
// 超时时间是默认值30s
long readTimeoutMs = timeout + (long) Math.round(timeout >> 1);
//发送请求检查是否有修改(如何加密检查)
HttpResult result = agent.httpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, params,
agent.getEncode(), readTimeoutMs);
if (HttpURLConnection.HTTP_OK == result.code) {
setHealthServer(true);
return parseUpdateDataIdResponse(result.content);
} else {
setHealthServer(false);
LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", agent.getName(), result.code);
}
} catch (IOException e) {
setHealthServer(false);
LOGGER.error("[" + agent.getName() + "] [check-update] get changed dataId exception", e);
throw e;
}
return Collections.emptyList();
}
2、以上是客户端逻辑,接下来是服务端接受请求
@PostMapping("/listener")
@Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class)
public void listener(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
String probeModify = request.getParameter("Listening-Configs");
if (StringUtils.isBlank(probeModify)) {
throw new IllegalArgumentException("invalid probeModify");
}
System.out.println("change listener is " + probeModify + "----" + LocalDateTime.now());
probeModify = URLDecoder.decode(probeModify, Constants.ENCODE);
Map<String, String> clientMd5Map;
try {
clientMd5Map = MD5Util.getClientMd5Map(probeModify);
} catch (Throwable e) {
throw new IllegalArgumentException("invalid probeModify");
}
// do long-polling
inner.doPollingConfig(request, response, clientMd5Map, probeModify.length());
}
//----------------------------------✂-----------------------------------------
/**
* 轮询接口
*/
public String doPollingConfig(HttpServletRequest request, HttpServletResponse response,
Map<String, String> clientMd5Map, int probeRequestSize)
throws IOException {
// 长轮询
if (LongPollingService.isSupportLongPolling(request)) {
longPollingService.addLongPollingClient(request, response, clientMd5Map, probeRequestSize);
return HttpServletResponse.SC_OK + "";
}
//以下的原代码兼容短轮询的,不走这方面,这边不做解释,有兴趣可以自己看
...
}
//----------------------------------✂-----------------------------------------
public void addLongPollingClient(HttpServletRequest req, HttpServletResponse rsp, Map<String, String> clientMd5Map,
int probeRequestSize) {
String str = req.getHeader(LongPollingService.LONG_POLLING_HEADER);
String noHangUpFlag = req.getHeader(LongPollingService.LONG_POLLING_NO_HANG_UP_HEADER);
String appName = req.getHeader(RequestUtil.CLIENT_APPNAME_HEADER);
String tag = req.getHeader("Vipserver-Tag");
int delayTime = SwitchService.getSwitchInteger(SwitchService.FIXED_DELAY_TIME, 500);
/**
* 提前500ms返回响应,为避免客户端超时 @qiaoyi.dingqy 2013.10.22改动 add delay time for LoadBalance
*/
long timeout = Math.max(10000, Long.parseLong(str) - delayTime);
if (isFixedPolling()) {
timeout = Math.max(10000, getFixedPollingInterval());
// do nothing but set fix polling timeout
} else {
long start = System.currentTimeMillis();
// 比较客户端的md5与当前server端的是否一致,不一致的返回修改的配置
List<String> changedGroups = MD5Util.compareMd5(req, rsp, clientMd5Map);
if (changedGroups.size() > 0) {
// 当前有MD5不一致的,生成响应信息直接返回
generateResponse(req, rsp, changedGroups);
LogUtil.clientLog.info("{}|{}|{}|{}|{}|{}|{}",
System.currentTimeMillis() - start, "instant", RequestUtil.getRemoteIp(req), "polling",
clientMd5Map.size(), probeRequestSize, changedGroups.size());
return;
}
// 当前无MD5不一致的,判断客户端传过来的Long-Pulling-Timeout-No-Hangup是否为true
// 如果为true,不需要Hangup,直接返回
// 客户端只在首次查询时,才会将此值设置为true,之后的查询都需要进行Hangup处理
else if (noHangUpFlag != null && noHangUpFlag.equalsIgnoreCase(TRUE_STR)) {
return;
}
}
String ip = RequestUtil.getRemoteIp(req);
// 一定要由HTTP线程调用,否则离开后容器会立即发送响应
final AsyncContext asyncContext = req.startAsync();
// AsyncContext.setTimeout()的超时时间不准,所以只能自己控制
asyncContext.setTimeout(0L);
// 执行长轮询的线程
scheduler.execute(
new ClientLongPolling(asyncContext, clientMd5Map, ip, probeRequestSize, timeout, appName, tag));
}
//----------------------------------✂-----------------------------------------
class ClientLongPolling implements Runnable {
public void run() {
// 注:这边allSubs.add(this);的代码应该是在创建定时任务下面的,为了方便理解放到上面来了
//将当前ClientLongPolling添加到订阅者队列中
allSubs.add(this);
// 创建一个定时任务延迟29.5s执行,(返回给客户端保证不超时)
asyncTimeoutFuture = scheduler.schedule(new Runnable() {
@Override
public void run() {
try {
getRetainIps().put(ClientLongPolling.this.ip, System.currentTimeMillis());
/** 到达超时时间,当前ClientLongPolling不需要再维持订阅关系
* 删除订阅关系
*/
allSubs.remove(ClientLongPolling.this);
//默认不走该分支
if (isFixedPolling()) {... }
else {
LogUtil.clientLog.info("{}|{}|{}|{}|{}|{}",
(System.currentTimeMillis() - createTime),
"timeout", RequestUtil.getRemoteIp((HttpServletRequest) asyncContext.getRequest()),
"polling",
clientMd5Map.size(), probeRequestSize);
System.out.println("sendResponse 413 " + "-----" + LocalDateTime.now());
// 到达超时时间,结束Hangup,生成响应信息并返回
sendResponse(null);
}
} catch (Throwable t) {
LogUtil.defaultLog.error("long polling error:" + t.getMessage(), t.getCause());
}
}
}, timeoutTime, TimeUnit.MILLISECONDS);
}
allSubs主要是一个订阅队列,每当有配置修改的时候就会这个事件
@Override
public void onEvent(Event event) {
if (isFixedPolling()) {
// ignore
} else {
if (event instanceof LocalDataChangeEvent) {
LocalDataChangeEvent evt = (LocalDataChangeEvent) event;
scheduler.execute(new DataChangeTask(evt.groupKey, evt.isBeta, evt.betaIps));
}
}
}
//----------------------------------✂-----------------------------------------
class DataChangeTask implements Runnable {
@Override
public void run() {
try {
ConfigCacheService.getContentBetaMd5(groupKey);
//轮询allSub里面对这个修改key感兴趣的线程
for (Iterator<ClientLongPolling> iter = allSubs.iterator(); iter.hasNext(); ) {
ClientLongPolling clientSub = iter.next();
if (clientSub.clientMd5Map.containsKey(groupKey)) {
// 如果beta发布且不在beta列表直接跳过
if (isBeta && !CollectionUtils.contains(betaIps, clientSub.ip)) {
continue;
}
// 如果tag发布且不在tag列表直接跳过
if (StringUtils.isNotBlank(tag) && !tag.equals(clientSub.tag)) {
continue;
}
getRetainIps().put(clientSub.ip, System.currentTimeMillis());
iter.remove(); // 删除订阅关系
//向客户端返回修改数据
clientSub.sendResponse(Arrays.asList(groupKey));
}
}
} catch (Throwable t) {
LogUtil.defaultLog.error("data change error: {}", ExceptionUtil.getStackTrace(t));
}
}
}
总结
- 客户端启动时会初始化一个NacosConfigService
它开启一个定时任务开启一个长轮训线程去服务端请求 - 服务端接收到请求后,首先查看查询的配置有没有变化,如果有变化他直接返回变化key,
如果没有配置变化他就把这个请求添加到allSubs订阅队列里面,等待事件唤醒,并且会开启一个定时任务线程(假设为定时任务A),执行时间是该长轮训快超时的29.5s(默认)。
如果在这段时间内allSubs的该订阅线程被唤醒了(也就是配置发生了改变),它会删除掉allSubs里该线程的订阅,并且返回修改的key
当该线程A的定时任务被唤醒的时候,会删除掉该线程的订阅,并且向客户端返回空数值
- 问题1:如果在定时任务A执行之前,事件已经触发了,那定时任务还会执行吗
答:每次它返回给客户端的时候会调用ClientLongPolling#sendResponse,这里面会取消掉定时任务
void sendResponse(List<String> changedGroups) {
/**
* 取消超时任务
*/
if (null != asyncTimeoutFuture) {
asyncTimeoutFuture.cancel(false);
}
generateResponse(changedGroups);
}
- 客户端接收到请求响应之后如果收到变更key,则会像服务端请求最新的key数据保存到本地,如果没有变更key的话,经过10s会继续再发起轮询请求(应用schedulewithfixeddelay来实现)
网友评论