feat:add zero protection.

pull/1278/head
Haotian Zhang 1 year ago
parent 8159888db2
commit 7d5b931ba8

@ -11,4 +11,5 @@
- [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1273) - [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1273)
- [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1275) - [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1275)
- [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1276) - [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1276)
- [feat:add zero protection.](https://github.com/Tencent/spring-cloud-tencent/pull/1278)
- [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1281) - [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1281)

@ -63,6 +63,9 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier
@Override @Override
public void modify(ConfigurationImpl configuration) { public void modify(ConfigurationImpl configuration) {
if (!polarisConfigProperties.isEnabled()) {
return;
}
if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) { if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) {
initByPolarisDataSource(configuration); initByPolarisDataSource(configuration);
} }

@ -78,6 +78,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
@Override @Override
public PropertySource<?> locate(Environment environment) { public PropertySource<?> locate(Environment environment) {
if (polarisConfigProperties.isEnabled()) {
CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME); CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
try { try {
// load custom config extension files // load custom config extension files
@ -96,6 +97,8 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
afterLocatePolarisConfigExtension(compositePropertySource); afterLocatePolarisConfigExtension(compositePropertySource);
} }
} }
return null;
}
private void initCustomPolarisConfigExtensionFiles(CompositePropertySource compositePropertySource) { private void initCustomPolarisConfigExtensionFiles(CompositePropertySource compositePropertySource) {
if (polarisConfigCustomExtensionLayer == null) { if (polarisConfigCustomExtensionLayer == null) {

@ -89,6 +89,7 @@ public class PolarisConfigFileLocatorTest {
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yaml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yaml")).thenReturn(emptyConfigFile);
when(polarisConfigProperties.isEnabled()).thenReturn(true);
when(polarisConfigProperties.getGroups()).thenReturn(null); when(polarisConfigProperties.getGroups()).thenReturn(null);
when(environment.getActiveProfiles()).thenReturn(new String[] {}); when(environment.getActiveProfiles()).thenReturn(new String[] {});
@ -136,6 +137,7 @@ public class PolarisConfigFileLocatorTest {
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yaml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yaml")).thenReturn(emptyConfigFile);
when(polarisConfigProperties.isEnabled()).thenReturn(true);
when(polarisConfigProperties.getGroups()).thenReturn(null); when(polarisConfigProperties.getGroups()).thenReturn(null);
when(environment.getActiveProfiles()).thenReturn(new String[] {"dev"}); when(environment.getActiveProfiles()).thenReturn(new String[] {"dev"});
@ -173,6 +175,7 @@ public class PolarisConfigFileLocatorTest {
configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2)); configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2));
customFiles.add(configFileGroup); customFiles.add(configFileGroup);
when(polarisConfigProperties.isEnabled()).thenReturn(true);
when(polarisConfigProperties.getGroups()).thenReturn(customFiles); when(polarisConfigProperties.getGroups()).thenReturn(customFiles);
when(environment.getActiveProfiles()).thenReturn(new String[] {}); when(environment.getActiveProfiles()).thenReturn(new String[] {});

@ -129,7 +129,7 @@ public class PolarisContractReporter implements ApplicationListener<ApplicationR
InterfaceDescriptor interfaceDescriptor = new InterfaceDescriptor(); InterfaceDescriptor interfaceDescriptor = new InterfaceDescriptor();
interfaceDescriptor.setPath(p.getKey()); interfaceDescriptor.setPath(p.getKey());
interfaceDescriptor.setMethod(o.getKey()); interfaceDescriptor.setMethod(o.getKey());
interfaceDescriptor.setContent(JacksonUtils.serialize2Json(p.getValue())); interfaceDescriptor.setContent(JacksonUtils.serialize2Json(o.getValue()));
interfaceDescriptorList.add(interfaceDescriptor); interfaceDescriptorList.add(interfaceDescriptor);
} }
} }

