From 887c4c50a79b2e192f0ccda26e78854569843a5c Mon Sep 17 00:00:00 2001 From: chengyouling Date: Mon, 13 Nov 2023 21:16:28 +0800 Subject: [PATCH] [#4004] fixed firstPull address choose logic --- .../center/client/ConfigCenterClient.java | 3 +- .../center/client/ConfigCenterManager.java | 10 ++-- .../center/client/ConfigCenterOperation.java | 3 +- .../config/kie/client/KieClient.java | 3 +- .../config/kie/client/KieConfigManager.java | 52 +++++++++++++------ .../config/kie/client/KieConfigOperation.java | 3 +- .../ConfigCenterConfigurationSourceImpl.java | 27 +++++----- .../kie/KieConfigurationSourceImpl.java | 3 +- 8 files changed, 66 insertions(+), 38 deletions(-) diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterClient.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterClient.java index a8ee3b99ec..f41fbd2284 100644 --- a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterClient.java +++ b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterClient.java @@ -58,14 +58,13 @@ public ConfigCenterClient(ConfigCenterAddressManager addressManager, HttpTranspo } @Override - public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request) { + public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request, String address) { String dimensionsInfo = buildDimensionsInfo(request, true); QueryConfigurationsResponse queryConfigurationsResponse = new QueryConfigurationsResponse(); Map configurations = new HashMap<>(); String uri = null; - String address = addressManager.address(); try { uri = address + "/configuration/items?dimensionsInfo=" + HttpUtils.encodeURLParam(dimensionsInfo) + "&revision=" + request.getRevision(); diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java index 9a89b92a6d..a5a4f90d31 100644 --- a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java +++ b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java @@ -45,13 +45,16 @@ public class ConfigCenterManager extends AbstractTask { private final ConfigCenterConfiguration configCenterConfiguration; - public ConfigCenterManager(ConfigCenterClient configCenterClient, EventBus eventBus, - ConfigConverter configConverter, ConfigCenterConfiguration configCenterConfiguration) { + private final ConfigCenterAddressManager configCenterAddressManager; + + public ConfigCenterManager(ConfigCenterClient configCenterClient, EventBus eventBus, ConfigConverter configConverter, + ConfigCenterConfiguration configCenterConfiguration, ConfigCenterAddressManager configCenterAddressManager) { super("config-center-configuration-task"); this.configCenterClient = configCenterClient; this.eventBus = eventBus; this.configConverter = configConverter; this.configCenterConfiguration = configCenterConfiguration; + this.configCenterAddressManager = configCenterAddressManager; } public void setQueryConfigurationsRequest(QueryConfigurationsRequest queryConfigurationsRequest) { @@ -72,7 +75,8 @@ public PollConfigurationTask(int failCount) { @Override public void execute() { try { - QueryConfigurationsResponse response = configCenterClient.queryConfigurations(queryConfigurationsRequest); + QueryConfigurationsResponse response = configCenterClient.queryConfigurations(queryConfigurationsRequest, + configCenterAddressManager.address()); if (response.isChanged()) { queryConfigurationsRequest.setRevision(response.getRevision()); Map lastData = configConverter.updateData(response.getConfigurations()); diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterOperation.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterOperation.java index 5e3a220706..d05967692e 100644 --- a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterOperation.java +++ b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterOperation.java @@ -25,8 +25,9 @@ public interface ConfigCenterOperation { /** * 根据查询条件查询配置项。 * @param request 查询的维度(project, application, serviceName, version) 和 revision 信息。 + * @param address 查询的配置中心地址。 * @return 如果存在配置变更,返回全量的配置项, changed = true。 如果没有变更, 返回 null, changed = false, * @throws OperationException If some problems happened to contact service center or non http 200 returned. */ - QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request); + QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request, String address); } diff --git a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java index 1e099e030f..ec567f90c2 100644 --- a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java +++ b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieClient.java @@ -67,8 +67,7 @@ public KieClient(KieAddressManager addressManager, HttpTransport httpTransport, } @Override - public ConfigurationsResponse queryConfigurations(ConfigurationsRequest request) { - String address = addressManager.address(); + public ConfigurationsResponse queryConfigurations(ConfigurationsRequest request, String address) { String url = buildUrl(request, address); try { if (kieConfiguration.isEnableLongPolling()) { diff --git a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java index 7c1eaf9238..2a8b4cd1c4 100644 --- a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java +++ b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java @@ -27,6 +27,7 @@ import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequest; import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequestFactory; import org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse; +import org.apache.servicecomb.config.kie.client.model.KieAddressManager; import org.apache.servicecomb.config.kie.client.model.KieConfiguration; import org.apache.servicecomb.http.client.task.AbstractTask; import org.apache.servicecomb.http.client.task.Task; @@ -51,9 +52,11 @@ public class KieConfigManager extends AbstractTask { private final KieConfiguration kieConfiguration; + private final KieAddressManager kieAddressManager; + + public KieConfigManager(KieConfigOperation configKieClient, EventBus eventBus, - KieConfiguration kieConfiguration, - ConfigConverter configConverter) { + KieConfiguration kieConfiguration, ConfigConverter configConverter, KieAddressManager kieAddressManager) { super("config-center-configuration-task"); this.configurationsRequests = ConfigurationsRequestFactory.buildConfigurationRequests(kieConfiguration); this.configurationsRequests.sort(ConfigurationsRequest::compareTo); @@ -61,30 +64,46 @@ public KieConfigManager(KieConfigOperation configKieClient, EventBus eventBus, this.eventBus = eventBus; this.configConverter = configConverter; this.kieConfiguration = kieConfiguration; + this.kieAddressManager = kieAddressManager; } public void firstPull() { + Map data = new HashMap<>(); try { - Map data = new HashMap<>(); - this.configurationsRequests.forEach(r -> { - r.setRevision(ConfigurationsRequest.INITIAL_REVISION); - ConfigurationsResponse response = configKieClient.queryConfigurations(r); - if (response.isChanged()) { - r.setRevision(response.getRevision()); - r.setLastRawData(response.getConfigurations()); - data.putAll(response.getConfigurations()); - } - }); - this.configConverter.updateData(data); - } catch (RuntimeException e) { + firstQueryConfigurations(data); + } catch (Exception e) { if (this.kieConfiguration.isFirstPullRequired()) { throw e; } else { - LOGGER.warn("first pull failed, and ignore {}", e.getMessage()); + LOGGER.warn("first pull failed!"); } } } + private void firstQueryConfigurations(Map data) { + for (int i = 0; i < 3;) { + String address = kieAddressManager.address(); + try { + this.configurationsRequests.forEach(r -> { + r.setRevision(ConfigurationsRequest.INITIAL_REVISION); + ConfigurationsResponse response = configKieClient.queryConfigurations(r, address); + if (response.isChanged()) { + r.setRevision(response.getRevision()); + r.setLastRawData(response.getConfigurations()); + data.putAll(response.getConfigurations()); + } + }); + this.configConverter.updateData(data); + break; + } catch (Exception e) { + if (i == 2) { + throw e; + } + LOGGER.warn("firstQueryConfigurations failed, config address {} and ignore {}", address, e.getMessage()); + } + i++; + } + } private void onDataChanged() { Map latestData = new HashMap<>(); this.configurationsRequests.forEach(r -> latestData.putAll(r.getLastRawData())); @@ -121,7 +140,8 @@ public PollConfigurationTask(int failCount, ConfigurationsRequest configurations @Override public void execute() { try { - ConfigurationsResponse response = configKieClient.queryConfigurations(configurationsRequest); + ConfigurationsResponse response = configKieClient.queryConfigurations(configurationsRequest, + kieAddressManager.address()); if (response.isChanged()) { configurationsRequest.setRevision(response.getRevision()); configurationsRequest.setLastRawData(response.getConfigurations()); diff --git a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigOperation.java b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigOperation.java index 364774fd22..229439867c 100644 --- a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigOperation.java +++ b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigOperation.java @@ -26,8 +26,9 @@ public interface KieConfigOperation { /** * 根据查询条件查询配置项。 * @param request 查询的维度(project, application, serviceName, version) 和 revision 信息。 + * @param address 查询的配置中心地址。 * @return 如果存在配置变更,返回全量的配置项, changed = true。 如果没有变更, 返回 null, changed = false, * @throws OperationException If some problems happened to contact service center or non http 200 returned. */ - ConfigurationsResponse queryConfigurations(ConfigurationsRequest request); + ConfigurationsResponse queryConfigurations(ConfigurationsRequest request, String address); } diff --git a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/ConfigCenterConfigurationSourceImpl.java b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/ConfigCenterConfigurationSourceImpl.java index 74834aaef6..fb516580a3 100644 --- a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/ConfigCenterConfigurationSourceImpl.java +++ b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/ConfigCenterConfigurationSourceImpl.java @@ -88,29 +88,30 @@ public boolean isValidSource(Configuration localConfiguration) { public void init(Configuration localConfiguration) { configConverter = new ConfigConverter(ConfigCenterConfig.INSTANCE.getFileSources()); - ConfigCenterAddressManager kieAddressManager = configKieAddressManager(); + ConfigCenterAddressManager configCenterAddressManager = configCenterAddressManager(); - HttpTransport httpTransport = createHttpTransport(kieAddressManager, + HttpTransport httpTransport = createHttpTransport(configCenterAddressManager, HttpTransportFactory.defaultRequestConfig().build(), localConfiguration); - ConfigCenterClient configCenterClient = new ConfigCenterClient(kieAddressManager, httpTransport); + ConfigCenterClient configCenterClient = new ConfigCenterClient(configCenterAddressManager, httpTransport); EventManager.register(this); ConfigCenterConfiguration configCenterConfiguration = createConfigCenterConfiguration(); - QueryConfigurationsRequest queryConfigurationsRequest = firstPull(configCenterClient); + QueryConfigurationsRequest queryConfigurationsRequest = firstPull(configCenterClient, configCenterAddressManager); configCenterManager = new ConfigCenterManager(configCenterClient, EventManager.getEventBus(), - configConverter, configCenterConfiguration); + configConverter, configCenterConfiguration, configCenterAddressManager); configCenterManager.setQueryConfigurationsRequest(queryConfigurationsRequest); configCenterManager.startConfigCenterManager(); } - private QueryConfigurationsRequest firstPull(ConfigCenterClient configCenterClient) { + private QueryConfigurationsRequest firstPull(ConfigCenterClient configCenterClient, + ConfigCenterAddressManager configCenterAddressManager) { QueryConfigurationsRequest queryConfigurationsRequest = createQueryConfigurationsRequest(); try { QueryConfigurationsResponse response = configCenterClient - .queryConfigurations(queryConfigurationsRequest); + .queryConfigurations(queryConfigurationsRequest, configCenterAddressManager.address()); if (response.isChanged()) { configConverter.updateData(response.getConfigurations()); updateConfiguration(WatchedUpdateResult.createIncremental(configConverter.getCurrentData(), null, null)); @@ -146,8 +147,8 @@ private ConfigCenterConfiguration createConfigCenterConfiguration(){ return new ConfigCenterConfiguration().setRefreshIntervalInMillis(ConfigCenterConfig.INSTANCE.getRefreshInterval()); } - private HttpTransport createHttpTransport(ConfigCenterAddressManager kieAddressManager, RequestConfig requestConfig, - Configuration localConfiguration) { + private HttpTransport createHttpTransport(ConfigCenterAddressManager configCenterAddressManager, + RequestConfig requestConfig, Configuration localConfiguration) { List authHeaderProviders = SPIServiceUtils.getOrLoadSortedService(AuthHeaderProvider.class); if (ConfigCenterConfig.INSTANCE.isProxyEnable()) { @@ -165,14 +166,16 @@ private HttpTransport createHttpTransport(ConfigCenterAddressManager kieAddressM return HttpTransportFactory .createHttpTransport( TransportUtils - .createSSLProperties(kieAddressManager.sslEnabled(), localConfiguration, ConfigCenterConfig.SSL_TAG), + .createSSLProperties(configCenterAddressManager.sslEnabled(), localConfiguration, + ConfigCenterConfig.SSL_TAG), getRequestAuthHeaderProvider(authHeaderProviders), httpClientBuilder); } return HttpTransportFactory .createHttpTransport( TransportUtils - .createSSLProperties(kieAddressManager.sslEnabled(), localConfiguration, ConfigCenterConfig.SSL_TAG), + .createSSLProperties(configCenterAddressManager.sslEnabled(), localConfiguration, + ConfigCenterConfig.SSL_TAG), getRequestAuthHeaderProvider(authHeaderProviders), requestConfig); } @@ -184,7 +187,7 @@ private static RequestAuthHeaderProvider getRequestAuthHeaderProvider(List