Merge pull request #42 from SkyeBeFreeman/main

feat:Support multi-discovery server.
pull/51/head
Haotian Zhang 2 years ago committed by GitHub
commit f2c48739f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -71,7 +71,7 @@
<properties>
<!-- Project revision -->
<revision>1.1.4.Hoxton.SR9</revision>
<revision>1.2.0.Hoxton.SR9-SNAPSHOT</revision>
<!-- Spring Cloud -->
<spring.cloud.version>Hoxton.SR9</spring.cloud.version>

@ -62,8 +62,8 @@
</developers>
<properties>
<revision>1.1.4.Hoxton.SR9</revision>
<polaris.version>1.2.4</polaris.version>
<revision>1.2.0.Hoxton.SR9-SNAPSHOT</revision>
<polaris.version>1.3.0-SNAPSHOT</polaris.version>
<tomcat.version>10.0.0-M6</tomcat.version>
<powermock.version>2.0.0</powermock.version>

@ -13,9 +13,7 @@ spring:
enabled: true
discovery:
register: true
health-check-path: /actuator/health
health-check-interval: 10s
instance-id: ${spring.application.name}:${server.port}
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
enabled: true
service-name: ${spring.application.name}
ip-address: localhost

@ -17,6 +17,9 @@
package com.tencent.cloud.polaris.circuitbreaker;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
import com.tencent.cloud.common.constant.ContextConstant.ModifierOrder;
import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignBeanPostProcessor;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
@ -32,8 +35,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
/**
* Configuration for Polaris {@link feign.Feign} which can automatically bring in the call results for reporting
*
@ -71,5 +72,10 @@ public class PolarisFeignClientAutoConfiguration {
//开启熔断配置
configuration.getConsumer().getCircuitBreaker().setEnable(true);
}
@Override
public int getOrder() {
return ModifierOrder.CIRCUIT_BREAKER_ORDER;
}
}
}

@ -0,0 +1,47 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.common.constant;
/**
* Constant for Context.
*
* @author skyehtzhang
*/
public interface ContextConstant {
/**
* Order of configuration modifier.
*/
interface ModifierOrder {
/**
* First modifier order.
*/
Integer FIRST = Integer.MIN_VALUE;
/**
* Last modifier order.
*/
Integer LAST = Integer.MAX_VALUE;
/**
* Order of circuit breaker configuration modifier.
*/
Integer CIRCUIT_BREAKER_ORDER = 1;
}
}

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent-starters</artifactId>
@ -37,6 +37,21 @@
<artifactId>connector-polaris-grpc</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-polaris-grpc</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-consul</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>connector-composite</artifactId>
</dependency>
<!--依赖resCache插件-->
<dependency>
<groupId>com.tencent.polaris</groupId>

@ -20,14 +20,23 @@ package com.tencent.cloud.polaris.context;
import com.tencent.polaris.factory.config.ConfigurationImpl;
/**
* Modifier interface for polaris configuration.
*
* @author Haotian Zhang
*/
public interface PolarisConfigModifier {
/**
*
* Modify configuration.
*
* @param configuration
* @param configuration
*/
void modify(ConfigurationImpl configuration);
/**
* Get modifier order for sorting.
*
* @return order
*/
int getOrder();
}

@ -17,6 +17,8 @@
package com.tencent.cloud.polaris.context;
import com.tencent.cloud.common.constant.ContextConstant.ModifierOrder;
import com.tencent.cloud.polaris.context.extend.consul.ConsulContextProperties;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.config.ConfigurationImpl;
@ -34,7 +36,7 @@ import org.springframework.context.annotation.Bean;
*
* @author Haotian Zhang
*/
@EnableConfigurationProperties(PolarisContextProperties.class)
@EnableConfigurationProperties({PolarisContextProperties.class, ConsulContextProperties.class})
public class PolarisContextConfiguration {
private static final String ADDRESS_SEPARATOR = ",";
@ -63,6 +65,11 @@ public class PolarisContextConfiguration {
}
}
@Override
public int getOrder() {
return ModifierOrder.FIRST;
}
private List<String> getAddressList(String addressInfo) {
List<String> addressList = new ArrayList<>();
String[] addresses = addressInfo.split(ADDRESS_SEPARATOR);
@ -73,5 +80,4 @@ public class PolarisContextConfiguration {
return addressList;
}
}
}

