Skip to content

Commit

Permalink
#modify reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
develpoerX committed Jan 24, 2022
1 parent 5341b5a commit f2b7763
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
public class AddressManager extends AbstractAddressManager {

public AddressManager(String projectName, List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsReques
Map<String, Object> 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<String, String> headers = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class KieClient implements KieConfigOperation {

protected String revision = "0";

private String currentAddress = "";

private final KieAddressManager addressManager;

private final KieConfiguration kieConfiguration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public KieAddressManager(List<String> addresses, EventBus eventBus) {

@Subscribe
public void onRefreshEndpointEvent(RefreshEndpointEvent event) {
refreshEndpoint(event, "KIE");
refreshEndpoint(event, RefreshEndpointEvent.KIE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> availableZone = new ArrayList<>();

private volatile List<String> availableRegion = new ArrayList<>();
Expand All @@ -59,9 +61,8 @@ public AbstractAddressManager(List<String> addresses) {
this.addresses.addAll(addresses);
}

public AbstractAddressManager(String projectName, List<String> addresses, String key) {
public AbstractAddressManager(String projectName, List<String> addresses) {
this.projectName = StringUtils.isEmpty(projectName) ? DEFAULT_PROJECT : projectName;
this.key = key;
this.addresses = this.transformAddress(addresses);
}

Expand All @@ -78,16 +79,17 @@ public boolean sslEnabled() {
return address().startsWith("https://");
}

private List<String> transformAddress(List<String> addresses) {
if (key == "/v3/") {
return addresses.stream().map(this::formatAddress).collect(Collectors.toList());
}
return addresses;
protected List<String> transformAddress(List<String> 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");
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@

public class AddressManager extends AbstractAddressManager {
public AddressManager(String projectName, List<String> addresses, EventBus eventBus) {
super(projectName, addresses, "/v4/");
super(projectName, addresses);
eventBus.register(this);
}

@Override
protected List<String> transformAddress(List<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ public class ConfigCenterConfigurationSourceImpl implements ConfigCenterConfigur

private ConfigConverter configConverter;

AddressManager kieAddressManager;

@Override
public int getOrder() {
return ORDER_BASE * 2;
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public class KieConfigurationSourceImpl implements ConfigCenterConfigurationSour

private ConfigConverter configConverter;

KieAddressManager kieAddressManager;

@Override
public int getOrder() {
return ORDER_BASE * 2;
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public class AddressManager extends AbstractAddressManager {

@Subscribe
public void onRefreshEndpointEvent(RefreshEndpointEvent event) {
refreshEndpoint(event, "CseMonitoring");
refreshEndpoint(event, RefreshEndpointEvent.CSE_MONITORING_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.servicecomb.huaweicloud.dashboard.monitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -70,7 +71,7 @@ public class DefaultMonitorDataPublisher implements MonitorDataPublisher {
@Override
public void init() {
try {
List<String> addresses = null;
List<String> addresses = new ArrayList<>();
SystemBootstrapInfo info = Deployment.getSystemBootStrapInfo(
MonitorConstant.SYSTEM_KEY_DASHBOARD_SERVICE);
if (info != null && info.getAccessURL() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f2b7763

Please sign in to comment.