Skip to content

Commit

Permalink
Merge branch 'master' into feature_api_doc_annotaion_generated
Browse files Browse the repository at this point in the history
  • Loading branch information
yu199195 committed Jul 14, 2023
2 parents 7f7274a + 4ea79fa commit 3507ed6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.apache.shenyu.admin.config;

import com.ecwid.consul.v1.ConsulClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
import org.apache.shenyu.register.client.server.consul.ShenyuConsulConfigWatch;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -36,7 +38,15 @@ public class ConsulServerConfiguration {
*/
@Bean(name = "registerConsulClient")
public ConsulClient consulClient(final ShenyuRegisterCenterConfig config) {
return new ConsulClient(config.getServerLists());
final String serverList = config.getServerLists();
if (StringUtils.isBlank(serverList)) {
throw new ShenyuException("serverList can not be null.");
}
final String[] addresses = serverList.split(":");
if (addresses.length != 2) {
throw new ShenyuException("serverList formatter is not incorrect.");
}
return new ConsulClient(addresses[0], Integer.parseInt(addresses[1]));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void consulClientTest() {
Properties properties = mock(Properties.class);
when(config.getProps()).thenReturn(properties);
when(config.getProps().getProperty(any(), any())).thenReturn("1", "30", "mocked valued");
when(config.getServerLists()).thenReturn("127.0.0.1:8500");
ConsulClient consulClient = configuration.consulClient(config);
assertNotNull(consulClient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.google.common.collect.Lists;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.client.server.api.ShenyuClientServerRegisterRepository;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
Expand Down Expand Up @@ -64,7 +66,13 @@ public class ConsulClientServerRegisterRepository implements ShenyuClientServerR
public void init(final ShenyuClientServerRegisterPublisher publisher,
final ShenyuRegisterCenterConfig config) {
this.publisher = publisher;
consulClient = new ConsulClient(config.getServerLists());

final String serverList = config.getServerLists();
if (StringUtils.isBlank(serverList)) {
throw new ShenyuException("serverList can not be null.");
}
final String[] addresses = splitAndCheckAddress(serverList);
consulClient = new ConsulClient(addresses[0], Integer.parseInt(addresses[1]));
}

/**
Expand Down Expand Up @@ -98,6 +106,14 @@ public void onMetadataChange(final ConsulConfigChangedEvent event) {
});
}

private String[] splitAndCheckAddress(final String serverList) {
final String[] addresses = serverList.split(":");
if (addresses.length != 2) {
throw new ShenyuException("serverList formatter is not incorrect.");
}
return addresses;
}

private void publishMetadata(final String data) {
publisher.publish(Lists.newArrayList(GsonUtils.getInstance().fromJson(data, MetaDataRegisterDTO.class)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testConsulServerRegisterRepository() {
new ApplicationContextRunner().withUserConfiguration(ConsulServerRegisterRepositoryTest.class)
.run(context -> {
MetaDataRegisterDTO mockServer = MetaDataRegisterDTO.builder().appName("mockServer").contextPath("/mock")
.host("127.0.0.1").rpcType(RpcTypeEnum.DUBBO.getName()).build();
.host("127.0.0.1:8500").rpcType(RpcTypeEnum.DUBBO.getName()).build();
Map<String, GetValue> mateData = new HashMap<>();
GetValue getValue = new GetValue();
getValue.setValue(Base64.getEncoder().encodeToString(GsonUtils.getInstance().toJson(mockServer).getBytes(StandardCharsets.UTF_8)));
Expand All @@ -125,7 +125,8 @@ public void initTest() {
final MockedConstruction<ConsulClient> consulClientMockedConstruction = mockConstruction(ConsulClient.class);
ConsulClientServerRegisterRepository consulServerRegisterRepository = new ConsulClientServerRegisterRepository();
final ShenyuClientServerRegisterPublisher shenyuClientServerRegisterPublisher = mock(ShenyuClientServerRegisterPublisher.class);
final ShenyuRegisterCenterConfig shenyuRegisterCenterConfig = mock(ShenyuRegisterCenterConfig.class);
ShenyuRegisterCenterConfig shenyuRegisterCenterConfig = new ShenyuRegisterCenterConfig();
shenyuRegisterCenterConfig.setServerLists("127.0.0.1:8500");
consulServerRegisterRepository.init(shenyuClientServerRegisterPublisher, shenyuRegisterCenterConfig);
consulClientMockedConstruction.close();
}
Expand Down

0 comments on commit 3507ed6

Please sign in to comment.