Optimize/optimize annotation (#672)

pull/674/head
lingxiao,wu 2 years ago committed by GitHub
parent aa84113924
commit f36195109d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,3 +2,4 @@
---
- [Optimize:optimize SpringValueProcessor.](https://github.com/Tencent/spring-cloud-tencent/pull/655)
- [Optimize/optimize annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/672)

@ -24,8 +24,6 @@ import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Spring Cloud Tencent config Override polaris config.
*
@ -33,8 +31,11 @@ import org.springframework.beans.factory.annotation.Autowired;
*/
public class DiscoveryConfigModifier implements PolarisConfigModifier {
@Autowired
private PolarisDiscoveryProperties polarisDiscoveryProperties;
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
public DiscoveryConfigModifier(PolarisDiscoveryProperties polarisDiscoveryProperties) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
}
@Override
public void modify(ConfigurationImpl configuration) {

@ -19,8 +19,13 @@ package com.tencent.cloud.polaris;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.cloud.polaris.extend.consul.ConsulConfigModifier;
import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.client.api.SDKContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -38,12 +43,27 @@ public class DiscoveryPropertiesAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public PolarisDiscoveryHandler polarisDiscoveryHandler() {
return new PolarisDiscoveryHandler();
public PolarisDiscoveryHandler polarisDiscoveryHandler(PolarisDiscoveryProperties polarisDiscoveryProperties,
ProviderAPI providerAPI, SDKContext sdkContext,
ConsumerAPI polarisConsumer) {
return new PolarisDiscoveryHandler(polarisDiscoveryProperties, providerAPI, sdkContext, polarisConsumer);
}
@Bean
public DiscoveryConfigModifier discoveryConfigModifier() {
return new DiscoveryConfigModifier();
@ConditionalOnMissingBean
public DiscoveryConfigModifier discoveryConfigModifier(PolarisDiscoveryProperties polarisDiscoveryProperties) {
return new DiscoveryConfigModifier(polarisDiscoveryProperties);
}
@Bean
@ConditionalOnMissingBean
public ConsulConfigModifier consulConfigModifier(@Autowired(required = false) ConsulContextProperties consulContextProperties) {
return new ConsulConfigModifier(consulContextProperties);
}
@Bean
@ConditionalOnMissingBean
public PolarisDiscoveryConfigModifier polarisDiscoveryConfigModifier(PolarisDiscoveryProperties polarisDiscoveryProperties) {
return new PolarisDiscoveryConfigModifier(polarisDiscoveryProperties);
}
}

@ -0,0 +1,59 @@
/*
* 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;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.consumer.DiscoveryConfigImpl;
import com.tencent.polaris.factory.config.provider.RegisterConfigImpl;
/**
* @author lingxiao.wlx
*/
public class PolarisDiscoveryConfigModifier implements PolarisConfigModifier {
private static final String ID = "polaris";
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
public PolarisDiscoveryConfigModifier(PolarisDiscoveryProperties polarisDiscoveryProperties) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
}
@Override
public void modify(ConfigurationImpl configuration) {
if (polarisDiscoveryProperties != null) {
DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl();
discoveryConfig.setServerConnectorId(ID);
discoveryConfig.setEnable(polarisDiscoveryProperties.isEnabled());
configuration.getConsumer().getDiscoveries().add(discoveryConfig);
RegisterConfigImpl registerConfig = new RegisterConfigImpl();
registerConfig.setServerConnectorId(ID);
registerConfig.setEnable(polarisDiscoveryProperties.isRegisterEnabled());
configuration.getProvider().getRegisters().add(registerConfig);
}
}
@Override
public int getOrder() {
return ContextConstant.ModifierOrder.LAST;
}
}

@ -19,16 +19,9 @@
package com.tencent.cloud.polaris;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.consumer.DiscoveryConfigImpl;
import com.tencent.polaris.factory.config.provider.RegisterConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import static com.tencent.cloud.common.constant.ContextConstant.DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL;
@ -223,6 +216,22 @@ public class PolarisDiscoveryProperties {
this.heartbeatInterval = heartbeatInterval;
}
public Boolean getEnabled() {
return enabled;
}
public Boolean getRegisterEnabled() {
return registerEnabled;
}
public void setRegisterEnabled(Boolean registerEnabled) {
this.registerEnabled = registerEnabled;
}
public Boolean getHeartbeatEnabled() {
return heartbeatEnabled;
}
@Override
public String toString() {
return "PolarisDiscoveryProperties{" +
@ -241,38 +250,4 @@ public class PolarisDiscoveryProperties {
", serviceListRefreshInterval=" + serviceListRefreshInterval +
'}';
}
@Bean
@ConditionalOnMissingBean
public PolarisDiscoveryConfigModifier polarisDiscoveryConfigModifier() {
return new PolarisDiscoveryConfigModifier();
}
private static class PolarisDiscoveryConfigModifier implements PolarisConfigModifier {
private static final String ID = "polaris";
@Autowired(required = false)
private PolarisDiscoveryProperties polarisDiscoveryProperties;
@Override
public void modify(ConfigurationImpl configuration) {
if (polarisDiscoveryProperties != null) {
DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl();
discoveryConfig.setServerConnectorId(ID);
discoveryConfig.setEnable(polarisDiscoveryProperties.enabled);
configuration.getConsumer().getDiscoveries().add(discoveryConfig);
RegisterConfigImpl registerConfig = new RegisterConfigImpl();
registerConfig.setServerConnectorId(ID);
registerConfig.setEnable(polarisDiscoveryProperties.registerEnabled);
configuration.getProvider().getRegisters().add(registerConfig);
}
}
@Override
public int getOrder() {
return ContextConstant.ModifierOrder.LAST;
}
}
}

@ -28,28 +28,28 @@ import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.polaris.api.rpc.ServicesResponse;
import com.tencent.polaris.client.api.SDKContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Discovery Handler for Polaris.
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Component
public class PolarisDiscoveryHandler {
@Autowired
private PolarisDiscoveryProperties polarisDiscoveryProperties;
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
private final ProviderAPI providerAPI;
@Autowired
private ProviderAPI providerAPI;
private final SDKContext sdkContext;
@Autowired
private SDKContext sdkContext;
private final ConsumerAPI polarisConsumer;
@Autowired
private ConsumerAPI polarisConsumer;
public PolarisDiscoveryHandler(PolarisDiscoveryProperties polarisDiscoveryProperties,
ProviderAPI providerAPI, SDKContext sdkContext, ConsumerAPI polarisConsumer) {
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
this.providerAPI = providerAPI;
this.sdkContext = sdkContext;
this.polarisConsumer = polarisConsumer;
}
/**
* Get a list of healthy instances.

@ -0,0 +1,95 @@
/*
* 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.extend.consul;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import com.tencent.cloud.common.constant.ContextConstant;
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.consumer.DiscoveryConfigImpl;
import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl;
import com.tencent.polaris.factory.config.provider.RegisterConfigImpl;
import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
/**
* @author lingxiao.wlx
*/
public class ConsulConfigModifier implements PolarisConfigModifier {
private static final String ID = "consul";
private final ConsulContextProperties consulContextProperties;
public ConsulConfigModifier(ConsulContextProperties consulContextProperties) {
this.consulContextProperties = consulContextProperties;
}
@Override
public void modify(ConfigurationImpl configuration) {
if (consulContextProperties != null && consulContextProperties.isEnabled()) {
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());
}
ServerConnectorConfigImpl serverConnectorConfig = new ServerConnectorConfigImpl();
serverConnectorConfig.setId(ID);
serverConnectorConfig.setAddresses(
Collections.singletonList(consulContextProperties.getHost() + ":" + consulContextProperties.getPort()));
serverConnectorConfig.setProtocol(DefaultPlugins.SERVER_CONNECTOR_CONSUL);
Map<String, String> metadata = serverConnectorConfig.getMetadata();
if (StringUtils.isNotBlank(consulContextProperties.getServiceName())) {
metadata.put(ConsulConstant.MetadataMapKey.SERVICE_NAME_KEY, consulContextProperties.getServiceName());
}
if (StringUtils.isNotBlank(consulContextProperties.getInstanceId())) {
metadata.put(ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY, consulContextProperties.getInstanceId());
}
if (consulContextProperties.isPreferIpAddress()
&& StringUtils.isNotBlank(consulContextProperties.getIpAddress())) {
metadata.put(ConsulConstant.MetadataMapKey.PREFER_IP_ADDRESS_KEY,
String.valueOf(consulContextProperties.isPreferIpAddress()));
metadata.put(ConsulConstant.MetadataMapKey.IP_ADDRESS_KEY, consulContextProperties.getIpAddress());
}
configuration.getGlobal().getServerConnectors().add(serverConnectorConfig);
DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl();
discoveryConfig.setServerConnectorId(ID);
discoveryConfig.setEnable(consulContextProperties.isDiscoveryEnabled());
configuration.getConsumer().getDiscoveries().add(discoveryConfig);
RegisterConfigImpl registerConfig = new RegisterConfigImpl();
registerConfig.setServerConnectorId(ID);
registerConfig.setEnable(consulContextProperties.isRegister());
configuration.getProvider().getRegisters().add(registerConfig);
}
}
@Override
public int getOrder() {
return ContextConstant.ModifierOrder.LAST;
}
}

@ -17,26 +17,8 @@
package com.tencent.cloud.polaris.extend.consul;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
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.consumer.DiscoveryConfigImpl;
import com.tencent.polaris.factory.config.global.ServerConnectorConfigImpl;
import com.tencent.polaris.factory.config.provider.RegisterConfigImpl;
import com.tencent.polaris.plugins.connector.common.constant.ConsulConstant.MetadataMapKey;
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.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
/**
* Discovery configuration of Consul.
@ -105,64 +87,43 @@ public class ConsulContextProperties {
return discoveryEnabled;
}
@Bean
@ConditionalOnMissingBean
public ConsulConfigModifier consulConfigModifier() {
return new ConsulConfigModifier();
public void setRegister(boolean register) {
this.register = register;
}
private static class ConsulConfigModifier implements PolarisConfigModifier {
private static final String ID = "consul";
@Autowired(required = false)
private ConsulContextProperties consulContextProperties;
@Override
public void modify(ConfigurationImpl configuration) {
if (consulContextProperties != null && consulContextProperties.enabled) {
if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) {
configuration.getGlobal().setServerConnectors(new ArrayList<>());
public void setDiscoveryEnabled(boolean discoveryEnabled) {
this.discoveryEnabled = discoveryEnabled;
}
if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())
&& null != configuration.getGlobal().getServerConnector()) {
configuration.getGlobal().getServerConnectors().add(configuration.getGlobal().getServerConnector());
}
ServerConnectorConfigImpl serverConnectorConfig = new ServerConnectorConfigImpl();
serverConnectorConfig.setId(ID);
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);
public String getInstanceId() {
return instanceId;
}
if (StringUtils.isNotBlank(consulContextProperties.instanceId)) {
metadata.put(MetadataMapKey.INSTANCE_ID_KEY, consulContextProperties.instanceId);
public void setInstanceId(String instanceId) {
this.instanceId = 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);
public String getServiceName() {
return serviceName;
}
configuration.getGlobal().getServerConnectors().add(serverConnectorConfig);
DiscoveryConfigImpl discoveryConfig = new DiscoveryConfigImpl();
discoveryConfig.setServerConnectorId(ID);
discoveryConfig.setEnable(consulContextProperties.discoveryEnabled);
configuration.getConsumer().getDiscoveries().add(discoveryConfig);
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
RegisterConfigImpl registerConfig = new RegisterConfigImpl();
registerConfig.setServerConnectorId(ID);
registerConfig.setEnable(consulContextProperties.register);
configuration.getProvider().getRegisters().add(registerConfig);
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
@Override
public int getOrder() {
return ModifierOrder.LAST;
public boolean isPreferIpAddress() {
return preferIpAddress;
}
public void setPreferIpAddress(boolean preferIpAddress) {
this.preferIpAddress = preferIpAddress;
}
}

@ -21,14 +21,12 @@ import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
/**
* Spring Context Util.
*
* @author Hongwei Zhu
*/
@Component
public class ApplicationContextAwareUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;

@ -88,13 +88,15 @@ public class RpcEnhancementAutoConfiguration {
static class PolarisReporterConfig {
@Bean
public SuccessPolarisReporter successPolarisReporter(RpcEnhancementReporterProperties properties) {
return new SuccessPolarisReporter(properties);
public SuccessPolarisReporter successPolarisReporter(RpcEnhancementReporterProperties properties,
@Autowired(required = false) ConsumerAPI consumerAPI) {
return new SuccessPolarisReporter(properties, consumerAPI);
}
@Bean
public ExceptionPolarisReporter exceptionPolarisReporter(RpcEnhancementReporterProperties properties) {
return new ExceptionPolarisReporter(properties);
public ExceptionPolarisReporter exceptionPolarisReporter(RpcEnhancementReporterProperties properties,
@Autowired(required = false) ConsumerAPI consumerAPI) {
return new ExceptionPolarisReporter(properties, consumerAPI);
}
}
}

@ -31,7 +31,6 @@ import feign.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
/**
@ -43,11 +42,13 @@ public class ExceptionPolarisReporter implements EnhancedFeignPlugin {
private static final Logger LOG = LoggerFactory.getLogger(ExceptionPolarisReporter.class);
private final RpcEnhancementReporterProperties reporterProperties;
@Autowired(required = false)
private ConsumerAPI consumerAPI;
public ExceptionPolarisReporter(RpcEnhancementReporterProperties reporterProperties) {
private final ConsumerAPI consumerAPI;
public ExceptionPolarisReporter(RpcEnhancementReporterProperties reporterProperties,
ConsumerAPI consumerAPI) {
this.reporterProperties = reporterProperties;
this.consumerAPI = consumerAPI;
}
@Override

@ -30,7 +30,6 @@ import feign.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
@ -42,11 +41,12 @@ import org.springframework.http.HttpStatus;
public class SuccessPolarisReporter extends AbstractPolarisReporterAdapter implements EnhancedFeignPlugin {
private static final Logger LOG = LoggerFactory.getLogger(SuccessPolarisReporter.class);
@Autowired(required = false)
private ConsumerAPI consumerAPI;
public SuccessPolarisReporter(RpcEnhancementReporterProperties properties) {
private final ConsumerAPI consumerAPI;
public SuccessPolarisReporter(RpcEnhancementReporterProperties properties, ConsumerAPI consumerAPI) {
super(properties);
this.consumerAPI = consumerAPI;
}
@Override

Loading…
Cancel
Save