@ -22,7 +22,9 @@ import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -67,6 +69,8 @@ public class PolarisContextProperties {
String defaultHost = getHost();
configuration.getGlobal().getAPI().setBindIP(defaultHost);
Collection<PolarisConfigModifier> modifiers = modifierList;
modifiers = modifiers.stream().sorted(Comparator.comparingInt(PolarisConfigModifier::getOrder))
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(modifiers)) {
for (PolarisConfigModifier modifier : modifiers) {
modifier.modify(configuration);

@ -0,0 +1,126 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.context.extend.consul;
import com.tencent.cloud.common.constant.ContextConstant.ModifierOrder;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.config.plugin.DefaultPlugins;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl;
import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
/**
* Discovery configuration of Consul.
*
* @author Haotian Zhang
*/
@ConditionalOnExpression("'true'.equals('${spring.cloud.consul.enabled:true}')"
+ " && 'true'.equals('${spring.cloud.consul.discovery.enabled:true}')")
@ConfigurationProperties("spring.cloud.consul")
public class ConsulContextProperties {
/**
* Host of consul(or consul agent)
*/
private String host;
private int port;
private boolean enabled;
@Value("${spring.cloud.consul.discovery.register:#{'true'}}")
private boolean register;
@Value("${spring.cloud.consul.discovery.enabled:#{'true'}}")
private boolean discoveryEnabled;
@Value("${spring.cloud.consul.discovery.instance-id:}")
private String instanceId;
@Value("${spring.cloud.consul.discovery.service-name:${spring.application.name:}}")
private String serviceName;
@Value("${spring.cloud.consul.discovery.ip-address:}")
private String ipAddress;
@Value("${spring.cloud.consul.discovery.prefer-ip-address:#{'false'}}")
private boolean preferIpAddress;
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Bean
@ConditionalOnMissingBean
public ConsulConfigModifier consulConfigModifier() {
return new ConsulConfigModifier();
}
private static class ConsulConfigModifier implements PolarisConfigModifier {
@Autowired(required = false)
private ConsulContextProperties consulContextProperties;
@Override
public void modify(ConfigurationImpl configuration) {
if (consulContextProperties != null && consulContextProperties.enabled
&& consulContextProperties.discoveryEnabled && consulContextProperties.register) {
if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) {
configuration.getGlobal().setServerConnectors(new ArrayList<>());
}
configuration.getGlobal().getServerConnectors().add(configuration.getGlobal().getServerConnector());
ServerConnectorConfigImpl serverConnectorConfig = new ServerConnectorConfigImpl();
serverConnectorConfig.setAddresses(
Collections.singletonList(consulContextProperties.host + ":" + consulContextProperties.port));
serverConnectorConfig.setProtocol(DefaultPlugins.SERVER_CONNECTOR_CONSUL);
Map<String, String> metadata = serverConnectorConfig.getMetadata();
if (StringUtils.isNotBlank(consulContextProperties.serviceName)) {
metadata.put(MetadataMapKey.SERVICE_NAME_KEY, consulContextProperties.serviceName);
}
if (StringUtils.isNotBlank(consulContextProperties.instanceId)) {
metadata.put(MetadataMapKey.INSTANCE_ID_KEY, consulContextProperties.instanceId);
}
if (consulContextProperties.preferIpAddress && StringUtils.isNotBlank(
consulContextProperties.ipAddress)) {
metadata.put(MetadataMapKey.PREFER_IP_ADDRESS_KEY,
String.valueOf(consulContextProperties.preferIpAddress));
metadata.put(MetadataMapKey.IP_ADDRESS_KEY, consulContextProperties.ipAddress);
}
configuration.getGlobal().getServerConnectors().add(serverConnectorConfig);
}
}
@Override
public int getOrder() {
return ModifierOrder.LAST;
}
}
}
Loading…
Cancel
Save