Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
ccc3afb4
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ccc3afb4
编写于
9月 02, 2018
作者:
N
nobodyiam
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support multiple spring contexts
上级
2a926ea2
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
73 addition
and
64 deletion
+73
-64
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloJsonValueProcessor.java
...rk/apollo/spring/annotation/ApolloJsonValueProcessor.java
+2
-2
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java
...mework/apollo/spring/annotation/SpringValueProcessor.java
+16
-8
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java
...mework/apollo/spring/config/PropertySourcesProcessor.java
+11
-14
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/AutoUpdateConfigChangeListener.java
...pollo/spring/property/AutoUpdateConfigChangeListener.java
+1
-1
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueDefinitionProcessor.java
...pollo/spring/property/SpringValueDefinitionProcessor.java
+21
-15
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java
...framework/apollo/spring/property/SpringValueRegistry.java
+22
-5
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java
...ramework/apollo/spring/AbstractSpringIntegrationTest.java
+0
-10
apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/EmbeddedApollo.java
...com/ctrip/framework/apollo/mockserver/EmbeddedApollo.java
+0
-9
未找到文件。
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloJsonValueProcessor.java
浏览文件 @
ccc3afb4
...
...
@@ -65,7 +65,7 @@ public class ApolloJsonValueProcessor extends ApolloProcessor implements BeanFac
Set
<
String
>
keys
=
placeholderHelper
.
extractPlaceholderKeys
(
placeholder
);
for
(
String
key
:
keys
)
{
SpringValue
springValue
=
new
SpringValue
(
key
,
placeholder
,
bean
,
beanName
,
field
,
true
);
springValueRegistry
.
register
(
key
,
springValue
);
springValueRegistry
.
register
(
beanFactory
,
key
,
springValue
);
logger
.
debug
(
"Monitoring {}"
,
springValue
);
}
}
...
...
@@ -102,7 +102,7 @@ public class ApolloJsonValueProcessor extends ApolloProcessor implements BeanFac
for
(
String
key
:
keys
)
{
SpringValue
springValue
=
new
SpringValue
(
key
,
apolloJsonValue
.
value
(),
bean
,
beanName
,
method
,
true
);
springValueRegistry
.
register
(
key
,
springValue
);
springValueRegistry
.
register
(
beanFactory
,
key
,
springValue
);
logger
.
debug
(
"Monitoring {}"
,
springValue
);
}
}
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java
浏览文件 @
ccc3afb4
...
...
@@ -19,9 +19,12 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactoryAware
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.config.BeanFactoryPostProcessor
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistry
;
import
org.springframework.context.annotation.Bean
;
/**
...
...
@@ -30,7 +33,7 @@ import org.springframework.context.annotation.Bean;
* @author github.com/zhegexiaohuozi seimimaster@gmail.com
* @since 2017/12/20.
*/
public
class
SpringValueProcessor
extends
ApolloProcessor
implements
BeanFactoryPostProcessor
{
public
class
SpringValueProcessor
extends
ApolloProcessor
implements
BeanFactoryPostProcessor
,
BeanFactoryAware
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
SpringValueProcessor
.
class
);
...
...
@@ -38,21 +41,22 @@ public class SpringValueProcessor extends ApolloProcessor implements BeanFactory
private
final
PlaceholderHelper
placeholderHelper
;
private
final
SpringValueRegistry
springValueRegistry
;
private
static
Multimap
<
String
,
SpringValueDefinition
>
beanName2SpringValueDefinitions
=
LinkedListMultimap
.
create
()
;
private
BeanFactory
beanFactory
;
private
Multimap
<
String
,
SpringValueDefinition
>
beanName2SpringValueDefinitions
;
public
SpringValueProcessor
()
{
configUtil
=
ApolloInjector
.
getInstance
(
ConfigUtil
.
class
);
placeholderHelper
=
SpringInjector
.
getInstance
(
PlaceholderHelper
.
class
);
springValueRegistry
=
SpringInjector
.
getInstance
(
SpringValueRegistry
.
class
);
beanName2SpringValueDefinitions
=
LinkedListMultimap
.
create
();
}
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
if
(
configUtil
.
isAutoUpdateInjectedSpringPropertiesEnabled
())
{
if
(
configUtil
.
isAutoUpdateInjectedSpringPropertiesEnabled
()
&&
beanFactory
instanceof
BeanDefinitionRegistry
)
{
beanName2SpringValueDefinitions
=
SpringValueDefinitionProcessor
.
getBeanName2SpringValueDefinitions
();
.
getBeanName2SpringValueDefinitions
(
(
BeanDefinitionRegistry
)
beanFactory
);
}
}
...
...
@@ -82,7 +86,7 @@ public class SpringValueProcessor extends ApolloProcessor implements BeanFactory
for
(
String
key
:
keys
)
{
SpringValue
springValue
=
new
SpringValue
(
key
,
value
.
value
(),
bean
,
beanName
,
field
,
false
);
springValueRegistry
.
register
(
key
,
springValue
);
springValueRegistry
.
register
(
beanFactory
,
key
,
springValue
);
logger
.
debug
(
"Monitoring {}"
,
springValue
);
}
}
...
...
@@ -112,7 +116,7 @@ public class SpringValueProcessor extends ApolloProcessor implements BeanFactory
for
(
String
key
:
keys
)
{
SpringValue
springValue
=
new
SpringValue
(
key
,
value
.
value
(),
bean
,
beanName
,
method
,
false
);
springValueRegistry
.
register
(
key
,
springValue
);
springValueRegistry
.
register
(
beanFactory
,
key
,
springValue
);
logger
.
info
(
"Monitoring {}"
,
springValue
);
}
}
...
...
@@ -135,7 +139,7 @@ public class SpringValueProcessor extends ApolloProcessor implements BeanFactory
}
SpringValue
springValue
=
new
SpringValue
(
definition
.
getKey
(),
definition
.
getPlaceholder
(),
bean
,
beanName
,
method
,
false
);
springValueRegistry
.
register
(
definition
.
getKey
(),
springValue
);
springValueRegistry
.
register
(
beanFactory
,
definition
.
getKey
(),
springValue
);
logger
.
debug
(
"Monitoring {}"
,
springValue
);
}
catch
(
Throwable
ex
)
{
logger
.
error
(
"Failed to enable auto update feature for {}.{}"
,
bean
.
getClass
(),
...
...
@@ -147,4 +151,8 @@ public class SpringValueProcessor extends ApolloProcessor implements BeanFactory
beanName2SpringValueDefinitions
.
removeAll
(
beanName
);
}
@Override
public
void
setBeanFactory
(
BeanFactory
beanFactory
)
throws
BeansException
{
this
.
beanFactory
=
beanFactory
;
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/config/PropertySourcesProcessor.java
浏览文件 @
ccc3afb4
...
...
@@ -11,9 +11,11 @@ import com.google.common.collect.Multimap;
import
com.ctrip.framework.apollo.Config
;
import
com.ctrip.framework.apollo.ConfigService
;
import
com.google.common.collect.Sets
;
import
java.util.List
;
import
java.util.
concurrent.atomic.AtomicBoolean
;
import
java.util.
Set
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.config.BeanFactoryPostProcessor
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.context.EnvironmentAware
;
...
...
@@ -40,7 +42,7 @@ import org.springframework.core.env.PropertySource;
*/
public
class
PropertySourcesProcessor
implements
BeanFactoryPostProcessor
,
EnvironmentAware
,
PriorityOrdered
{
private
static
final
Multimap
<
Integer
,
String
>
NAMESPACE_NAMES
=
LinkedHashMultimap
.
create
();
private
static
final
AtomicBoolean
INITIALIZED
=
new
AtomicBoolean
(
false
);
private
static
final
Set
<
BeanFactory
>
AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES
=
Sets
.
newConcurrentHashSet
(
);
private
final
ConfigPropertySourceFactory
configPropertySourceFactory
=
SpringInjector
.
getInstance
(
ConfigPropertySourceFactory
.
class
);
...
...
@@ -53,11 +55,8 @@ public class PropertySourcesProcessor implements BeanFactoryPostProcessor, Envir
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
if
(
INITIALIZED
.
compareAndSet
(
false
,
true
))
{
initializePropertySources
();
initializeAutoUpdatePropertiesFeature
(
beanFactory
);
}
initializePropertySources
();
initializeAutoUpdatePropertiesFeature
(
beanFactory
);
}
private
void
initializePropertySources
()
{
...
...
@@ -80,6 +79,9 @@ public class PropertySourcesProcessor implements BeanFactoryPostProcessor, Envir
}
}
// clean up
NAMESPACE_NAMES
.
clear
();
// add after the bootstrap property source or to the first
if
(
environment
.
getPropertySources
()
.
contains
(
PropertySourcesConstants
.
APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME
))
{
...
...
@@ -110,7 +112,8 @@ public class PropertySourcesProcessor implements BeanFactoryPostProcessor, Envir
}
private
void
initializeAutoUpdatePropertiesFeature
(
ConfigurableListableBeanFactory
beanFactory
)
{
if
(!
configUtil
.
isAutoUpdateInjectedSpringPropertiesEnabled
())
{
if
(!
configUtil
.
isAutoUpdateInjectedSpringPropertiesEnabled
()
||
!
AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES
.
add
(
beanFactory
))
{
return
;
}
...
...
@@ -129,12 +132,6 @@ public class PropertySourcesProcessor implements BeanFactoryPostProcessor, Envir
this
.
environment
=
(
ConfigurableEnvironment
)
environment
;
}
//only for test
private
static
void
reset
()
{
NAMESPACE_NAMES
.
clear
();
INITIALIZED
.
set
(
false
);
}
@Override
public
int
getOrder
()
{
//make it as early as possible
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/AutoUpdateConfigChangeListener.java
浏览文件 @
ccc3afb4
...
...
@@ -51,7 +51,7 @@ public class AutoUpdateConfigChangeListener implements ConfigChangeListener{
}
for
(
String
key
:
keys
)
{
// 1. check whether the changed key is relevant
Collection
<
SpringValue
>
targetValues
=
springValueRegistry
.
get
(
key
);
Collection
<
SpringValue
>
targetValues
=
springValueRegistry
.
get
(
beanFactory
,
key
);
if
(
targetValues
==
null
||
targetValues
.
isEmpty
())
{
continue
;
}
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueDefinitionProcessor.java
浏览文件 @
ccc3afb4
package
com.ctrip.framework.apollo.spring.property
;
import
com.ctrip.framework.apollo.spring.util.SpringInjector
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Sets
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.atomic.AtomicBoolean
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.MutablePropertyValues
;
import
org.springframework.beans.PropertyValue
;
...
...
@@ -30,9 +32,9 @@ import com.google.common.collect.Multimap;
* </pre>
*/
public
class
SpringValueDefinitionProcessor
implements
BeanDefinitionRegistryPostProcessor
{
private
static
final
M
ultimap
<
String
,
SpringValueDefinition
>
beanName2SpringValueDefinitions
=
LinkedListMultimap
.
create
();
private
static
final
AtomicBoolean
initialized
=
new
AtomicBoolean
(
false
);
private
static
final
M
ap
<
BeanDefinitionRegistry
,
Multimap
<
String
,
SpringValueDefinition
>
>
beanName2SpringValueDefinitions
=
Maps
.
newConcurrentMap
();
private
static
final
Set
<
BeanDefinitionRegistry
>
PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES
=
Sets
.
newConcurrentHashSet
(
);
private
final
ConfigUtil
configUtil
;
private
final
PlaceholderHelper
placeholderHelper
;
...
...
@@ -54,16 +56,27 @@ public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPos
}
public
static
Multimap
<
String
,
SpringValueDefinition
>
getBeanName2SpringValueDefinitions
()
{
return
beanName2SpringValueDefinitions
;
public
static
Multimap
<
String
,
SpringValueDefinition
>
getBeanName2SpringValueDefinitions
(
BeanDefinitionRegistry
registry
)
{
Multimap
<
String
,
SpringValueDefinition
>
springValueDefinitions
=
beanName2SpringValueDefinitions
.
get
(
registry
);
if
(
springValueDefinitions
==
null
)
{
springValueDefinitions
=
LinkedListMultimap
.
create
();
}
return
springValueDefinitions
;
}
private
void
processPropertyValues
(
BeanDefinitionRegistry
beanRegistry
)
{
if
(!
initialized
.
compareAndSet
(
false
,
true
))
{
if
(!
PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES
.
add
(
beanRegistry
))
{
// already initialized
return
;
}
if
(!
beanName2SpringValueDefinitions
.
containsKey
(
beanRegistry
))
{
beanName2SpringValueDefinitions
.
put
(
beanRegistry
,
LinkedListMultimap
.<
String
,
SpringValueDefinition
>
create
());
}
Multimap
<
String
,
SpringValueDefinition
>
springValueDefinitions
=
beanName2SpringValueDefinitions
.
get
(
beanRegistry
);
String
[]
beanNames
=
beanRegistry
.
getBeanDefinitionNames
();
for
(
String
beanName
:
beanNames
)
{
BeanDefinition
beanDefinition
=
beanRegistry
.
getBeanDefinition
(
beanName
);
...
...
@@ -82,16 +95,9 @@ public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPos
}
for
(
String
key
:
keys
)
{
beanName2SpringValueDefinitions
.
put
(
beanName
,
new
SpringValueDefinition
(
key
,
placeholder
,
propertyValue
.
getName
()));
springValueDefinitions
.
put
(
beanName
,
new
SpringValueDefinition
(
key
,
placeholder
,
propertyValue
.
getName
()));
}
}
}
}
//only for test
private
static
void
reset
()
{
initialized
.
set
(
false
);
beanName2SpringValueDefinitions
.
clear
();
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/property/SpringValueRegistry.java
浏览文件 @
ccc3afb4
package
com.ctrip.framework.apollo.spring.property
;
import
com.google.common.collect.LinkedListMultimap
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Multimap
;
import
java.util.Collection
;
import
java.util.Map
;
import
org.springframework.beans.factory.BeanFactory
;
public
class
SpringValueRegistry
{
private
final
Multimap
<
String
,
SpringValue
>
registry
=
LinkedListMultimap
.
create
();
public
void
register
(
String
key
,
SpringValue
springValue
)
{
registry
.
put
(
key
,
springValue
);
private
final
Map
<
BeanFactory
,
Multimap
<
String
,
SpringValue
>>
registry
=
Maps
.
newConcurrentMap
();
private
final
Object
LOCK
=
new
Object
();
public
void
register
(
BeanFactory
beanFactory
,
String
key
,
SpringValue
springValue
)
{
if
(!
registry
.
containsKey
(
beanFactory
))
{
synchronized
(
LOCK
)
{
if
(!
registry
.
containsKey
(
beanFactory
))
{
registry
.
put
(
beanFactory
,
LinkedListMultimap
.<
String
,
SpringValue
>
create
());
}
}
}
registry
.
get
(
beanFactory
).
put
(
key
,
springValue
);
}
public
Collection
<
SpringValue
>
get
(
String
key
)
{
return
registry
.
get
(
key
);
public
Collection
<
SpringValue
>
get
(
BeanFactory
beanFactory
,
String
key
)
{
Multimap
<
String
,
SpringValue
>
beanFactorySpringValues
=
registry
.
get
(
beanFactory
);
if
(
beanFactorySpringValues
==
null
)
{
return
null
;
}
return
beanFactorySpringValues
.
get
(
key
);
}
}
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/AbstractSpringIntegrationTest.java
浏览文件 @
ccc3afb4
...
...
@@ -33,16 +33,10 @@ import com.google.common.collect.Maps;
*/
public
abstract
class
AbstractSpringIntegrationTest
{
private
static
final
Map
<
String
,
Config
>
CONFIG_REGISTRY
=
Maps
.
newHashMap
();
private
static
Method
PROPERTY_SOURCES_PROCESSOR_CLEAR
;
private
static
Method
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
;
private
static
Method
CONFIG_SERVICE_RESET
;
static
{
try
{
PROPERTY_SOURCES_PROCESSOR_CLEAR
=
PropertySourcesProcessor
.
class
.
getDeclaredMethod
(
"reset"
);
ReflectionUtils
.
makeAccessible
(
PROPERTY_SOURCES_PROCESSOR_CLEAR
);
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
=
SpringValueDefinitionProcessor
.
class
.
getDeclaredMethod
(
"reset"
);
ReflectionUtils
.
makeAccessible
(
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
);
CONFIG_SERVICE_RESET
=
ConfigService
.
class
.
getDeclaredMethod
(
"reset"
);
ReflectionUtils
.
makeAccessible
(
CONFIG_SERVICE_RESET
);
}
catch
(
NoSuchMethodException
e
)
{
...
...
@@ -112,10 +106,6 @@ public abstract class AbstractSpringIntegrationTest {
}
protected
static
void
doSetUp
()
{
//as PropertySourcesProcessor has some static states, so we must manually clear its state
ReflectionUtils
.
invokeMethod
(
PROPERTY_SOURCES_PROCESSOR_CLEAR
,
null
);
//as SpringValueDefinitionProcessor has some static states, so we must manually clear its state
ReflectionUtils
.
invokeMethod
(
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
,
null
);
//as ConfigService is singleton, so we must manually clear its container
ReflectionUtils
.
invokeMethod
(
CONFIG_SERVICE_RESET
,
null
);
MockInjector
.
reset
();
...
...
apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/EmbeddedApollo.java
浏览文件 @
ccc3afb4
...
...
@@ -37,8 +37,6 @@ public class EmbeddedApollo extends ExternalResource {
private
static
final
Type
notificationType
=
new
TypeToken
<
List
<
ApolloConfigNotification
>>()
{
}.
getType
();
private
static
Method
PROPERTY_SOURCES_PROCESSOR_CLEAR
;
private
static
Method
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
;
private
static
Method
CONFIG_SERVICE_LOCATOR_CLEAR
;
private
static
ConfigServiceLocator
CONFIG_SERVICE_LOCATOR
;
...
...
@@ -51,10 +49,6 @@ public class EmbeddedApollo extends ExternalResource {
static
{
try
{
System
.
setProperty
(
"apollo.longPollingInitialDelayInMills"
,
"0"
);
PROPERTY_SOURCES_PROCESSOR_CLEAR
=
PropertySourcesProcessor
.
class
.
getDeclaredMethod
(
"reset"
);
PROPERTY_SOURCES_PROCESSOR_CLEAR
.
setAccessible
(
true
);
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
=
SpringValueDefinitionProcessor
.
class
.
getDeclaredMethod
(
"reset"
);
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
.
setAccessible
(
true
);
CONFIG_SERVICE_LOCATOR
=
ApolloInjector
.
getInstance
(
ConfigServiceLocator
.
class
);
CONFIG_SERVICE_LOCATOR_CLEAR
=
ConfigServiceLocator
.
class
.
getDeclaredMethod
(
"initConfigServices"
);
CONFIG_SERVICE_LOCATOR_CLEAR
.
setAccessible
(
true
);
...
...
@@ -105,9 +99,6 @@ public class EmbeddedApollo extends ExternalResource {
private
void
clear
()
throws
Exception
{
resetOverriddenProperties
();
// clear Apollo states
PROPERTY_SOURCES_PROCESSOR_CLEAR
.
invoke
(
null
);
SPRING_VALUE_DEFINITION_PROCESS_CLEAR
.
invoke
(
null
);
}
private
void
mockConfigServiceUrl
(
String
url
)
throws
Exception
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录