feat: support multi ratelimit

pull/1675/head
fishtailfu 1 month ago
parent 290f0c12ae
commit 55ad3388e9

@ -67,6 +67,18 @@ public final class AddressUtils {
return addressList; return addressList;
} }
public static List<String> parseHostPortList(String addressInfo) {
if (StringUtils.isBlank(addressInfo)) {
return Collections.emptyList();
}
String[] addresses = addressInfo.split(ADDRESS_SEPARATOR);
List<String> addressList = new ArrayList<>();
for (String address : addresses) {
addressList.add(address.trim());
}
return addressList;
}
public static boolean accessible(String ip, int port, int timeout) { public static boolean accessible(String ip, int port, int timeout) {
Socket socket = new Socket(); Socket socket = new Socket();
try { try {

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.context.event; package com.tencent.cloud.polaris.context.event;
import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.common.util.AddressUtils;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.config.plugin.DefaultPlugins; import com.tencent.polaris.api.config.plugin.DefaultPlugins;
import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.ConfigurationImpl;
@ -49,7 +50,7 @@ public class PushGatewayEventReporterConfigModifier implements PolarisConfigModi
else { else {
pushGatewayEventReporterConfig.setEnable(true); pushGatewayEventReporterConfig.setEnable(true);
} }
pushGatewayEventReporterConfig.setAddress(properties.getAddress()); pushGatewayEventReporterConfig.setAddress(AddressUtils.parseHostPortList(properties.getAddress()));
pushGatewayEventReporterConfig.setEventQueueSize(properties.getEventQueueSize()); pushGatewayEventReporterConfig.setEventQueueSize(properties.getEventQueueSize());
pushGatewayEventReporterConfig.setMaxBatchSize(properties.getMaxBatchSize()); pushGatewayEventReporterConfig.setMaxBatchSize(properties.getMaxBatchSize());
pushGatewayEventReporterConfig.setNamespace(properties.getNamespace()); pushGatewayEventReporterConfig.setNamespace(properties.getNamespace());

@ -35,9 +35,9 @@ public class PushGatewayEventReporterProperties {
private boolean enabled = false; 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<String> address; private String address;
/** /**
* Queue size for push gateway event queue. Default is 1000. * Queue size for push gateway event queue. Default is 1000.
@ -67,11 +67,11 @@ public class PushGatewayEventReporterProperties {
this.enabled = enabled; this.enabled = enabled;
} }
public List<String> getAddress() { public String getAddress() {
return address; return address;
} }
public void setAddress(List<String> address) { public void setAddress(String address) {
this.address = address; this.address = address;
} }

@ -17,6 +17,9 @@
package com.tencent.cloud.polaris.context.event; 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.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -53,8 +56,9 @@ public class PushGatewayEventReporterPropertiesTest {
this.applicationContextRunner.run(context -> { this.applicationContextRunner.run(context -> {
PushGatewayEventReporterProperties properties = context.getBean(PushGatewayEventReporterProperties.class); PushGatewayEventReporterProperties properties = context.getBean(PushGatewayEventReporterProperties.class);
assertThat(properties.isEnabled()).isTrue(); assertThat(properties.isEnabled()).isTrue();
assertThat(properties.getAddress().get(0)).isEqualTo("1.2.3.4:9091"); List<String> addresses = AddressUtils.parseHostPortList(properties.getAddress());
assertThat(properties.getAddress().get(1)).isEqualTo("1.2.3.5:9091"); 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.getEventQueueSize()).isEqualTo(123);
assertThat(properties.getMaxBatchSize()).isEqualTo(456); assertThat(properties.getMaxBatchSize()).isEqualTo(456);
assertThat(properties.getNamespace()).isEqualTo("test-namespace"); assertThat(properties.getNamespace()).isEqualTo("test-namespace");

@ -51,7 +51,7 @@ public class PolarisStatProperties {
* PushGateway address. * PushGateway address.
*/ */
@Value("${spring.cloud.polaris.stat.pushgateway.address:}") @Value("${spring.cloud.polaris.stat.pushgateway.address:}")
private List<String> pushGatewayAddress; private String pushGatewayAddress;
/** /**
* PushGateway namespace. * PushGateway namespace.
@ -107,11 +107,11 @@ public class PolarisStatProperties {
this.pushGatewayEnabled = pushGatewayEnabled; this.pushGatewayEnabled = pushGatewayEnabled;
} }
public List<String> getPushGatewayAddress() { public String getPushGatewayAddress() {
return pushGatewayAddress; return pushGatewayAddress;
} }
public void setPushGatewayAddress(List<String> pushGatewayAddress) { public void setPushGatewayAddress(String pushGatewayAddress) {
this.pushGatewayAddress = pushGatewayAddress; this.pushGatewayAddress = pushGatewayAddress;
} }

@ -18,6 +18,7 @@
package com.tencent.cloud.rpc.enhancement.stat.config; package com.tencent.cloud.rpc.enhancement.stat.config;
import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.common.util.AddressUtils;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.global.StatReporterConfigImpl; import com.tencent.polaris.factory.config.global.StatReporterConfigImpl;
@ -51,7 +52,7 @@ public class StatConfigModifier implements PolarisConfigModifier {
if (polarisStatProperties.isPushGatewayEnabled()) { if (polarisStatProperties.isPushGatewayEnabled()) {
// push gateway // push gateway
prometheusHandlerConfig.setType("push"); prometheusHandlerConfig.setType("push");
prometheusHandlerConfig.setAddress(polarisStatProperties.getPushGatewayAddress()); prometheusHandlerConfig.setAddress(AddressUtils.parseAddressList(polarisStatProperties.getPushGatewayAddress()));
prometheusHandlerConfig.setNamespace(polarisStatProperties.getStatNamespace()); prometheusHandlerConfig.setNamespace(polarisStatProperties.getStatNamespace());
prometheusHandlerConfig.setService(polarisStatProperties.getStatService()); prometheusHandlerConfig.setService(polarisStatProperties.getStatService());
prometheusHandlerConfig.setPushInterval(polarisStatProperties.getPushGatewayPushInterval()); prometheusHandlerConfig.setPushInterval(polarisStatProperties.getPushGatewayPushInterval());

@ -19,6 +19,7 @@ package com.tencent.cloud.rpc.enhancement.stat.config;
import java.util.List; import java.util.List;
import com.tencent.cloud.common.util.AddressUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.enabled=true")
.withPropertyValues("spring.cloud.polaris.stat.path=/xxx") .withPropertyValues("spring.cloud.polaris.stat.path=/xxx")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.enabled=true") .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.namespace=test-namespace")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.service=test-service") .withPropertyValues("spring.cloud.polaris.stat.pushgateway.service=test-service")
.withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000") .withPropertyValues("spring.cloud.polaris.stat.pushgateway.push-interval=1000")
@ -54,8 +55,9 @@ public class PolarisStatPropertiesTest {
assertThat(polarisStatProperties.isEnabled()).isTrue(); assertThat(polarisStatProperties.isEnabled()).isTrue();
assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx"); assertThat(polarisStatProperties.getPath()).isEqualTo("/xxx");
assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue(); assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue();
assertThat(polarisStatProperties.getPushGatewayAddress().get(0)).isEqualTo("127.0.0.1:9091"); List<String> addresses = AddressUtils.parseHostPortList(polarisStatProperties.getPushGatewayAddress());
assertThat(polarisStatProperties.getPushGatewayAddress().get(1)).isEqualTo("127.0.0.1:9092"); 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.getStatNamespace()).isEqualTo("test-namespace");
assertThat(polarisStatProperties.getStatService()).isEqualTo("test-service"); assertThat(polarisStatProperties.getStatService()).isEqualTo("test-service");
assertThat(polarisStatProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000"); assertThat(polarisStatProperties.getPushGatewayPushInterval().toString()).isEqualTo("1000");
@ -71,10 +73,11 @@ public class PolarisStatPropertiesTest {
assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue(); assertThat(polarisStatProperties.isPushGatewayEnabled()).isTrue();
// PushGatewayAddress // PushGatewayAddress
List<String> 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); polarisStatProperties.setPushGatewayAddress(pushGatewayAddress);
assertThat(polarisStatProperties.getPushGatewayAddress().get(0)).isEqualTo("127.0.0.1:9091"); List<String> addresses = AddressUtils.parseAddressList(polarisStatProperties.getPushGatewayAddress());
assertThat(polarisStatProperties.getPushGatewayAddress().get(1)).isEqualTo("127.0.0.1:9092"); assertThat(addresses.get(0)).isEqualTo("127.0.0.1:9091");
assertThat(addresses.get(1)).isEqualTo("127.0.0.1:9092");
// PushGatewayPushInterval // PushGatewayPushInterval
polarisStatProperties.setPushGatewayPushInterval(1000L); polarisStatProperties.setPushGatewayPushInterval(1000L);

Loading…
Cancel
Save