diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java index 8f998bd50..6597eb4a2 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/AddressUtils.java @@ -67,6 +67,18 @@ public final class AddressUtils { return addressList; } + public static List parseHostPortList(String addressInfo) { + if (StringUtils.isBlank(addressInfo)) { + return Collections.emptyList(); + } + String[] addresses = addressInfo.split(ADDRESS_SEPARATOR); + List addressList = new ArrayList<>(); + for (String address : addresses) { + addressList.add(address.trim()); + } + return addressList; + } + public static boolean accessible(String ip, int port, int timeout) { Socket socket = new Socket(); try { diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifier.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifier.java index 662e9b7bb..816da90aa 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifier.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifier.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.context.event; import com.tencent.cloud.common.constant.OrderConstant; +import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.polaris.api.config.plugin.DefaultPlugins; import com.tencent.polaris.factory.config.ConfigurationImpl; @@ -49,7 +50,7 @@ public class PushGatewayEventReporterConfigModifier implements PolarisConfigModi else { pushGatewayEventReporterConfig.setEnable(true); } - pushGatewayEventReporterConfig.setAddress(properties.getAddress()); + pushGatewayEventReporterConfig.setAddress(AddressUtils.parseHostPortList(properties.getAddress())); pushGatewayEventReporterConfig.setEventQueueSize(properties.getEventQueueSize()); pushGatewayEventReporterConfig.setMaxBatchSize(properties.getMaxBatchSize()); pushGatewayEventReporterConfig.setNamespace(properties.getNamespace()); diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterProperties.java index bbc429530..e6db66dd2 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterProperties.java @@ -35,9 +35,9 @@ public class PushGatewayEventReporterProperties { private boolean enabled = false; /** - * Address of pushgateway. For example: 1.2.3.4:9091. + * Address list of pushgateway. For example: 1.2.3.4:9091,1.2.3.4:9092. */ - private List address; + private String address; /** * Queue size for push gateway event queue. Default is 1000. @@ -67,11 +67,11 @@ public class PushGatewayEventReporterProperties { this.enabled = enabled; } - public List getAddress() { + public String getAddress() { return address; } - public void setAddress(List address) { + public void setAddress(String address) { this.address = address; } diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterPropertiesTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterPropertiesTest.java index 473f6e949..f6d7c31ae 100644 --- a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterPropertiesTest.java +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterPropertiesTest.java @@ -17,6 +17,9 @@ package com.tencent.cloud.polaris.context.event; +import java.util.List; + +import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import org.junit.jupiter.api.BeforeEach; @@ -53,8 +56,9 @@ public class PushGatewayEventReporterPropertiesTest { this.applicationContextRunner.run(context -> { PushGatewayEventReporterProperties properties = context.getBean(PushGatewayEventReporterProperties.class); assertThat(properties.isEnabled()).isTrue(); - assertThat(properties.getAddress().get(0)).isEqualTo("1.2.3.4:9091"); - assertThat(properties.getAddress().get(1)).isEqualTo("1.2.3.5:9091"); + List addresses = AddressUtils.parseHostPortList(properties.getAddress()); + assertThat(addresses.get(0)).isEqualTo("1.2.3.4:9091"); + assertThat(addresses.get(1)).isEqualTo("1.2.3.5:9091"); assertThat(properties.getEventQueueSize()).isEqualTo(123); assertThat(properties.getMaxBatchSize()).isEqualTo(456); assertThat(properties.getNamespace()).isEqualTo("test-namespace"); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java index 07eba89a7..b5d17b4a2 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatProperties.java @@ -51,7 +51,7 @@ public class PolarisStatProperties { * PushGateway address. */ @Value("${spring.cloud.polaris.stat.pushgateway.address:}") - private List pushGatewayAddress; + private String pushGatewayAddress; /** * PushGateway namespace. @@ -107,11 +107,11 @@ public class PolarisStatProperties { this.pushGatewayEnabled = pushGatewayEnabled; } - public List getPushGatewayAddress() { + public String getPushGatewayAddress() { return pushGatewayAddress; } - public void setPushGatewayAddress(List pushGatewayAddress) { + public void setPushGatewayAddress(String pushGatewayAddress) { this.pushGatewayAddress = pushGatewayAddress; } diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java index 066e8361e..b1b4450d8 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java @@ -18,6 +18,7 @@ package com.tencent.cloud.rpc.enhancement.stat.config; import com.tencent.cloud.common.constant.OrderConstant; +import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.global.StatReporterConfigImpl; @@ -51,7 +52,7 @@ public class StatConfigModifier implements PolarisConfigModifier { if (polarisStatProperties.isPushGatewayEnabled()) { // push gateway prometheusHandlerConfig.setType("push"); - prometheusHandlerConfig.setAddress(polarisStatProperties.getPushGatewayAddress()); + prometheusHandlerConfig.setAddress(AddressUtils.parseAddressList(polarisStatProperties.getPushGatewayAddress())); prometheusHandlerConfig.setNamespace(polarisStatProperties.getStatNamespace()); prometheusHandlerConfig.setService(polarisStatProperties.getStatService()); prometheusHandlerConfig.setPushInterval(polarisStatProperties.getPushGatewayPushInterval()); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java index cfe09e8f9..f5da28035 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/stat/config/PolarisStatPropertiesTest.java @@ -19,6 +19,7 @@ package com.tencent.cloud.rpc.enhancement.stat.config; import java.util.List; +import com.tencent.cloud.common.util.AddressUtils; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -39,7 +40,7 @@ public class PolarisStatPropertiesTest { .withPropertyValues("spring.cloud.polaris.stat.enabled=true") .withPropertyValues("spring.cloud.polaris.stat.path=/xxx") .withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true") - .withPropertyValues("spring.cloud.polaris.stat.pushgateway.address=127.0.0.1:9091,127.0.0.1:9092") + .withPropertyValues("spring.cloud.polaris.stat.pushgateway.address=127.0.0.1:9091, 127.0.0.1:9092") .withPropertyValues("spring.cloud.polaris.stat.pushgateway.namespace=test-namespace") .withPropertyValues("spring.cloud.polaris.stat.pushgateway.service=test-service") .withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000") @@ -54,8 +55,9 @@ public class PolarisStatPropertiesTest { assertThat(polarisStatProperties.isEnabled()).isTrue(); assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx"); assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue(); - assertThat(polarisStatProperties.getPushGatewayAddress().get(0)).isEqualTo("127.0.0.1:9091"); - assertThat(polarisStatProperties.getPushGatewayAddress().get(1)).isEqualTo("127.0.0.1:9092"); + List addresses = AddressUtils.parseHostPortList(polarisStatProperties.getPushGatewayAddress()); + assertThat(addresses.get(0)).isEqualTo("127.0.0.1:9091"); + assertThat(addresses.get(1)).isEqualTo("127.0.0.1:9092"); assertThat(polarisStatProperties.getStatNamespace()).isEqualTo("test-namespace"); assertThat(polarisStatProperties.getStatService()).isEqualTo("test-service"); assertThat(polarisStatProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000"); @@ -71,10 +73,11 @@ public class PolarisStatPropertiesTest { assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue(); // PushGatewayAddress - List pushGatewayAddress = List.of("127.0.0.1:9091", "127.0.0.1:9092"); + String pushGatewayAddress = "127.0.0.1:9091, " + "127.0.0.1:9092"; polarisStatProperties.setPushGatewayAddress(pushGatewayAddress); - assertThat(polarisStatProperties.getPushGatewayAddress().get(0)).isEqualTo("127.0.0.1:9091"); - assertThat(polarisStatProperties.getPushGatewayAddress().get(1)).isEqualTo("127.0.0.1:9092"); + List addresses = AddressUtils.parseAddressList(polarisStatProperties.getPushGatewayAddress()); + assertThat(addresses.get(0)).isEqualTo("127.0.0.1:9091"); + assertThat(addresses.get(1)).isEqualTo("127.0.0.1:9092"); // PushGatewayPushInterval polarisStatProperties.setPushGatewayPushInterval(1000L);