Skip to content

Commit

Permalink
optimize: optimize Apollo configuration (apache#4018)
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly committed Sep 15, 2021
1 parent a38b873 commit 64c9598
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 26 deletions.
5 changes: 2 additions & 3 deletions changes/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单
- [[#3982](https://github.com/seata/seata/pull/3982)] 优化 readme 文档和升级POM依赖
- [[#3991](https://github.com/seata/seata/pull/3991)] 关闭spring boot下无用的fileListener
- [[#3327](https://github.com/seata/seata/pull/3327)] 支持从etcd3单一key中读取所有配置
- [[#4001](https://github.com/seata/seata/pull/4001)] 支持从nacos,zk,consul,etcd3中读取yml
- [[#4017](https://github.com/seata/seata/pull/4017)] 优化文件配置

- [[#4001](https://github.com/seata/seata/pull/4001)] 支持从Nacos,Zookeeper,Consul,Etcd3 中读取 yml
- [[#4017](https://github.com/seata/seata/pull/4017)] 优化文件配置

### test:

Expand Down
2 changes: 1 addition & 1 deletion changes/en-us/1.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
- [[#3949](https://github.com/seata/seata/pull/3949)] `nacos-config.py` support default parameters and optional input parameters
- [[#3991](https://github.com/seata/seata/pull/3991)] disable listening in the FileConfiguration center in Springboot
- [[#3327](https://github.com/seata/seata/pull/3327)] supports reading all configurations from a single Etcd3 key
- [[#4001](https://github.com/seata/seata/pull/4001)] support to read YML from NACOS, ZK, Consul, ETCD3
- [[#4001](https://github.com/seata/seata/pull/4001)] support to read YML configuration from Nacos, Zookeeper, Consul, Etcd3
- [[#4017](https://github.com/seata/seata/pull/4017)] optimize file configuration

### test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ApolloConfiguration extends AbstractConfiguration {
private static final String REGISTRY_TYPE = "apollo";
private static final String APP_ID = "appId";
private static final String APOLLO_META = "apolloMeta";
private static final String APOLLO_SECRET = "apolloAccesskeySecret";
private static final String APOLLO_SECRET = "apolloAccessKeySecret";
private static final String APOLLO_CLUSTER = "cluster";
private static final String APOLLO_CONFIG_SERVICE = "apolloConfigService";
private static final String PROP_APP_ID = "app.id";
Expand Down Expand Up @@ -208,27 +208,27 @@ public String getTypeName() {
return REGISTRY_TYPE;
}

private static String getApolloMetaFileKey() {
public static String getApolloMetaFileKey() {
return String.join(FILE_CONFIG_SPLIT_CHAR, FILE_ROOT_CONFIG, REGISTRY_TYPE, APOLLO_META);
}

private static String getApolloSecretFileKey() {
public static String getApolloSecretFileKey() {
return String.join(FILE_CONFIG_SPLIT_CHAR, FILE_ROOT_CONFIG, REGISTRY_TYPE, APOLLO_SECRET);
}

private static String getApolloAppIdFileKey() {
public static String getApolloAppIdFileKey() {
return String.join(FILE_CONFIG_SPLIT_CHAR, FILE_ROOT_CONFIG, REGISTRY_TYPE, APP_ID);
}

private static String getApolloNamespaceKey() {
public static String getApolloNamespaceKey() {
return String.join(FILE_CONFIG_SPLIT_CHAR, FILE_ROOT_CONFIG, REGISTRY_TYPE, NAMESPACE);
}

private static String getApolloCluster() {
public static String getApolloCluster() {
return String.join(FILE_CONFIG_SPLIT_CHAR, FILE_ROOT_CONFIG, REGISTRY_TYPE, APOLLO_CLUSTER);
}

private static String getApolloConfigService() {
public static String getApolloConfigService() {
return String.join(FILE_CONFIG_SPLIT_CHAR, FILE_ROOT_CONFIG, REGISTRY_TYPE, APOLLO_CONFIG_SERVICE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@

<artifactId>seata-spring-autoconfigure-core</artifactId>
<name>seata-spring-autoconfigure-core ${project.version}</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-discovery-all</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
@ConfigurationProperties(prefix = CONFIG_APOLLO_PREFIX)
public class ConfigApolloProperties {
private String appId = "seata-server";
private String apolloMeta = "http://192.168.1.204:8801";
private String apolloMeta = "";
private String namespace = "application";
private String apolloAccesskeySecret = "";
private String apolloAccessKeySecret = "";
private String apolloConfigService;
private String cluster;

public String getAppId() {
return appId;
Expand Down Expand Up @@ -58,13 +60,30 @@ public ConfigApolloProperties setNamespace(String namespace) {
return this;
}

public String getApolloAccesskeySecret() {
return apolloAccesskeySecret;
public String getApolloAccessKeySecret() {
return apolloAccessKeySecret;
}

public ConfigApolloProperties setApolloAccesskeySecret(String apolloAccesskeySecret) {
this.apolloAccesskeySecret = apolloAccesskeySecret;
public ConfigApolloProperties setApolloAccessKeySecret(String apolloAccessKeySecret) {
this.apolloAccessKeySecret = apolloAccessKeySecret;
return this;
}

public String getApolloConfigService() {
return apolloConfigService;
}

public ConfigApolloProperties setApolloConfigService(String apolloConfigService) {
this.apolloConfigService = apolloConfigService;
return this;
}

public String getCluster() {
return cluster;
}

public ConfigApolloProperties setCluster(String cluster) {
this.cluster = cluster;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.seata.spring.boot.autoconfigure;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @author slievrly
*/
public class BaseConfigPropertiesTest {
protected static AnnotationConfigApplicationContext applicationContex;
protected static final String STR_TEST_AAA = "aaa";
protected static final String STR_TEST_BBB = "bbb";
protected static final String STR_TEST_CCC = "ccc";
protected static final String STR_TEST_DDD = "ddd";
protected static final String STR_TEST_EEE = "eee";
protected static final String STR_TEST_FFF = "fff";

@BeforeAll
public static void setUp() {
applicationContex = new AnnotationConfigApplicationContext(
new String[] {"io.seata.spring.boot.autoconfigure.properties.test"});
SeataCoreEnvironmentPostProcessor processor = new SeataCoreEnvironmentPostProcessor();
processor.postProcessEnvironment(null, null);
}

@AfterAll
public static void tearDown() {
if (null != applicationContex) {
applicationContex.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
package io.seata.spring.boot.autoconfigure;

import io.seata.spring.boot.autoconfigure.properties.LogProperties;
import io.seata.spring.boot.autoconfigure.properties.ShutdownProperties;
import io.seata.spring.boot.autoconfigure.properties.ThreadFactoryProperties;
import io.seata.spring.boot.autoconfigure.properties.TransportProperties;
import io.seata.spring.boot.autoconfigure.properties.ShutdownProperties;
import io.seata.spring.boot.autoconfigure.properties.LogProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigApolloProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigConsulProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigCustomProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigEtcd3Properties;
Expand Down Expand Up @@ -90,12 +89,6 @@ public void testLogProperties() {
assertEquals(100, context.getBean(LogProperties.class).getExceptionRate());
}

@Test
public void testConfigApolloProperties() {
assertEquals("seata-server", context.getBean(ConfigApolloProperties.class).getAppId());
assertEquals("http://192.168.1.204:8801", context.getBean(ConfigApolloProperties.class).getApolloMeta());
}

@Test
public void testConfigConsulProperties() {
assertEquals("127.0.0.1:8500", context.getBean(ConfigConsulProperties.class).getServerAddr());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 io.seata.spring.boot.autoconfigure.properties.test;

import io.seata.common.loader.EnhancedServiceLoader;
import io.seata.config.Configuration;
import io.seata.config.ExtConfigurationProvider;
import io.seata.config.FileConfiguration;
import io.seata.config.apollo.ApolloConfiguration;
import io.seata.spring.boot.autoconfigure.BaseConfigPropertiesTest;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigApolloProperties;
import io.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

/**
* @author slievrly
*/
@org.springframework.context.annotation.Configuration
@Import(SpringApplicationContextProvider.class)
class ConfigApolloPropertiesTest extends BaseConfigPropertiesTest {

@Bean
public ConfigApolloProperties configApolloProperties() {
return new ConfigApolloProperties().setApolloMeta(STR_TEST_AAA).setApolloAccessKeySecret(STR_TEST_BBB).setAppId(
STR_TEST_CCC).setNamespace(STR_TEST_DDD).setCluster(STR_TEST_EEE).setApolloConfigService(STR_TEST_FFF);
}

@Test
public void testConfigApolloProperties() {
FileConfiguration configuration = mock(FileConfiguration.class);
Configuration currentConfiguration = EnhancedServiceLoader.load(ExtConfigurationProvider.class).provide(
configuration);

assertEquals(STR_TEST_AAA, currentConfiguration.getConfig(ApolloConfiguration.getApolloMetaFileKey()));
assertEquals(STR_TEST_BBB, currentConfiguration.getConfig(ApolloConfiguration.getApolloSecretFileKey()));
assertEquals(STR_TEST_CCC, currentConfiguration.getConfig(ApolloConfiguration.getApolloAppIdFileKey()));
assertEquals(STR_TEST_DDD, currentConfiguration.getConfig(ApolloConfiguration.getApolloNamespaceKey()));
assertEquals(STR_TEST_EEE, currentConfiguration.getConfig(ApolloConfiguration.getApolloCluster()));
assertEquals(STR_TEST_FFF, currentConfiguration.getConfig(ApolloConfiguration.getApolloConfigService()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
io.seata.spring.boot.autoconfigure.SeataCoreEnvironmentPostProcessor

0 comments on commit 64c9598

Please sign in to comment.