Skip to content

Commit c0b096f

Browse files
authored
Merge branch 'master' into apollo-logo
2 parents f0b5ed3 + cad7446 commit c0b096f

File tree

55 files changed

+867
-228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+867
-228
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ Apollo(配置中心)
33

44
[![Build Status](https://travis-ci.org/ctripcorp/apollo.svg?branch=master)](https://travis-ci.org/ctripcorp/apollo)
55
[![GitHub release](https://img.shields.io/github/release/ctripcorp/apollo.svg)](https://github.com/ctripcorp/apollo/releases)
6+
![maven](https://img.shields.io/maven-central/v/com.ctrip.framework.apollo/apollo.svg)
67
[![Coverage Status](https://coveralls.io/repos/github/ctripcorp/apollo/badge.svg?branch=master)](https://coveralls.io/github/ctripcorp/apollo?branch=master)
7-
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
88
<a href="https://scan.coverity.com/projects/ctripcorp-apollo">
99
<img alt="Coverity Scan Build Status" src="https://img.shields.io/coverity/scan/8244.svg"/>
1010
</a>
1111
[![codecov.io](https://codecov.io/github/ctripcorp/apollo/coverage.svg?branch=master)](https://codecov.io/github/ctripcorp/apollo?branch=master)
12+
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
1213

1314
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
1415

@@ -199,3 +200,4 @@ The project is licensed under the [Apache 2 license](https://github.com/ctripcor
199200
![河姆渡](https://raw.githubusercontent.com/ctripcorp/apollo/master/doc/images/known-users/homedo.png)
200201
![新网银行](https://raw.githubusercontent.com/ctripcorp/apollo/master/doc/images/known-users/xwbank.png)
201202
![中旅安信云贷](https://raw.githubusercontent.com/ctripcorp/apollo/master/doc/images/known-users/ctspcl.png)
203+
![美柚](https://raw.githubusercontent.com/ctripcorp/apollo/master/doc/images/known-users/meiyou.png)

apollo-adminservice/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.ctrip.framework.apollo</groupId>
66
<artifactId>apollo</artifactId>
7-
<version>0.11.0</version>
7+
<version>1.0.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>

apollo-adminservice/src/main/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
FROM openjdk:8-jre-alpine
88
MAINTAINER ameizi <sxyx2008@163.com>
99

10-
ENV VERSION 0.11.0
10+
ENV VERSION 1.0.0
1111

1212
RUN echo "http://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories \
1313
&& echo "http://mirrors.aliyun.com/alpine/v3.6/community" >> /etc/apk/repositories \

apollo-assembly/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.ctrip.framework.apollo</groupId>
66
<artifactId>apollo</artifactId>
7-
<version>0.11.0</version>
7+
<version>1.0.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>

apollo-biz/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.ctrip.framework.apollo</groupId>
66
<artifactId>apollo</artifactId>
7-
<version>0.11.0</version>
7+
<version>1.0.0</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<artifactId>apollo-biz</artifactId>

apollo-buildtools/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.ctrip.framework.apollo</groupId>
66
<artifactId>apollo</artifactId>
7-
<version>0.11.0</version>
7+
<version>1.0.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>

apollo-client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ If you need this functionality, you could specify the cluster as follows:
8888
<dependency>
8989
<groupId>com.ctrip.framework.apollo</groupId>
9090
<artifactId>apollo-client</artifactId>
91-
<version>0.11.0</version>
91+
<version>1.0.0</version>
9292
</dependency>
9393

9494
## III. Client Usage

apollo-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.ctrip.framework.apollo</groupId>
66
<artifactId>apollo</artifactId>
7-
<version>0.11.0</version>
7+
<version>1.0.0</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>

apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,20 @@ public interface Config {
162162
public long getDurationProperty(String key, long defaultValue);
163163

164164
/**
165-
* Add change listener to this config instance.
165+
* Add change listener to this config instance, will be notified when any key is changed in this namespace.
166166
*
167167
* @param listener the config change listener
168168
*/
169169
public void addChangeListener(ConfigChangeListener listener);
170170

171+
/**
172+
* Add change listener to this config instance, will only be notified when any of the interested keys is changed in this namespace.
173+
*
174+
* @param listener the config change listener
175+
* @param interestedKeys the keys interested by the listener
176+
*/
177+
public void addChangeListener(ConfigChangeListener listener, Set<String> interestedKeys);
178+
171179
/**
172180
* Return a set of the property names
173181
*

apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@
4040
public abstract class AbstractConfig implements Config {
4141
private static final Logger logger = LoggerFactory.getLogger(AbstractConfig.class);
4242

43-
private static ExecutorService m_executorService;
43+
private static final ExecutorService m_executorService;
4444

45-
private List<ConfigChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();
46-
private ConfigUtil m_configUtil;
45+
private final List<ConfigChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();
46+
private final Map<ConfigChangeListener, Set<String>> m_interestedKeys = Maps.newConcurrentMap();
47+
private final ConfigUtil m_configUtil;
4748
private volatile Cache<String, Integer> m_integerCache;
4849
private volatile Cache<String, Long> m_longCache;
4950
private volatile Cache<String, Short> m_shortCache;
@@ -53,9 +54,9 @@ public abstract class AbstractConfig implements Config {
5354
private volatile Cache<String, Boolean> m_booleanCache;
5455
private volatile Cache<String, Date> m_dateCache;
5556
private volatile Cache<String, Long> m_durationCache;
56-
private Map<String, Cache<String, String[]>> m_arrayCache;
57-
private List<Cache> allCaches;
58-
private AtomicLong m_configVersion; //indicate config version
57+
private final Map<String, Cache<String, String[]>> m_arrayCache;
58+
private final List<Cache> allCaches;
59+
private final AtomicLong m_configVersion; //indicate config version
5960

6061
static {
6162
m_executorService = Executors.newCachedThreadPool(ApolloThreadFactory
@@ -71,8 +72,16 @@ public AbstractConfig() {
7172

7273
@Override
7374
public void addChangeListener(ConfigChangeListener listener) {
75+
addChangeListener(listener, null);
76+
}
77+
78+
@Override
79+
public void addChangeListener(ConfigChangeListener listener, Set<String> interestedKeys) {
7480
if (!m_listeners.contains(listener)) {
7581
m_listeners.add(listener);
82+
if (interestedKeys != null && !interestedKeys.isEmpty()) {
83+
m_interestedKeys.put(listener, Sets.newHashSet(interestedKeys));
84+
}
7685
}
7786
}
7887

@@ -395,6 +404,10 @@ protected void clearConfigCache() {
395404

396405
protected void fireConfigChange(final ConfigChangeEvent changeEvent) {
397406
for (final ConfigChangeListener listener : m_listeners) {
407+
// check whether the listener is interested in this change event
408+
if (!isConfigChangeListenerInterested(listener, changeEvent)) {
409+
continue;
410+
}
398411
m_executorService.submit(new Runnable() {
399412
@Override
400413
public void run() {
@@ -415,6 +428,22 @@ public void run() {
415428
}
416429
}
417430

431+
private boolean isConfigChangeListenerInterested(ConfigChangeListener configChangeListener, ConfigChangeEvent configChangeEvent) {
432+
Set<String> interestedKeys = m_interestedKeys.get(configChangeListener);
433+
434+
if (interestedKeys == null || interestedKeys.isEmpty()) {
435+
return true; // no interested keys means interested in all keys
436+
}
437+
438+
for (String interestedKey : interestedKeys) {
439+
if (configChangeEvent.isChanged(interestedKey)) {
440+
return true;
441+
}
442+
}
443+
444+
return false;
445+
}
446+
418447
List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
419448
Properties current) {
420449
if (previous == null) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.ctrip.framework.apollo.internals;
2+
3+
import com.ctrip.framework.apollo.core.ConfigConsts;
4+
import com.ctrip.framework.apollo.core.enums.Env;
5+
import com.ctrip.framework.apollo.core.spi.MetaServerProvider;
6+
import com.ctrip.framework.foundation.Foundation;
7+
import com.google.common.base.Strings;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
public class DefaultMetaServerProvider implements MetaServerProvider {
12+
13+
public static final int ORDER = 0;
14+
private static final Logger logger = LoggerFactory.getLogger(DefaultMetaServerProvider.class);
15+
16+
private final String metaServerAddress;
17+
18+
public DefaultMetaServerProvider() {
19+
metaServerAddress = initMetaServerAddress();
20+
}
21+
22+
private String initMetaServerAddress() {
23+
// 1. Get from System Property
24+
String metaAddress = System.getProperty(ConfigConsts.APOLLO_META_KEY);
25+
if (Strings.isNullOrEmpty(metaAddress)) {
26+
// 2. Get from OS environment variable, which could not contain dot and is normally in UPPER case
27+
metaAddress = System.getenv("APOLLO_META");
28+
}
29+
if (Strings.isNullOrEmpty(metaAddress)) {
30+
// 3. Get from server.properties
31+
metaAddress = Foundation.server().getProperty(ConfigConsts.APOLLO_META_KEY, null);
32+
}
33+
if (Strings.isNullOrEmpty(metaAddress)) {
34+
// 4. Get from app.properties
35+
metaAddress = Foundation.app().getProperty(ConfigConsts.APOLLO_META_KEY, null);
36+
}
37+
38+
if (Strings.isNullOrEmpty(metaAddress)) {
39+
logger.warn("Could not find meta server address, because it is not available in neither (1) JVM system property 'apollo.meta', (2) OS env variable 'APOLLO_META' (3) property 'apollo.meta' from server.properties nor (4) property 'apollo.meta' from app.properties");
40+
} else {
41+
metaAddress = metaAddress.trim();
42+
logger.info("Located meta services from apollo.meta configuration: {}!", metaAddress);
43+
}
44+
45+
return metaAddress;
46+
}
47+
48+
@Override
49+
public String getMetaServerAddress(Env targetEnv) {
50+
//for default meta server provider, we don't care the actual environment
51+
return metaServerAddress;
52+
}
53+
54+
@Override
55+
public int getOrder() {
56+
return ORDER;
57+
}
58+
}

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import com.ctrip.framework.apollo.ConfigService;
66
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
77
import com.google.common.base.Preconditions;
8+
import com.google.common.collect.Sets;
89
import java.lang.reflect.Field;
910
import java.lang.reflect.Method;
11+
import java.util.Set;
1012
import org.springframework.core.annotation.AnnotationUtils;
1113
import org.springframework.util.ReflectionUtils;
1214

@@ -51,6 +53,8 @@ protected void processMethod(final Object bean, String beanName, final Method me
5153

5254
ReflectionUtils.makeAccessible(method);
5355
String[] namespaces = annotation.value();
56+
String[] annotatedInterestedKeys = annotation.interestedKeys();
57+
Set<String> interestedKeys = annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null;
5458
ConfigChangeListener configChangeListener = new ConfigChangeListener() {
5559
@Override
5660
public void onChange(ConfigChangeEvent changeEvent) {
@@ -61,7 +65,11 @@ public void onChange(ConfigChangeEvent changeEvent) {
6165
for (String namespace : namespaces) {
6266
Config config = ConfigService.getConfig(namespace);
6367

64-
config.addChangeListener(configChangeListener);
68+
if (interestedKeys == null) {
69+
config.addChangeListener(configChangeListener);
70+
} else {
71+
config.addChangeListener(configChangeListener, interestedKeys);
72+
}
6573
}
6674
}
6775
}

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloConfigChangeListener.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
*
1414
* <p>Usage example:</p>
1515
* <pre class="code">
16-
* //Listener on namespaces of "someNamespace" and "anotherNamespace"
16+
* //Listener on namespaces of "someNamespace" and "anotherNamespace", will be notified when any key is changed
1717
* &#064;ApolloConfigChangeListener({"someNamespace","anotherNamespace"})
1818
* private void onChange(ConfigChangeEvent changeEvent) {
1919
* //handle change event
2020
* }
21+
* <br />
22+
* //Listener on namespaces of "someNamespace" and "anotherNamespace", will only be notified when "someKey" or "anotherKey" is changed
23+
* &#064;ApolloConfigChangeListener(value = {"someNamespace","anotherNamespace"}, interestedKeys = {"someKey", "anotherKey"})
24+
* private void onChange(ConfigChangeEvent changeEvent) {
25+
* //handle change event
26+
* }
2127
* </pre>
2228
*
2329
* @author Jason Song(song_s@ctrip.com)
@@ -30,4 +36,11 @@
3036
* Apollo namespace for the config, if not specified then default to application
3137
*/
3238
String[] value() default {ConfigConsts.NAMESPACE_APPLICATION};
39+
40+
/**
41+
* The keys interested by the listener, will only be notified if any of the interested keys is changed.
42+
* <br />
43+
* If not specified then will be notified when any key is changed.
44+
*/
45+
String[] interestedKeys() default {};
3346
}

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.ctrip.framework.apollo.spring.config.PropertySourcesConstants;
88
import com.ctrip.framework.apollo.spring.util.SpringInjector;
99
import com.google.common.base.Splitter;
10+
import com.google.common.base.Strings;
1011
import java.util.List;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
@@ -16,17 +17,22 @@
1617
import org.springframework.core.env.ConfigurableEnvironment;
1718

1819
/**
19-
* Inject the Apollo config in Spring Boot bootstrap phase
20+
* Initialize apollo system properties and inject the Apollo config in Spring Boot bootstrap phase
2021
*
2122
* <p>Configuration example:</p>
2223
* <pre class="code">
23-
* # will inject 'application' namespace in bootstrap phase
24+
* # set app.id
25+
* app.id = 100004458
26+
* # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
2427
* apollo.bootstrap.enabled = true
2528
* </pre>
2629
*
2730
* or
2831
*
2932
* <pre class="code">
33+
* # set app.id
34+
* app.id = 100004458
35+
* # enable apollo bootstrap config
3036
* apollo.bootstrap.enabled = true
3137
* # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
3238
* apollo.bootstrap.namespaces = application,FX.apollo
@@ -36,13 +42,18 @@ public class ApolloApplicationContextInitializer implements
3642
ApplicationContextInitializer<ConfigurableApplicationContext> {
3743
private static final Logger logger = LoggerFactory.getLogger(ApolloApplicationContextInitializer.class);
3844
private static final Splitter NAMESPACE_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults();
45+
private static final String[] APOLLO_SYSTEM_PROPERTIES = {"app.id", ConfigConsts.APOLLO_CLUSTER_KEY,
46+
"apollo.cacheDir", ConfigConsts.APOLLO_META_KEY};
3947

4048
private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector
4149
.getInstance(ConfigPropertySourceFactory.class);
4250

4351
@Override
4452
public void initialize(ConfigurableApplicationContext context) {
4553
ConfigurableEnvironment environment = context.getEnvironment();
54+
55+
initializeSystemProperty(environment);
56+
4657
String enabled = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED, "false");
4758
if (!Boolean.valueOf(enabled)) {
4859
logger.debug("Apollo bootstrap config is not enabled for context {}, see property: ${{}}", context, PropertySourcesConstants.APOLLO_BOOTSTRAP_ENABLED);
@@ -68,4 +79,27 @@ public void initialize(ConfigurableApplicationContext context) {
6879

6980
environment.getPropertySources().addFirst(composite);
7081
}
82+
83+
/**
84+
* To fill system properties from environment config
85+
*/
86+
void initializeSystemProperty(ConfigurableEnvironment environment) {
87+
for (String propertyName : APOLLO_SYSTEM_PROPERTIES) {
88+
fillSystemPropertyFromEnvironment(environment, propertyName);
89+
}
90+
}
91+
92+
private void fillSystemPropertyFromEnvironment(ConfigurableEnvironment environment, String propertyName) {
93+
if (System.getProperty(propertyName) != null) {
94+
return;
95+
}
96+
97+
String propertyValue = environment.getProperty(propertyName);
98+
99+
if (Strings.isNullOrEmpty(propertyValue)) {
100+
return;
101+
}
102+
103+
System.setProperty(propertyName, propertyValue);
104+
}
71105
}

0 commit comments

Comments
 (0)