@ -51,6 +51,10 @@ public class DiscoveryConfigModifier implements PolarisConfigModifier {
// Set ServiceRefreshInterval // Set ServiceRefreshInterval
configuration.getConsumer().getLocalCache() configuration.getConsumer().getLocalCache()
.setServiceListRefreshInterval(polarisDiscoveryProperties.getServiceListRefreshInterval()); .setServiceListRefreshInterval(polarisDiscoveryProperties.getServiceListRefreshInterval());
configuration.getConsumer().getZeroProtection().setEnable(polarisDiscoveryProperties.isZeroProtectionEnabled());
configuration.getConsumer().getZeroProtection()
.setNeedTestConnectivity(polarisDiscoveryProperties.isZeroProtectionNeedTestConnectivity());
} }
@Override @Override

@ -101,6 +101,18 @@ public class PolarisDiscoveryProperties {
*/ */
private Long serviceListRefreshInterval = 60000L; private Long serviceListRefreshInterval = 60000L;
/**
* Zero protection switch.
*/
@Value("${spring.cloud.polaris.discovery.zero-protection.enabled:false}")
private boolean zeroProtectionEnabled = false;
/**
* Zero protection test connectivity switch.
*/
@Value("${spring.cloud.polaris.discovery.zero-protection.is-need-test-connectivity:false}")
private boolean zeroProtectionNeedTestConnectivity = false;
public String getInstanceId() { public String getInstanceId() {
return instanceId; return instanceId;
} }
@ -192,6 +204,22 @@ public class PolarisDiscoveryProperties {
this.heartbeatInterval = heartbeatInterval; this.heartbeatInterval = heartbeatInterval;
} }
public boolean isZeroProtectionEnabled() {
return zeroProtectionEnabled;
}
public void setZeroProtectionEnabled(boolean zeroProtectionEnabled) {
this.zeroProtectionEnabled = zeroProtectionEnabled;
}
public boolean isZeroProtectionNeedTestConnectivity() {
return zeroProtectionNeedTestConnectivity;
}
public void setZeroProtectionNeedTestConnectivity(boolean zeroProtectionNeedTestConnectivity) {
this.zeroProtectionNeedTestConnectivity = zeroProtectionNeedTestConnectivity;
}
public Boolean getEnabled() { public Boolean getEnabled() {
return enabled; return enabled;
} }
@ -227,6 +255,8 @@ public class PolarisDiscoveryProperties {
", heartbeatInterval=" + heartbeatInterval + ", heartbeatInterval=" + heartbeatInterval +
", healthCheckUrl='" + healthCheckUrl + '\'' + ", healthCheckUrl='" + healthCheckUrl + '\'' +
", serviceListRefreshInterval=" + serviceListRefreshInterval + ", serviceListRefreshInterval=" + serviceListRefreshInterval +
", zeroProtectionEnabled=" + zeroProtectionEnabled +
", zeroProtectionNeedTestConnectivity=" + zeroProtectionNeedTestConnectivity +
'}'; '}';
} }
} }

