diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/AddressManager.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/AddressManager.java index 82561645f3a..5a32ed75d72 100644 --- a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/AddressManager.java +++ b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/AddressManager.java @@ -28,12 +28,12 @@ public class AddressManager extends AbstractAddressManager { public AddressManager(String projectName, List addresses, EventBus eventBus) { - super(projectName, addresses, "/v3/"); + super(projectName, addresses); eventBus.register(this); } @Subscribe public void onRefreshEndpointEvent(RefreshEndpointEvent event) { - refreshEndpoint(event, "CseConfigCenter"); + refreshEndpoint(event, RefreshEndpointEvent.CONFIG_CENTER_NAME); } } \ No newline at end of file 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 8c88972b5e1..5238126c1fa 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 @@ -65,9 +65,8 @@ public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsReques Map configurations = new HashMap<>(); String uri = null; - String currentAddress = addressManager.address(); try { - uri = currentAddress + "/configuration/items?dimensionsInfo=" + uri = addressManager.address() + "/configuration/items?dimensionsInfo=" + HttpUtils.encodeURLParam(dimensionsInfo) + "&revision=" + request.getRevision(); Map headers = new HashMap<>(); 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 539754b7862..cc891ff1fbb 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 @@ -52,8 +52,6 @@ public class KieClient implements KieConfigOperation { protected String revision = "0"; - private String currentAddress = ""; - private final KieAddressManager addressManager; private final KieConfiguration kieConfiguration; diff --git a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/model/KieAddressManager.java b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/model/KieAddressManager.java index e6ef9d7a3a9..0742c11fb10 100644 --- a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/model/KieAddressManager.java +++ b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/model/KieAddressManager.java @@ -34,6 +34,6 @@ public KieAddressManager(List addresses, EventBus eventBus) { @Subscribe public void onRefreshEndpointEvent(RefreshEndpointEvent event) { - refreshEndpoint(event, "KIE"); + refreshEndpoint(event, RefreshEndpointEvent.KIE_NAME); } } diff --git a/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/common/AbstractAddressManager.java b/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/common/AbstractAddressManager.java index 68fe5f000fa..995fe824ddf 100644 --- a/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/common/AbstractAddressManager.java +++ b/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/common/AbstractAddressManager.java @@ -34,18 +34,20 @@ public class AbstractAddressManager { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractAddressManager.class); + public static final String DEFAULT_PROJECT = "default"; + + public static final String V4_PREFIX = "/v4/"; + + private static final String V3_PREFIX = "/v3/"; + private List addresses = new ArrayList<>(); private int index = 0; private String projectName; - private String key; - private String currentAddress = ""; - public static final String DEFAULT_PROJECT = "default"; - private volatile List availableZone = new ArrayList<>(); private volatile List availableRegion = new ArrayList<>(); @@ -59,9 +61,8 @@ public AbstractAddressManager(List addresses) { this.addresses.addAll(addresses); } - public AbstractAddressManager(String projectName, List addresses, String key) { + public AbstractAddressManager(String projectName, List addresses) { this.projectName = StringUtils.isEmpty(projectName) ? DEFAULT_PROJECT : projectName; - this.key = key; this.addresses = this.transformAddress(addresses); } @@ -78,16 +79,17 @@ public boolean sslEnabled() { return address().startsWith("https://"); } - private List transformAddress(List addresses) { - if (key == "/v3/") { - return addresses.stream().map(this::formatAddress).collect(Collectors.toList()); - } - return addresses; + protected List transformAddress(List addresses) { + return addresses.stream().map(this::formatAddress).collect(Collectors.toList()); + } + + protected String getUrlPrefix(String address) { + return address + V3_PREFIX; } private String formatAddress(String address) { try { - return address + key + HttpUtils.encodeURLParam(this.projectName); + return getUrlPrefix(address) + HttpUtils.encodeURLParam(this.projectName); } catch (Exception e) { throw new IllegalStateException("not possible"); } @@ -154,7 +156,7 @@ private String getUri(String endpoint) { } public void refreshEndpoint(RefreshEndpointEvent event, String key) { - if (null == event || event.getName() != key) { + if (null == event || !event.getName().equals(key)) { return; } availableZone = event.getSameZone(); diff --git a/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/event/RefreshEndpointEvent.java b/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/event/RefreshEndpointEvent.java index 1e940d25b7d..231abdb7f46 100644 --- a/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/event/RefreshEndpointEvent.java +++ b/clients/http-client-common/src/main/java/org/apache/servicecomb/http/client/event/RefreshEndpointEvent.java @@ -23,6 +23,14 @@ public class RefreshEndpointEvent { + public static final String SERVICE_CENTER_NAME = "SERVICECENTER"; + + public static final String KIE_NAME = "KIE"; + + public static final String CONFIG_CENTER_NAME = "CseConfigCenter"; + + public static final String CSE_MONITORING_NAME = "CseMonitoring"; + private static final String SAME_ZONE = "sameZone"; private static final String SAME_REGION = "sameRegion"; diff --git a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/AddressManager.java b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/AddressManager.java index 8294d6e26b7..1185fdedd41 100644 --- a/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/AddressManager.java +++ b/clients/service-center-client/src/main/java/org/apache/servicecomb/service/center/client/AddressManager.java @@ -27,13 +27,22 @@ public class AddressManager extends AbstractAddressManager { public AddressManager(String projectName, List addresses, EventBus eventBus) { - super(projectName, addresses, "/v4/"); + super(projectName, addresses); eventBus.register(this); } + @Override + protected List transformAddress(List addresses) { + return addresses; + } + + @Override + protected String getUrlPrefix(String address) { + return address + V4_PREFIX; + } @Subscribe public void onRefreshEndpointEvent(RefreshEndpointEvent event) { - refreshEndpoint(event, "SERVICECENTER"); + refreshEndpoint(event, RefreshEndpointEvent.SERVICE_CENTER_NAME); } } diff --git a/clients/service-center-client/src/test/java/org/apache/servicecomb/service/center/client/AddressManagerTest.java b/clients/service-center-client/src/test/java/org/apache/servicecomb/service/center/client/AddressManagerTest.java new file mode 100644 index 00000000000..eaacb21785f --- /dev/null +++ b/clients/service-center-client/src/test/java/org/apache/servicecomb/service/center/client/AddressManagerTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.service.center.client; + +import java.util.Arrays; + +import org.junit.Assert; +import org.junit.Test; + +import com.google.common.eventbus.EventBus; + +public class AddressManagerTest { + + private static final String PROJECT_NAME = "default"; + + @Test + public void AddressManagerTest() { + + AddressManager addressManager = new AddressManager(PROJECT_NAME, Arrays.asList("http://127.0.0.1:30100"), + new EventBus()); + Assert.assertNotNull(addressManager); + + String addressPrefix = addressManager.getUrlPrefix("http://127.0.0.1:30100"); + Assert.assertEquals("http://127.0.0.1:30100/v4/", addressPrefix); + + String address = addressManager.address(); + Assert.assertEquals("http://127.0.0.1:30100", address); + + address = addressManager.formatUrl("/test", false); + Assert.assertEquals("http://127.0.0.1:30100/v4/default/test", address); + + address = addressManager.formatUrl("/test", true); + Assert.assertEquals("http://127.0.0.1:30100/test", address); + } +} \ No newline at end of file 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 e4b42c89cf2..ee94138f2af 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 @@ -65,8 +65,6 @@ public class ConfigCenterConfigurationSourceImpl implements ConfigCenterConfigur private ConfigConverter configConverter; - AddressManager kieAddressManager; - @Override public int getOrder() { return ORDER_BASE * 2; @@ -89,7 +87,7 @@ public boolean isValidSource(Configuration localConfiguration) { public void init(Configuration localConfiguration) { configConverter = new ConfigConverter(ConfigCenterConfig.INSTANCE.getFileSources()); - kieAddressManager = configKieAddressManager(); + AddressManager kieAddressManager = configKieAddressManager(); HttpTransport httpTransport = createHttpTransport(kieAddressManager, HttpTransportFactory.defaultRequestConfig().build(), diff --git a/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/ConfigCenterConfigurationSourceImplTest.java b/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/ConfigCenterConfigurationSourceImplTest.java new file mode 100644 index 00000000000..c53d41d5afc --- /dev/null +++ b/dynamic-config/config-cc/src/test/java/org/apache/servicecomb/config/ConfigCenterConfigurationSourceImplTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.config; + + +import java.util.ArrayList; +import java.util.List; + +import org.apache.servicecomb.config.center.client.AddressManager; +import org.apache.servicecomb.foundation.common.event.EventManager; +import org.junit.Assert; +import org.junit.jupiter.api.Test; + +class ConfigCenterConfigurationSourceImplTest { + + @Test + void configKieAddressManagerTest() { + List addresses = new ArrayList<>(); + addresses.add("http://127.0.0.1:30103"); + addresses.add("http://127.0.0.2:30103"); + AddressManager addressManager = new AddressManager("test", addresses, EventManager.getEventBus()); + Assert.assertNotNull(addressManager); + + String address = addressManager.address(); + Assert.assertEquals("http://127.0.0.2:30103/v3/test",address); + address = addressManager.address(); + Assert.assertEquals("http://127.0.0.1:30103/v3/test",address); + + addressManager = new AddressManager(null, addresses, EventManager.getEventBus()); + address = addressManager.address(); + Assert.assertEquals("http://127.0.0.2:30103/v3/default",address); + } +} \ No newline at end of file diff --git a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieConfigurationSourceImpl.java b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieConfigurationSourceImpl.java index 23ba17f27cb..183bd6a4376 100644 --- a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieConfigurationSourceImpl.java +++ b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieConfigurationSourceImpl.java @@ -64,8 +64,6 @@ public class KieConfigurationSourceImpl implements ConfigCenterConfigurationSour private ConfigConverter configConverter; - KieAddressManager kieAddressManager; - @Override public int getOrder() { return ORDER_BASE * 2; @@ -85,7 +83,7 @@ public boolean isValidSource(Configuration localConfiguration) { @Override public void init(Configuration localConfiguration) { configConverter = new ConfigConverter(KieConfig.INSTANCE.getFileSources()); - kieAddressManager = configKieAddressManager(); + KieAddressManager kieAddressManager = configKieAddressManager(); RequestConfig.Builder requestBuilder = HttpTransportFactory.defaultRequestConfig(); if (KieConfig.INSTANCE.enableLongPolling() diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/registry/MicroserviceInstance.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/registry/MicroserviceInstance.java index 3f1b54c52b2..47fbd962459 100644 --- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/registry/MicroserviceInstance.java +++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/registry/MicroserviceInstance.java @@ -212,18 +212,15 @@ public static MicroserviceInstance createFromDefinition(Configuration configurat } private static void loadDataCenterInfo(MicroserviceInstance microserviceInstance) { - String availableZone = DynamicPropertyFactory.getInstance() - .getStringProperty("servicecomb.datacenter.availableZone", null) + String dataCenterName = DynamicPropertyFactory.getInstance() + .getStringProperty("servicecomb.datacenter.name", "default") .get(); - if (StringUtils.isNotEmpty(availableZone)) { - DataCenterInfo dataCenterInfo = new DataCenterInfo(); - dataCenterInfo.setAvailableZone(availableZone); - dataCenterInfo - .setRegion( - DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.datacenter.region", "default").get()); - dataCenterInfo.setName( - DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.datacenter.name", "default").get()); - microserviceInstance.setDataCenterInfo(dataCenterInfo); - } + DataCenterInfo dataCenterInfo = new DataCenterInfo(); + dataCenterInfo.setName(dataCenterName); + dataCenterInfo.setRegion(DynamicPropertyFactory.getInstance(). + getStringProperty("servicecomb.datacenter.region", "default").get()); + dataCenterInfo.setAvailableZone(DynamicPropertyFactory.getInstance(). + getStringProperty("servicecomb.datacenter.availableZone", "default").get()); + microserviceInstance.setDataCenterInfo(dataCenterInfo); } } diff --git a/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/AddressManager.java b/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/AddressManager.java index c7500e1aa70..d2e3eea7cd3 100644 --- a/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/AddressManager.java +++ b/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/AddressManager.java @@ -36,6 +36,6 @@ public class AddressManager extends AbstractAddressManager { @Subscribe public void onRefreshEndpointEvent(RefreshEndpointEvent event) { - refreshEndpoint(event, "CseMonitoring"); + refreshEndpoint(event, RefreshEndpointEvent.CSE_MONITORING_NAME); } } diff --git a/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/DefaultMonitorDataPublisher.java b/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/DefaultMonitorDataPublisher.java index 60d77866317..5846a41dca3 100644 --- a/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/DefaultMonitorDataPublisher.java +++ b/huawei-cloud/dashboard/src/main/java/org/apache/servicecomb/huaweicloud/dashboard/monitor/DefaultMonitorDataPublisher.java @@ -17,6 +17,7 @@ package org.apache.servicecomb.huaweicloud.dashboard.monitor; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -70,7 +71,7 @@ public class DefaultMonitorDataPublisher implements MonitorDataPublisher { @Override public void init() { try { - List addresses = null; + List addresses = new ArrayList<>(); SystemBootstrapInfo info = Deployment.getSystemBootStrapInfo( MonitorConstant.SYSTEM_KEY_DASHBOARD_SERVICE); if (info != null && info.getAccessURL() != null) { diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/AddressManger.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/AddressManger.java index 4b90efa95a8..ec71f3b2765 100644 --- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/AddressManger.java +++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/refresh/AddressManger.java @@ -123,7 +123,7 @@ public void onRefreshEndpointEvent(RefreshEndpointEvent event) { } public void refreshEndpoint(RefreshEndpointEvent event, String key) { - if (null == event || event.getName() != key) { + if (null == event || !event.getName().equals(key)) { return; } availableZone = event.getSameZone();