Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
openSource
apollo
Commits
886406b9
Commit
886406b9
authored
Mar 28, 2016
by
Jason Song
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #51 from yiming187/conf_update
Extract domain conf into files
parents
c9fc5b11
fd110337
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
97 additions
and
85 deletions
+97
-85
ClientEnvironment.java
...n/java/com/ctrip/apollo/client/env/ClientEnvironment.java
+2
-51
MetaDomainConsts.java
...src/main/java/com/ctrip/apollo/core/MetaDomainConsts.java
+14
-34
ResourceUtils.java
.../main/java/com/ctrip/apollo/core/utils/ResourceUtils.java
+62
-0
apollo-env.properties
apollo-core/src/main/resources/apollo-env.properties
+2
-0
MetaDomainTest.java
...e/src/test/java/com/ctrip/apollo/core/MetaDomainTest.java
+15
-0
apollo-env.properties
apollo-core/src/test/resources/apollo-env.properties
+2
-0
No files found.
apollo-client/src/main/java/com/ctrip/apollo/client/env/ClientEnvironment.java
View file @
886406b9
...
...
@@ -4,18 +4,12 @@ import com.ctrip.apollo.Apollo;
import
com.ctrip.apollo.Apollo.Env
;
import
com.ctrip.apollo.client.constants.Constants
;
import
com.ctrip.apollo.core.MetaDomainConsts
;
import
com.ctrip.apollo.core.utils.ResourceUtils
;
import
com.ctrip.apollo.core.utils.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.util.Enumeration
;
import
java.util.Properties
;
import
java.util.concurrent.atomic.AtomicReference
;
...
...
@@ -41,7 +35,7 @@ public class ClientEnvironment {
if
(
env
.
get
()
==
null
)
{
Env
resultEnv
=
Apollo
.
getEnv
();
Properties
apolloProperties
=
null
;
apolloProperties
=
readConfigFile
(
DEFAULT_FILE
,
null
);
apolloProperties
=
ResourceUtils
.
readConfigFile
(
DEFAULT_FILE
,
null
);
if
(
apolloProperties
!=
null
)
{
String
strEnv
=
apolloProperties
.
getProperty
(
Constants
.
ENV
);
if
(!
StringUtils
.
isBlank
(
strEnv
))
{
...
...
@@ -62,47 +56,4 @@ public class ClientEnvironment {
return
MetaDomainConsts
.
getDomain
(
getEnv
());
}
@SuppressWarnings
(
"unchecked"
)
private
Properties
readConfigFile
(
String
configPath
,
Properties
defaults
)
{
InputStream
in
=
this
.
getClass
().
getResourceAsStream
(
configPath
);
logger
.
info
(
"Reading config from resource {}"
,
configPath
);
Properties
props
=
new
Properties
();
try
{
if
(
in
==
null
)
{
// load outside resource under current user path
Path
path
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
configPath
).
toPath
();
if
(
Files
.
isReadable
(
path
))
{
in
=
new
FileInputStream
(
path
.
toFile
());
logger
.
info
(
"Reading config from file {} "
,
path
);
}
}
if
(
defaults
!=
null
)
{
props
.
putAll
(
defaults
);
}
if
(
in
!=
null
)
{
props
.
load
(
in
);
in
.
close
();
}
}
catch
(
Exception
ex
)
{
logger
.
warn
(
"Reading config failed: {}"
,
ex
.
getMessage
());
}
finally
{
if
(
in
!=
null
)
{
try
{
in
.
close
();
}
catch
(
IOException
ex
)
{
logger
.
warn
(
"Close config failed: {}"
,
ex
.
getMessage
());
}
}
}
StringBuilder
sb
=
new
StringBuilder
();
for
(
Enumeration
<
String
>
e
=
(
Enumeration
<
String
>)
props
.
propertyNames
();
e
.
hasMoreElements
();
)
{
String
key
=
e
.
nextElement
();
String
val
=
(
String
)
props
.
getProperty
(
key
);
sb
.
append
(
key
).
append
(
'='
).
append
(
val
).
append
(
'\n'
);
}
logger
.
info
(
"Reading properties: \n"
+
sb
.
toString
());
return
props
;
}
}
apollo-core/src/main/java/com/ctrip/apollo/core/MetaDomainConsts.java
View file @
886406b9
package
com
.
ctrip
.
apollo
.
core
;
import
com.ctrip.apollo.Apollo.Env
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Properties
;
public
class
MetaDomainConsts
{
public
static
final
String
DEFAULT_PORT
=
"8080"
;
public
static
final
String
LOCAL
=
"http://localhost"
+
":"
+
DEFAULT_PORT
;
public
static
final
String
DEV
=
"http://10.3.2.56"
+
":"
+
DEFAULT_PORT
;
public
static
final
String
FAT
=
"http://ws.meta.apollo.fx.fat.nt.ctripcorp.com"
+
":"
+
DEFAULT_PORT
;
public
static
final
String
FWS
=
"http://ws.meta.apollo.fx.fws.nt.ctripcorp.com"
+
":"
+
DEFAULT_PORT
;
public
static
final
String
UAT
=
"http://ws.meta.apollo.fx.uat.nt.ctripcorp.com"
+
":"
+
DEFAULT_PORT
;
public
static
final
String
LPT
=
"http://ws.meta.apollo.fx.lpt.nt.ctripcorp.com"
+
":"
+
DEFAULT_PORT
;
public
static
final
String
TOOLS
=
"http://ws.meta.apollo.fx.tools.ctripcorp.com"
+
":"
+
DEFAULT_PORT
;
import
com.ctrip.apollo.Apollo.Env
;
import
com.ctrip.apollo.core.utils.ResourceUtils
;
public
static
final
String
PRO
=
"http://ws.meta.apollo.fx.ctripcorp.com"
+
":"
+
DEFAULT_PORT
;
public
class
MetaDomainConsts
{
private
static
Map
<
Env
,
String
>
domains
=
new
HashMap
<>();
static
{
domains
.
put
(
Env
.
LOCAL
,
LOCAL
);
domains
.
put
(
Env
.
DEV
,
DEV
);
domains
.
put
(
Env
.
FAT
,
FAT
);
domains
.
put
(
Env
.
FWS
,
FWS
);
domains
.
put
(
Env
.
UAT
,
UAT
);
domains
.
put
(
Env
.
LPT
,
LPT
);
domains
.
put
(
Env
.
TOOLS
,
TOOLS
);
domains
.
put
(
Env
.
PRO
,
PRO
);
Properties
prop
=
new
Properties
();
prop
=
ResourceUtils
.
readConfigFile
(
"apollo-env.properties"
,
prop
);
domains
.
put
(
Env
.
LOCAL
,
prop
.
getProperty
(
"local.meta"
,
"http://localhost:8080"
));
domains
.
put
(
Env
.
DEV
,
prop
.
getProperty
(
"dev.meta"
));
domains
.
put
(
Env
.
FAT
,
prop
.
getProperty
(
"fat.meta"
));
domains
.
put
(
Env
.
FWS
,
prop
.
getProperty
(
"fws.meta"
));
domains
.
put
(
Env
.
UAT
,
prop
.
getProperty
(
"uat.meta"
));
domains
.
put
(
Env
.
LPT
,
prop
.
getProperty
(
"lpt.meta"
));
domains
.
put
(
Env
.
TOOLS
,
prop
.
getProperty
(
"tools.meta"
));
domains
.
put
(
Env
.
PRO
,
prop
.
getProperty
(
"pro.meta"
));
}
public
static
String
getDomain
(
Env
env
)
{
...
...
apollo-core/src/main/java/com/ctrip/apollo/core/utils/ResourceUtils.java
0 → 100644
View file @
886406b9
package
com
.
ctrip
.
apollo
.
core
.
utils
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.util.Enumeration
;
import
java.util.Properties
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
public
class
ResourceUtils
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ResourceUtils
.
class
);
@SuppressWarnings
(
"unchecked"
)
public
static
Properties
readConfigFile
(
String
configPath
,
Properties
defaults
)
{
InputStream
in
=
ClassLoader
.
getSystemResourceAsStream
(
configPath
);
logger
.
info
(
"Reading config from resource {}"
,
configPath
);
Properties
props
=
new
Properties
();
try
{
if
(
in
==
null
)
{
// load outside resource under current user path
Path
path
=
new
File
(
System
.
getProperty
(
"user.dir"
)
+
configPath
).
toPath
();
if
(
Files
.
isReadable
(
path
))
{
in
=
new
FileInputStream
(
path
.
toFile
());
logger
.
info
(
"Reading config from file {} "
,
path
);
}
}
if
(
defaults
!=
null
)
{
props
.
putAll
(
defaults
);
}
if
(
in
!=
null
)
{
props
.
load
(
in
);
in
.
close
();
}
}
catch
(
Exception
ex
)
{
logger
.
warn
(
"Reading config failed: {}"
,
ex
.
getMessage
());
}
finally
{
if
(
in
!=
null
)
{
try
{
in
.
close
();
}
catch
(
IOException
ex
)
{
logger
.
warn
(
"Close config failed: {}"
,
ex
.
getMessage
());
}
}
}
StringBuilder
sb
=
new
StringBuilder
();
for
(
Enumeration
<
String
>
e
=
(
Enumeration
<
String
>)
props
.
propertyNames
();
e
.
hasMoreElements
();
)
{
String
key
=
e
.
nextElement
();
String
val
=
(
String
)
props
.
getProperty
(
key
);
sb
.
append
(
key
).
append
(
'='
).
append
(
val
).
append
(
'\n'
);
}
logger
.
info
(
"Reading properties: \n"
+
sb
.
toString
());
return
props
;
}
}
apollo-core/src/main/resources/apollo-env.properties
0 → 100644
View file @
886406b9
local.meta
=
http://localhost:8090
\ No newline at end of file
apollo-core/src/test/java/com/ctrip/apollo/core/MetaDomainTest.java
0 → 100644
View file @
886406b9
package
com
.
ctrip
.
apollo
.
core
;
import
org.junit.Test
;
import
org.springframework.util.Assert
;
import
com.ctrip.apollo.Apollo.Env
;
public
class
MetaDomainTest
{
@Test
public
void
testDefaultDomain
()
{
Assert
.
notNull
(
MetaDomainConsts
.
getDomain
(
Env
.
LOCAL
));
Assert
.
isNull
(
MetaDomainConsts
.
getDomain
(
Env
.
PRO
));
}
}
apollo-core/src/test/resources/apollo-env.properties
0 → 100644
View file @
886406b9
local.meta
=
http://localhost:8090
\ No newline at end of file
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