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)
- [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)
- [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)

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

@ -78,23 +78,26 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
try {
// load custom config extension files
initCustomPolarisConfigExtensionFiles(compositePropertySource);
// load spring boot default config files
initInternalConfigFiles(compositePropertySource);
// load custom config files
List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups();
if (CollectionUtils.isEmpty(configFileGroups)) {
if (polarisConfigProperties.isEnabled()) {
CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
try {
// load custom config extension files
initCustomPolarisConfigExtensionFiles(compositePropertySource);
// load spring boot default config files
initInternalConfigFiles(compositePropertySource);
// load custom config files
List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups();
if (CollectionUtils.isEmpty(configFileGroups)) {
return compositePropertySource;
}
initCustomPolarisConfigFiles(compositePropertySource, configFileGroups);
return compositePropertySource;
}
initCustomPolarisConfigFiles(compositePropertySource, configFileGroups);
return compositePropertySource;
}
finally {
afterLocatePolarisConfigExtension(compositePropertySource);
finally {
afterLocatePolarisConfigExtension(compositePropertySource);
}
}
return null;
}
private void initCustomPolarisConfigExtensionFiles(CompositePropertySource compositePropertySource) {

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

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

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

@ -101,6 +101,18 @@ public class PolarisDiscoveryProperties {
*/
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() {
return instanceId;
}
@ -192,6 +204,22 @@ public class PolarisDiscoveryProperties {
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() {
return enabled;
}
@ -227,6 +255,8 @@ public class PolarisDiscoveryProperties {
", heartbeatInterval=" + heartbeatInterval +
", healthCheckUrl='" + healthCheckUrl + '\'' +
", serviceListRefreshInterval=" + serviceListRefreshInterval +
", zeroProtectionEnabled=" + zeroProtectionEnabled +
", zeroProtectionNeedTestConnectivity=" + zeroProtectionNeedTestConnectivity +
'}';
}
}

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

@ -55,6 +55,9 @@ public class ConsulContextProperties {
@Value("${spring.cloud.consul.discovery.prefer-ip-address:#{'false'}}")
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() {
return host;
}
@ -83,14 +86,14 @@ public class ConsulContextProperties {
return register;
}
public boolean isDiscoveryEnabled() {
return discoveryEnabled;
}
public void setRegister(boolean register) {
this.register = register;
}
public boolean isDiscoveryEnabled() {
return discoveryEnabled;
}
public void setDiscoveryEnabled(boolean discoveryEnabled) {
this.discoveryEnabled = discoveryEnabled;
}
@ -126,4 +129,12 @@ public class ConsulContextProperties {
public void setPreferIpAddress(boolean 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."
},
{
"name": "spring.cloud.polaris.loadbalancer.enabled",
"name": "spring.cloud.polaris.discovery.zero-protection.enabled",
"type": "java.lang.Boolean",
"defaultValue": "true",
"description": "polaris loadbalancer."
"defaultValue": false,
"description": "Zero protection switch. Default: false."
},
{
"name": "spring.cloud.polaris.loadbalancer.strategy",
"type": "java.lang.String",
"defaultValue": "roundRobin",
"description": "loadbalancer strategy."
"name": "spring.cloud.polaris.discovery.zero-protection.is-need-test-connectivity",
"type": "java.lang.Boolean",
"defaultValue": false,
"description": "Zero protection test connectivity switch. Default: false."
},
{
"name": "spring.cloud.nacos.discovery.enabled",

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

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

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

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.context;
import java.util.ArrayList;
import java.util.List;
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 org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
/**
* Modify polaris server address.
*
@ -48,6 +51,13 @@ public class ModifyAddress implements PolarisConfigModifier {
List<String> addresses = AddressUtils.parseAddressList(properties.getAddress());
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

Loading…
Cancel
Save