Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
[#81] support reading environment for configurations (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Feb 23, 2021
1 parent 5017c4c commit 3111835
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,34 @@

import java.util.Arrays;

import org.apache.dubbo.common.utils.StringUtils;
import org.apache.servicecomb.config.center.client.AddressManager;
import org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest;

import org.apache.dubbo.common.utils.ConfigUtils;
import org.springframework.core.env.Environment;


public class ConfigCenterConfiguration {
private Environment environment;

public ConfigCenterConfiguration(Environment environment) {
this.environment = environment;
}

public static AddressManager createAddressManager() {
String address = ConfigUtils.getProperty(KEY_CONFIG_ADDRESS, "http://127.0.0.1:30103");
String project = ConfigUtils.getProperty(KEY_SERVICE_PROJECT, "default");
public AddressManager createAddressManager() {
String address = environment.getProperty(KEY_CONFIG_ADDRESS, "");
if (StringUtils.isEmpty(address)) {
return null;
}
String project = environment.getProperty(KEY_SERVICE_PROJECT, "default");
return new AddressManager(project, Arrays.asList(address.split(",")));
}

public static QueryConfigurationsRequest createQueryConfigurationsRequest() {
public QueryConfigurationsRequest createQueryConfigurationsRequest() {
QueryConfigurationsRequest request = new QueryConfigurationsRequest();
request.setApplication(ConfigUtils.getProperty(KEY_SERVICE_APPLICATION, "default"));
request.setServiceName(ConfigUtils.getProperty(KEY_SERVICE_NAME, "defaultMicroserviceName"));
request.setVersion(ConfigUtils.getProperty(KEY_SERVICE_VERSION, "1.0.0.0"));
request.setEnvironment(ConfigUtils.getProperty(KEY_SERVICE_ENVIRONMENT, ""));
request.setApplication(environment.getProperty(KEY_SERVICE_APPLICATION, "default"));
request.setServiceName(environment.getProperty(KEY_SERVICE_NAME, "defaultMicroserviceName"));
request.setVersion(environment.getProperty(KEY_SERVICE_VERSION, "1.0.0.0"));
request.setEnvironment(environment.getProperty(KEY_SERVICE_ENVIRONMENT, ""));
// 需要设置为 null, 并且 query 参数为 revision=null 才会返回 revision 信息。 revision = 是不行的。
request.setRevision(null);
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,23 @@ public class ConfigurationSpringInitializer extends PropertyPlaceholderConfigure

private int pollingWaitInSeconds = 0;

private ConfigCenterConfiguration configCenterConfiguration;

private KieConfigConfiguration kieConfigConfiguration;

public ConfigurationSpringInitializer() {
setOrder(Ordered.LOWEST_PRECEDENCE / 2);
setIgnoreUnresolvablePlaceholders(true);
}

@Override
public void setEnvironment(Environment environment) {
configCenterConfiguration = new ConfigCenterConfiguration(environment);
kieConfigConfiguration = new KieConfigConfiguration(environment);
if (!(environment instanceof ConfigurableEnvironment)) {
return;
}

ConfigurableEnvironment ce = (ConfigurableEnvironment) environment;

addDubboProperties(ce);
Expand Down Expand Up @@ -130,8 +137,12 @@ private void addDubboProperties(ConfigurableEnvironment ce) {
}

private void configCenterClient(ConfigurableEnvironment ce) {
queryConfigurationsRequest = ConfigCenterConfiguration.createQueryConfigurationsRequest();
AddressManager addressManager = ConfigCenterConfiguration.createAddressManager();
queryConfigurationsRequest = configCenterConfiguration.createQueryConfigurationsRequest();
AddressManager addressManager = configCenterConfiguration.createAddressManager();
if (addressManager == null) {
LOGGER.warn("Config center address is not configured and will not enable dynamic config.");
return;
}
ConfigCenterClient configCenterClient = new ConfigCenterClient(addressManager, httpTransport);
try {
QueryConfigurationsResponse response = configCenterClient.queryConfigurations(queryConfigurationsRequest);
Expand All @@ -151,8 +162,12 @@ private void configCenterClient(ConfigurableEnvironment ce) {

//use KIE as config center
private void configKieClient(ConfigurableEnvironment ce) {
configurationsRequest = KieConfigConfiguration.createConfigurationsRequest();
KieAddressManager kieAddressManager = KieConfigConfiguration.createKieAddressManager();
configurationsRequest = kieConfigConfiguration.createConfigurationsRequest();
KieAddressManager kieAddressManager = kieConfigConfiguration.createKieAddressManager();
if (kieAddressManager == null) {
LOGGER.warn("Kie address is not configured and will not enable dynamic config.");
return;
}
KieConfigOperation kieClient = new KieClient(kieAddressManager, httpTransport);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,59 @@
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_CONFIG_ADDRESS;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_CONFIG_ADDRESSTYPE;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_APPLICATION;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_ENABLELONGPOLLING;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_ENVIRONMENT;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_NAME;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_VERSION;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_ENABLELONGPOLLING;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_POLLINGWAITSEC;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_PROJECT;
import static com.huaweicloud.dubbo.common.CommonConfiguration.KEY_SERVICE_VERSION;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.dubbo.common.utils.ConfigUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.servicecomb.config.kie.client.model.ConfigConstants;
import org.apache.servicecomb.config.kie.client.model.KieAddressManager;
import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequest;
import org.apache.servicecomb.config.kie.client.model.KieAddressManager;
import org.springframework.core.env.Environment;


public class KieConfigConfiguration {
private Environment environment;

public KieConfigConfiguration(Environment environment) {
this.environment = environment;
}

//初始化配置属性值,这里统一初始化,对于client模块,解耦对默认值的感知,屏蔽不同框架带来的配置项差异对KieAddressManager的影响
public static KieAddressManager createKieAddressManager() {
Properties properties = ConfigUtils.getProperties();
public KieAddressManager createKieAddressManager() {
String address = environment.getProperty(KEY_CONFIG_ADDRESS, "");
if (StringUtils.isEmpty(address)) {
return null;
}
Properties properties = new Properties();
Map<String, String> configKey = new HashMap<>();
properties.setProperty(KEY_SERVICE_PROJECT,ConfigUtils.getProperty(KEY_SERVICE_PROJECT, "default"));
properties.setProperty(KEY_CONFIG_ADDRESSTYPE,ConfigUtils.getProperty(KEY_CONFIG_ADDRESSTYPE, ""));
properties.setProperty(KEY_SERVICE_ENABLELONGPOLLING,ConfigUtils.getProperty(KEY_SERVICE_ENABLELONGPOLLING, "true"));
properties.setProperty(KEY_SERVICE_POLLINGWAITSEC,ConfigUtils.getProperty(KEY_SERVICE_POLLINGWAITSEC, "30"));
String address = properties.getProperty(KEY_CONFIG_ADDRESS, "http://127.0.0.1:30110");
configKey.put(ConfigConstants.KEY_PROJECT,KEY_SERVICE_PROJECT);
configKey.put(ConfigConstants.KEY_ENABLELONGPOLLING,KEY_SERVICE_ENABLELONGPOLLING);
configKey.put(ConfigConstants.KEY_POLLINGWAITSEC,KEY_SERVICE_POLLINGWAITSEC);
properties.setProperty(KEY_SERVICE_PROJECT, environment.getProperty(KEY_SERVICE_PROJECT, "default"));
properties.setProperty(KEY_CONFIG_ADDRESSTYPE, environment.getProperty(KEY_CONFIG_ADDRESSTYPE, ""));
properties
.setProperty(KEY_SERVICE_ENABLELONGPOLLING, environment.getProperty(KEY_SERVICE_ENABLELONGPOLLING, "true"));
properties.setProperty(KEY_SERVICE_POLLINGWAITSEC, environment.getProperty(KEY_SERVICE_POLLINGWAITSEC, "30"));

configKey.put(ConfigConstants.KEY_PROJECT, KEY_SERVICE_PROJECT);
configKey.put(ConfigConstants.KEY_ENABLELONGPOLLING, KEY_SERVICE_ENABLELONGPOLLING);
configKey.put(ConfigConstants.KEY_POLLINGWAITSEC, KEY_SERVICE_POLLINGWAITSEC);

return new KieAddressManager(properties, Arrays.asList(address.split(",")), configKey);
}

public static ConfigurationsRequest createConfigurationsRequest() {
public ConfigurationsRequest createConfigurationsRequest() {
ConfigurationsRequest request = new ConfigurationsRequest();
request.setApplication(ConfigUtils.getProperty(KEY_SERVICE_APPLICATION, "default"));
request.setServiceName(ConfigUtils.getProperty(KEY_SERVICE_NAME, "defaultMicroserviceName"));
request.setVersion(ConfigUtils.getProperty(KEY_SERVICE_VERSION, "1.0.0.0"));
request.setEnvironment(ConfigUtils.getProperty(KEY_SERVICE_ENVIRONMENT, ""));
request.setApplication(environment.getProperty(KEY_SERVICE_APPLICATION, "default"));
request.setServiceName(environment.getProperty(KEY_SERVICE_NAME, "defaultMicroserviceName"));
request.setVersion(environment.getProperty(KEY_SERVICE_VERSION, "1.0.0.0"));
request.setEnvironment(environment.getProperty(KEY_SERVICE_ENVIRONMENT, ""));
// 需要设置为 null, 并且 query 参数为 revision=null 才会返回 revision 信息。 revision = 是不行的。
request.setRevision(null);
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.core.env.Environment;

import com.google.common.eventbus.Subscribe;
import com.huaweicloud.dubbo.common.CommonConfiguration;
Expand All @@ -61,7 +63,8 @@
import com.huaweicloud.dubbo.common.RegistrationReadyEvent;
import com.huaweicloud.dubbo.common.SchemaInfo;

public class RegistrationListener implements ApplicationListener<ApplicationEvent>, ApplicationEventPublisherAware {
public class RegistrationListener implements ApplicationListener<ApplicationEvent>, ApplicationEventPublisherAware,
EnvironmentAware {
static class SubscriptionKey {
final String appId;

Expand Down Expand Up @@ -136,6 +139,8 @@ static class SubscriptionData {

private List<NewSubscriberEvent> pendingSubscribeEvent = new ArrayList<>();

private ServiceCenterConfiguration serviceCenterConfiguration;

public RegistrationListener() {
}

Expand All @@ -160,23 +165,28 @@ public void shutdown() {
}
}

@Override
public void setEnvironment(Environment environment) {
serviceCenterConfiguration = new ServiceCenterConfiguration(environment);
}

@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
if (applicationEvent instanceof ContextStartedEvent) {
try {
AddressManager addressManager = ServiceCenterConfiguration.createAddressManager();
AddressManager addressManager = serviceCenterConfiguration.createAddressManager();
SSLProperties sslProperties = CommonConfiguration.createSSLProperties();
RequestAuthHeaderProvider requestAuthHeaderProvider = CommonConfiguration.createRequestAuthHeaderProvider();
client = new ServiceCenterClient(addressManager, sslProperties, requestAuthHeaderProvider,
"default", null);
microservice = ServiceCenterConfiguration.createMicroservice();
microservice = serviceCenterConfiguration.createMicroservice();
if (registry != null) {
// consumer: 如果没有 provider 接口, dubbo 启动的时候, 不会初始化 Registry。 调用接口的时候,才会初始化。
microservice
.setSchemas(registry.getRegisters().stream().map(URL::getPath).collect(Collectors.toList()));
}

instance = ServiceCenterConfiguration.createMicroserviceInstance();
instance = serviceCenterConfiguration.createMicroserviceInstance();
List<String> endpoints = new ArrayList<>();
if (registry != null) {
endpoints.addAll(registry.getRegisters().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@

import java.util.Arrays;


import org.apache.dubbo.common.utils.ConfigUtils;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.servicecomb.service.center.client.AddressManager;
import org.apache.servicecomb.service.center.client.model.Framework;
import org.apache.servicecomb.service.center.client.model.Microservice;
import org.apache.servicecomb.service.center.client.model.MicroserviceInstance;
import org.apache.servicecomb.service.center.client.model.MicroserviceInstanceStatus;
import org.springframework.core.env.Environment;

public class ServiceCenterConfiguration {
public static Microservice createMicroservice() {
private Environment environment;

public ServiceCenterConfiguration(Environment environment) {
this.environment = environment;
}

public Microservice createMicroservice() {
Microservice microservice = new Microservice();
microservice.setAppId(ConfigUtils.getProperty(KEY_SERVICE_APPLICATION, "default"));
microservice.setServiceName(ConfigUtils.getProperty(KEY_SERVICE_NAME, "defaultMicroserviceName"));
microservice.setVersion(ConfigUtils.getProperty(KEY_SERVICE_VERSION, "1.0.0.0"));
microservice.setEnvironment(ConfigUtils.getProperty(KEY_SERVICE_ENVIRONMENT, ""));
microservice.setAppId(environment.getProperty(KEY_SERVICE_APPLICATION, "default"));
microservice.setServiceName(environment.getProperty(KEY_SERVICE_NAME, "defaultMicroserviceName"));
microservice.setVersion(environment.getProperty(KEY_SERVICE_VERSION, "1.0.0.0"));
microservice.setEnvironment(environment.getProperty(KEY_SERVICE_ENVIRONMENT, ""));
Framework framework = new Framework();
framework.setName("DUBBO-SERVICECOMB");
StringBuilder version = new StringBuilder();
Expand All @@ -56,15 +61,15 @@ public static Microservice createMicroservice() {
return microservice;
}

public static MicroserviceInstance createMicroserviceInstance() {
public MicroserviceInstance createMicroserviceInstance() {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setStatus(MicroserviceInstanceStatus.valueOf(ConfigUtils.getProperty(KEY_INSTANCE_ENVIRONMENT, "UP")));
instance.setStatus(MicroserviceInstanceStatus.valueOf(environment.getProperty(KEY_INSTANCE_ENVIRONMENT, "UP")));
return instance;
}

public static AddressManager createAddressManager() {
String address = ConfigUtils.getProperty(KEY_REGISTRY_ADDRESS, "http://127.0.0.1:30100");
String project = ConfigUtils.getProperty(KEY_SERVICE_PROJECT, "default");
public AddressManager createAddressManager() {
String address = environment.getProperty(KEY_REGISTRY_ADDRESS, "http://127.0.0.1:30100");
String project = environment.getProperty(KEY_SERVICE_PROJECT, "default");
return new AddressManager(project, Arrays.asList(address.split(",")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,10 @@
<artifactId>spring-boot-discovery-common-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-service-center</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-governance-center</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-config-center</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-spring-boot</artifactId>
<artifactId>dubbo-servicecomb-solution-spring-boot</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,13 @@
<artifactId>spring-boot-discovery-common-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-service-center</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-config-center</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-governance-center</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-spring-boot</artifactId>
<artifactId>dubbo-servicecomb-solution-spring-boot</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,23 @@
## ---------------------------------------------------------------------------

server:
port: 8084
port: 8084

PAAS_CSE_SC_ENDPOINT: http://127.0.0.1:30100
PAAS_CSE_CC_ENDPOINT: http://127.0.0.1:30113
#### 服务配置信息 ####
dubbo:
servicecomb:
service:
application: discovery # 所属应用。
name: price-provider # 服务名称。
version: 1.0.0 # 版本。默认为 1.0.0.0
# environmen: production # 环境。默认为空。可选值:development, testing, acceptance, production
# project: # project。 默认为 default
# instance:
# initialStatus: UP # 实例初始状态。可选值: UP, DOWN, STARTING, OUTOFSERVICE

registry:
address: ${PAAS_CSE_SC_ENDPOINT}
config:
address: ${PAAS_CSE_CC_ENDPOINT}
Loading

0 comments on commit 3111835

Please sign in to comment.