@ -90,6 +90,9 @@ public class ConsulConfigModifier implements PolarisConfigModifier {
if (StringUtils.isNotBlank(consulContextProperties.getInstanceId())) { if (StringUtils.isNotBlank(consulContextProperties.getInstanceId())) {
metadata.put(ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY, consulContextProperties.getInstanceId()); metadata.put(ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY, consulContextProperties.getInstanceId());
} }
if (StringUtils.isNotBlank(consulContextProperties.getAclToken())) {
serverConnectorConfig.setToken(consulContextProperties.getAclToken());
}
if (consulContextProperties.isPreferIpAddress() if (consulContextProperties.isPreferIpAddress()
&& StringUtils.isNotBlank(consulContextProperties.getIpAddress())) { && StringUtils.isNotBlank(consulContextProperties.getIpAddress())) {
metadata.put(ConsulConstant.MetadataMapKey.PREFER_IP_ADDRESS_KEY, metadata.put(ConsulConstant.MetadataMapKey.PREFER_IP_ADDRESS_KEY,

@ -55,6 +55,9 @@ public class ConsulContextProperties {
@Value("${spring.cloud.consul.discovery.prefer-ip-address:#{'false'}}") @Value("${spring.cloud.consul.discovery.prefer-ip-address:#{'false'}}")
private boolean preferIpAddress; private boolean preferIpAddress;
@Value("${consul.token:${CONSUL_TOKEN:${spring.cloud.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:${spring.cloud.consul.discovery.acl-token:}}}}}")
private String aclToken;
public String getHost() { public String getHost() {
return host; return host;
} }
@ -83,14 +86,14 @@ public class ConsulContextProperties {
return register; return register;
} }
public boolean isDiscoveryEnabled() {
return discoveryEnabled;
}
public void setRegister(boolean register) { public void setRegister(boolean register) {
this.register = register; this.register = register;
} }
public boolean isDiscoveryEnabled() {
return discoveryEnabled;
}
public void setDiscoveryEnabled(boolean discoveryEnabled) { public void setDiscoveryEnabled(boolean discoveryEnabled) {
this.discoveryEnabled = discoveryEnabled; this.discoveryEnabled = discoveryEnabled;
} }
@ -126,4 +129,12 @@ public class ConsulContextProperties {
public void setPreferIpAddress(boolean preferIpAddress) { public void setPreferIpAddress(boolean preferIpAddress) {
this.preferIpAddress = preferIpAddress; this.preferIpAddress = preferIpAddress;
} }
public String getAclToken() {
return aclToken;
}
public void setAclToken(String aclToken) {
this.aclToken = aclToken;
}
} }

@ -67,16 +67,16 @@
"description": "Millis interval of refresh of service info list. Default: 60000." "description": "Millis interval of refresh of service info list. Default: 60000."
}, },
{ {
"name": "spring.cloud.polaris.loadbalancer.enabled", "name": "spring.cloud.polaris.discovery.zero-protection.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
"defaultValue": "true", "defaultValue": false,
"description": "polaris loadbalancer." "description": "Zero protection switch. Default: false."
}, },
{ {
"name": "spring.cloud.polaris.loadbalancer.strategy", "name": "spring.cloud.polaris.discovery.zero-protection.is-need-test-connectivity",
"type": "java.lang.String", "type": "java.lang.Boolean",
"defaultValue": "roundRobin", "defaultValue": false,
"description": "loadbalancer strategy." "description": "Zero protection test connectivity switch. Default: false."
}, },
{ {
"name": "spring.cloud.nacos.discovery.enabled", "name": "spring.cloud.nacos.discovery.enabled",

@ -101,6 +101,8 @@ public class PolarisDiscoveryPropertiesTest {
+ ", registerEnabled=true" + ", registerEnabled=true"
+ ", heartbeatInterval=20" + ", heartbeatInterval=20"
+ ", healthCheckUrl='/health'" + ", healthCheckUrl='/health'"
+ ", serviceListRefreshInterval=1000}"); + ", serviceListRefreshInterval=1000"
+ ", zeroProtectionEnabled=false"
+ ", zeroProtectionNeedTestConnectivity=false}");
} }
} }

@ -125,7 +125,7 @@ public class OrderConstant {
/** /**
* Address modifier order. * Address modifier order.
*/ */
public static Integer ADDRESS_ORDER = Integer.MIN_VALUE; public static Integer ADDRESS_ORDER = Integer.MIN_VALUE + 10;
/** /**
* Discovery config modifier order. * Discovery config modifier order.

@ -16,6 +16,9 @@ spring:
heartbeat: heartbeat:
enabled: true enabled: true
health-check-url: /quickstart/caller/healthCheck health-check-url: /quickstart/caller/healthCheck
zero-protection:
enabled: true
is-need-test-connectivity: true
contract: contract:
exposure: true exposure: true
report: report:

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.context; package com.tencent.cloud.polaris.context;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.common.constant.OrderConstant;
@ -26,6 +27,8 @@ import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
/** /**
* Modify polaris server address. * Modify polaris server address.
* *
@ -48,6 +51,13 @@ public class ModifyAddress implements PolarisConfigModifier {
List<String> addresses = AddressUtils.parseAddressList(properties.getAddress()); List<String> addresses = AddressUtils.parseAddressList(properties.getAddress());
configuration.getGlobal().getServerConnector().setAddresses(addresses); configuration.getGlobal().getServerConnector().setAddresses(addresses);
if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) {
configuration.getGlobal().setServerConnectors(new ArrayList<>());
}
if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())
&& null != configuration.getGlobal().getServerConnector()) {
configuration.getGlobal().getServerConnectors().add(configuration.getGlobal().getServerConnector());
}
} }
@Override @Override

Loading…
Cancel
Save