Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
apollo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
apollo
Commits
eea20083
Unverified
Commit
eea20083
authored
Aug 06, 2018
by
Jason Song
Committed by
GitHub
Aug 06, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1336 from nobodyiam/1.x
support setting apollo system properties via application.properties
parents
3ee37488
5258ba11
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
131 additions
and
5 deletions
+131
-5
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java
...ollo/spring/boot/ApolloApplicationContextInitializer.java
+36
-2
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java
.../spring/boot/ApolloApplicationContextInitializerTest.java
+95
-0
apollo-core/src/main/java/com/ctrip/framework/foundation/internals/provider/DefaultApplicationProvider.java
...dation/internals/provider/DefaultApplicationProvider.java
+0
-3
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializer.java
View file @
eea20083
...
@@ -7,6 +7,7 @@ import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory;
...
@@ -7,6 +7,7 @@ import com.ctrip.framework.apollo.spring.config.ConfigPropertySourceFactory;
import
com.ctrip.framework.apollo.spring.config.PropertySourcesConstants
;
import
com.ctrip.framework.apollo.spring.config.PropertySourcesConstants
;
import
com.ctrip.framework.apollo.spring.util.SpringInjector
;
import
com.ctrip.framework.apollo.spring.util.SpringInjector
;
import
com.google.common.base.Splitter
;
import
com.google.common.base.Splitter
;
import
com.google.common.base.Strings
;
import
java.util.List
;
import
java.util.List
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -16,17 +17,22 @@ import org.springframework.core.env.CompositePropertySource;
...
@@ -16,17 +17,22 @@ import org.springframework.core.env.CompositePropertySource;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.ConfigurableEnvironment
;
/**
/**
* Inject the Apollo config in Spring Boot bootstrap phase
* In
itialize apollo system properties and in
ject the Apollo config in Spring Boot bootstrap phase
*
*
* <p>Configuration example:</p>
* <p>Configuration example:</p>
* <pre class="code">
* <pre class="code">
* # will inject 'application' namespace in bootstrap phase
* # set app.id
* app.id = 100004458
* # enable apollo bootstrap config and inject 'application' namespace in bootstrap phase
* apollo.bootstrap.enabled = true
* apollo.bootstrap.enabled = true
* </pre>
* </pre>
*
*
* or
* or
*
*
* <pre class="code">
* <pre class="code">
* # set app.id
* app.id = 100004458
* # enable apollo bootstrap config
* apollo.bootstrap.enabled = true
* apollo.bootstrap.enabled = true
* # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
* # will inject 'application' and 'FX.apollo' namespaces in bootstrap phase
* apollo.bootstrap.namespaces = application,FX.apollo
* apollo.bootstrap.namespaces = application,FX.apollo
...
@@ -36,6 +42,8 @@ public class ApolloApplicationContextInitializer implements
...
@@ -36,6 +42,8 @@ public class ApolloApplicationContextInitializer implements
ApplicationContextInitializer
<
ConfigurableApplicationContext
>
{
ApplicationContextInitializer
<
ConfigurableApplicationContext
>
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ApolloApplicationContextInitializer
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ApolloApplicationContextInitializer
.
class
);
private
static
final
Splitter
NAMESPACE_SPLITTER
=
Splitter
.
on
(
","
).
omitEmptyStrings
().
trimResults
();
private
static
final
Splitter
NAMESPACE_SPLITTER
=
Splitter
.
on
(
","
).
omitEmptyStrings
().
trimResults
();
private
static
final
String
[]
APOLLO_SYSTEM_PROPERTIES
=
{
"app.id"
,
ConfigConsts
.
APOLLO_CLUSTER_KEY
,
"apollo.cacheDir"
,
ConfigConsts
.
APOLLO_META_KEY
};
private
final
ConfigPropertySourceFactory
configPropertySourceFactory
=
SpringInjector
private
final
ConfigPropertySourceFactory
configPropertySourceFactory
=
SpringInjector
.
getInstance
(
ConfigPropertySourceFactory
.
class
);
.
getInstance
(
ConfigPropertySourceFactory
.
class
);
...
@@ -43,6 +51,9 @@ public class ApolloApplicationContextInitializer implements
...
@@ -43,6 +51,9 @@ public class ApolloApplicationContextInitializer implements
@Override
@Override
public
void
initialize
(
ConfigurableApplicationContext
context
)
{
public
void
initialize
(
ConfigurableApplicationContext
context
)
{
ConfigurableEnvironment
environment
=
context
.
getEnvironment
();
ConfigurableEnvironment
environment
=
context
.
getEnvironment
();
initializeSystemProperty
(
environment
);
String
enabled
=
environment
.
getProperty
(
PropertySourcesConstants
.
APOLLO_BOOTSTRAP_ENABLED
,
"false"
);
String
enabled
=
environment
.
getProperty
(
PropertySourcesConstants
.
APOLLO_BOOTSTRAP_ENABLED
,
"false"
);
if
(!
Boolean
.
valueOf
(
enabled
))
{
if
(!
Boolean
.
valueOf
(
enabled
))
{
logger
.
debug
(
"Apollo bootstrap config is not enabled for context {}, see property: ${{}}"
,
context
,
PropertySourcesConstants
.
APOLLO_BOOTSTRAP_ENABLED
);
logger
.
debug
(
"Apollo bootstrap config is not enabled for context {}, see property: ${{}}"
,
context
,
PropertySourcesConstants
.
APOLLO_BOOTSTRAP_ENABLED
);
...
@@ -68,4 +79,27 @@ public class ApolloApplicationContextInitializer implements
...
@@ -68,4 +79,27 @@ public class ApolloApplicationContextInitializer implements
environment
.
getPropertySources
().
addFirst
(
composite
);
environment
.
getPropertySources
().
addFirst
(
composite
);
}
}
/**
* To fill system properties from environment config
*/
void
initializeSystemProperty
(
ConfigurableEnvironment
environment
)
{
for
(
String
propertyName
:
APOLLO_SYSTEM_PROPERTIES
)
{
fillSystemPropertyFromEnvironment
(
environment
,
propertyName
);
}
}
private
void
fillSystemPropertyFromEnvironment
(
ConfigurableEnvironment
environment
,
String
propertyName
)
{
if
(
System
.
getProperty
(
propertyName
)
!=
null
)
{
return
;
}
String
propertyValue
=
environment
.
getProperty
(
propertyName
);
if
(
Strings
.
isNullOrEmpty
(
propertyValue
))
{
return
;
}
System
.
setProperty
(
propertyName
,
propertyValue
);
}
}
}
apollo-client/src/test/java/com/ctrip/framework/apollo/spring/boot/ApolloApplicationContextInitializerTest.java
0 → 100644
View file @
eea20083
package
com
.
ctrip
.
framework
.
apollo
.
spring
.
boot
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.framework.apollo.core.ConfigConsts
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.core.env.ConfigurableEnvironment
;
public
class
ApolloApplicationContextInitializerTest
{
private
ApolloApplicationContextInitializer
apolloApplicationContextInitializer
;
@Before
public
void
setUp
()
throws
Exception
{
apolloApplicationContextInitializer
=
new
ApolloApplicationContextInitializer
();
}
@After
public
void
tearDown
()
throws
Exception
{
System
.
clearProperty
(
"app.id"
);
System
.
clearProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
);
System
.
clearProperty
(
"apollo.cacheDir"
);
System
.
clearProperty
(
ConfigConsts
.
APOLLO_META_KEY
);
}
@Test
public
void
testFillFromEnvironment
()
throws
Exception
{
String
someAppId
=
"someAppId"
;
String
someCluster
=
"someCluster"
;
String
someCacheDir
=
"someCacheDir"
;
String
someApolloMeta
=
"someApolloMeta"
;
ConfigurableEnvironment
environment
=
mock
(
ConfigurableEnvironment
.
class
);
when
(
environment
.
getProperty
(
"app.id"
)).
thenReturn
(
someAppId
);
when
(
environment
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
)).
thenReturn
(
someCluster
);
when
(
environment
.
getProperty
(
"apollo.cacheDir"
)).
thenReturn
(
someCacheDir
);
when
(
environment
.
getProperty
(
ConfigConsts
.
APOLLO_META_KEY
)).
thenReturn
(
someApolloMeta
);
apolloApplicationContextInitializer
.
initializeSystemProperty
(
environment
);
assertEquals
(
someAppId
,
System
.
getProperty
(
"app.id"
));
assertEquals
(
someCluster
,
System
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
));
assertEquals
(
someCacheDir
,
System
.
getProperty
(
"apollo.cacheDir"
));
assertEquals
(
someApolloMeta
,
System
.
getProperty
(
ConfigConsts
.
APOLLO_META_KEY
));
}
@Test
public
void
testFillFromEnvironmentWithSystemPropertyAlreadyFilled
()
throws
Exception
{
String
someAppId
=
"someAppId"
;
String
someCluster
=
"someCluster"
;
String
someCacheDir
=
"someCacheDir"
;
String
someApolloMeta
=
"someApolloMeta"
;
System
.
setProperty
(
"app.id"
,
someAppId
);
System
.
setProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
,
someCluster
);
System
.
setProperty
(
"apollo.cacheDir"
,
someCacheDir
);
System
.
setProperty
(
ConfigConsts
.
APOLLO_META_KEY
,
someApolloMeta
);
String
anotherAppId
=
"anotherAppId"
;
String
anotherCluster
=
"anotherCluster"
;
String
anotherCacheDir
=
"anotherCacheDir"
;
String
anotherApolloMeta
=
"anotherApolloMeta"
;
ConfigurableEnvironment
environment
=
mock
(
ConfigurableEnvironment
.
class
);
when
(
environment
.
getProperty
(
"app.id"
)).
thenReturn
(
anotherAppId
);
when
(
environment
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
)).
thenReturn
(
anotherCluster
);
when
(
environment
.
getProperty
(
"apollo.cacheDir"
)).
thenReturn
(
anotherCacheDir
);
when
(
environment
.
getProperty
(
ConfigConsts
.
APOLLO_META_KEY
)).
thenReturn
(
anotherApolloMeta
);
apolloApplicationContextInitializer
.
initializeSystemProperty
(
environment
);
assertEquals
(
someAppId
,
System
.
getProperty
(
"app.id"
));
assertEquals
(
someCluster
,
System
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
));
assertEquals
(
someCacheDir
,
System
.
getProperty
(
"apollo.cacheDir"
));
assertEquals
(
someApolloMeta
,
System
.
getProperty
(
ConfigConsts
.
APOLLO_META_KEY
));
}
@Test
public
void
testFillFromEnvironmentWithNoPropertyFromEnvironment
()
throws
Exception
{
ConfigurableEnvironment
environment
=
mock
(
ConfigurableEnvironment
.
class
);
apolloApplicationContextInitializer
.
initializeSystemProperty
(
environment
);
assertNull
(
System
.
getProperty
(
"app.id"
));
assertNull
(
System
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
));
assertNull
(
System
.
getProperty
(
"apollo.cacheDir"
));
assertNull
(
System
.
getProperty
(
ConfigConsts
.
APOLLO_META_KEY
));
}
}
apollo-core/src/main/java/com/ctrip/framework/foundation/internals/provider/DefaultApplicationProvider.java
View file @
eea20083
...
@@ -28,9 +28,6 @@ public class DefaultApplicationProvider implements ApplicationProvider {
...
@@ -28,9 +28,6 @@ public class DefaultApplicationProvider implements ApplicationProvider {
in
=
DefaultApplicationProvider
.
class
.
getResourceAsStream
(
APP_PROPERTIES_CLASSPATH
);
in
=
DefaultApplicationProvider
.
class
.
getResourceAsStream
(
APP_PROPERTIES_CLASSPATH
);
}
}
if
(
in
==
null
)
{
logger
.
warn
(
"{} not found from classpath!"
,
APP_PROPERTIES_CLASSPATH
);
}
initialize
(
in
);
initialize
(
in
);
}
catch
(
Throwable
ex
)
{
}
catch
(
Throwable
ex
)
{
logger
.
error
(
"Initialize DefaultApplicationProvider failed."
,
ex
);
logger
.
error
(
"Initialize DefaultApplicationProvider failed."
,
ex
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment