-
Notifications
You must be signed in to change notification settings - Fork 820
[SCB-526]fetch once from dynamic config source when boot up #681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
29a7145
d43b41b
ef7918a
45398c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,8 @@ public class ConfigCenterClient { | |
|
|
||
| private static final long HEARTBEAT_INTERVAL = 30000; | ||
|
|
||
| private static final long BOOTUP_WAIT_TIME = 10; | ||
|
|
||
| private ScheduledExecutorService heartbeatTask = null; | ||
|
|
||
| private int refreshMode = CONFIG_CENTER_CONFIG.getRefreshMode(); | ||
|
|
@@ -129,7 +131,9 @@ public void connectServer() { | |
| throw new IllegalStateException(e); | ||
| } | ||
| refreshMembers(memberDiscovery); | ||
| EXECUTOR.scheduleWithFixedDelay(new ConfigRefresh(parseConfigUtils, memberDiscovery), | ||
| ConfigRefresh refreshTask = new ConfigRefresh(parseConfigUtils, memberDiscovery); | ||
| refreshTask.run(true); | ||
| EXECUTOR.scheduleWithFixedDelay(refreshTask, | ||
| firstRefreshInterval, | ||
| refreshInterval, | ||
| TimeUnit.MILLISECONDS); | ||
|
|
@@ -155,6 +159,9 @@ private void refreshMembers(MemberDiscovery memberDiscovery) { | |
| request.headers().add("X-Auth-Token", ConfigCenterConfig.INSTANCE.getToken()); | ||
| } | ||
| authHeaderProviders.forEach(provider -> request.headers().addAll(provider.getSignAuthHeaders(signReq))); | ||
| request.exceptionHandler(e -> { | ||
| LOGGER.error("Fetch member from {} failed. Error message is [{}].", configCenter, e.getMessage()); | ||
| }); | ||
| request.end(); | ||
| }); | ||
| } | ||
|
|
@@ -211,25 +218,29 @@ class ConfigRefresh implements Runnable { | |
| this.memberdis = memberdis; | ||
| } | ||
|
|
||
| // 具体动作 | ||
| @Override | ||
| public void run() { | ||
| public void run(boolean wait) { | ||
| // this will be single threaded, so we don't care about concurrent | ||
| // staffs | ||
| try { | ||
| String configCenter = memberdis.getConfigServer(); | ||
| if (refreshMode == 1) { | ||
| refreshConfig(configCenter); | ||
| refreshConfig(configCenter, wait); | ||
| } else if (!isWatching) { | ||
| // 重新监听时需要先加载,避免在断开期间丢失变更 | ||
| refreshConfig(configCenter); | ||
| refreshConfig(configCenter, wait); | ||
| doWatch(configCenter); | ||
| } | ||
| } catch (Exception e) { | ||
| LOGGER.error("client refresh thread exception", e); | ||
| } | ||
| } | ||
|
|
||
| // 具体动作 | ||
| @Override | ||
| public void run() { | ||
| run(false); | ||
| } | ||
|
|
||
| // create watch and wait for done | ||
| public void doWatch(String configCenter) | ||
| throws UnsupportedEncodingException, InterruptedException { | ||
|
|
@@ -270,7 +281,7 @@ public void doWatch(String configCenter) | |
| LOGGER.info("watching config recieved {}", action); | ||
| Map<String, Object> mAction = action.toJsonObject().getMap(); | ||
| if ("CREATE".equals(mAction.get("action"))) { | ||
| refreshConfig(configCenter); | ||
| refreshConfig(configCenter, false); | ||
| } else if ("MEMBER_CHANGE".equals(mAction.get("action"))) { | ||
| refreshMembers(memberdis); | ||
| } else { | ||
|
|
@@ -282,7 +293,10 @@ public void doWatch(String configCenter) | |
| waiter.countDown(); | ||
| }, | ||
| e -> { | ||
| LOGGER.error("watcher connect to config center {} refresh port {} failed. Error message is [{}]", configCenter, refreshPort, e.getMessage()); | ||
| LOGGER.error("watcher connect to config center {} refresh port {} failed. Error message is [{}]", | ||
| configCenter, | ||
| refreshPort, | ||
| e.getMessage()); | ||
| waiter.countDown(); | ||
| }); | ||
| }); | ||
|
|
@@ -313,7 +327,8 @@ private void sendHeartbeat(WebSocket ws) { | |
| } | ||
| } | ||
|
|
||
| public void refreshConfig(String configcenter) { | ||
| public void refreshConfig(String configcenter, boolean wait) { | ||
| CountDownLatch latch = new CountDownLatch(1); | ||
| clientMgr.findThreadBindClientPool().runOnContext(client -> { | ||
| String path = URIConst.ITEMS + "?dimensionsInfo=" + StringUtils.deleteWhitespace(serviceName); | ||
| IpPort ipPort = NetUtils.parseIpPortFromURI(configcenter); | ||
|
|
@@ -330,10 +345,12 @@ public void refreshConfig(String configcenter) { | |
| EventManager.post(new ConnFailEvent("config refresh result parse fail " + e.getMessage())); | ||
| LOGGER.error("Config refresh from {} failed. Error message is [{}].", configcenter, e.getMessage()); | ||
| } | ||
| latch.countDown(); | ||
| }); | ||
| } else { | ||
| rsp.bodyHandler(buf -> { | ||
| LOGGER.error("Server error message is [{}].", buf); | ||
| latch.countDown(); | ||
| }); | ||
| EventManager.post(new ConnFailEvent("fetch config fail")); | ||
| LOGGER.error("Config refresh from {} failed.", configcenter); | ||
|
|
@@ -354,9 +371,19 @@ public void refreshConfig(String configcenter) { | |
| request.exceptionHandler(e -> { | ||
| EventManager.post(new ConnFailEvent("fetch config fail")); | ||
| LOGGER.error("Config refresh from {} failed. Error message is [{}].", configcenter, e.getMessage()); | ||
| latch.countDown(); | ||
| }); | ||
| request.end(); | ||
| }); | ||
| if (wait) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar wait code here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two steps, so got similar code.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed refresh member wait logic. Because we can use the configured address and it's first time, no need to wait this operation. |
||
| LOGGER.info("Refreshing remote config..."); | ||
| try { | ||
| latch.await(BOOTUP_WAIT_TIME, TimeUnit.SECONDS); | ||
| } catch (InterruptedException e) { | ||
| LOGGER.warn(e.getMessage()); | ||
| } | ||
| LOGGER.info("Refreshing remote config is done."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
java.lang.Thread#getThreads is cheaper