This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 4ea79fa2f [ISSUE #4842] fix shenyu admin cannot configure consule port
(#4843)
4ea79fa2f is described below
commit 4ea79fa2f6401e401b65412dc63c2978f193ac00
Author: xuziyang <[email protected]>
AuthorDate: Fri Jul 14 11:29:55 2023 +0800
[ISSUE #4842] fix shenyu admin cannot configure consule port (#4843)
---
.../shenyu/admin/config/ConsulServerConfiguration.java | 12 +++++++++++-
.../admin/config/ConsulServerConfigurationTest.java | 1 +
.../consul/ConsulClientServerRegisterRepository.java | 18 +++++++++++++++++-
.../consul/ConsulServerRegisterRepositoryTest.java | 5 +++--
4 files changed, 32 insertions(+), 4 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ConsulServerConfiguration.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ConsulServerConfiguration.java
index ad234ed3d..95a815a06 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ConsulServerConfiguration.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/ConsulServerConfiguration.java
@@ -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;
@@ -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]));
}
/**
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/ConsulServerConfigurationTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/ConsulServerConfigurationTest.java
index 5d77ebc86..211ae65e9 100644
---
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/ConsulServerConfigurationTest.java
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/ConsulServerConfigurationTest.java
@@ -57,6 +57,7 @@ public final class ConsulServerConfigurationTest {
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);
}
diff --git
a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ConsulClientServerRegisterRepository.java
b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ConsulClientServerRegisterRepository.java
index 33829651d..8051dc133 100644
---
a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ConsulClientServerRegisterRepository.java
+++
b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/main/java/org/apache/shenyu/register/client/server/consul/ConsulClientServerRegisterRepository.java
@@ -24,7 +24,9 @@ import com.ecwid.consul.v1.kv.model.GetValue;
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;
@@ -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]));
}
/**
@@ -98,6 +106,14 @@ public class ConsulClientServerRegisterRepository
implements ShenyuClientServerR
});
}
+ 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)));
}
diff --git
a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/test/java/org/apache/shenyu/register/client/server/consul/ConsulServerRegisterRepositoryTest.java
b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/test/java/org/apache/shenyu/register/client/server/consul/ConsulServerRegisterRepositoryTest.java
index bf90802f1..ee39a0be0 100644
---
a/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/test/java/org/apache/shenyu/register/client/server/consul/ConsulServerRegisterRepositoryTest.java
+++
b/shenyu-register-center/shenyu-register-client-server/shenyu-register-client-server-consul/src/test/java/org/apache/shenyu/register/client/server/consul/ConsulServerRegisterRepositoryTest.java
@@ -107,7 +107,7 @@ public class ConsulServerRegisterRepositoryTest {
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)));
@@ -125,7 +125,8 @@ public class ConsulServerRegisterRepositoryTest {
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();
}