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
59ad9e90
Commit
59ad9e90
authored
Jun 13, 2016
by
张乐
Committed by
GitHub
Jun 13, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #263 from nobodyiam/serverconfig-cluster
support server config separated by cluster
parents
f1b91928
2837221d
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
203 additions
and
30 deletions
+203
-30
BizLoggingCustomizer.java
.../framework/apollo/biz/customize/BizLoggingCustomizer.java
+4
-4
ServerConfig.java
...a/com/ctrip/framework/apollo/biz/entity/ServerConfig.java
+11
-0
ApolloEurekaClientConfig.java
...framework/apollo/biz/eureka/ApolloEurekaClientConfig.java
+5
-7
ServerConfigRepository.java
...amework/apollo/biz/repository/ServerConfigRepository.java
+1
-1
ServerConfigService.java
...rip/framework/apollo/biz/service/ServerConfigService.java
+48
-0
AllTests.java
...rc/test/java/com/ctrip/framework/apollo/biz/AllTests.java
+12
-1
ApolloEurekaClientConfigTest.java
...ework/apollo/biz/eureka/ApolloEurekaClientConfigTest.java
+9
-16
ServerConfigServiceTest.java
...framework/apollo/biz/service/ServerConfigServiceTest.java
+111
-0
ConfigUtil.java
...main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+1
-1
ConfigConsts.java
...in/java/com/ctrip/framework/apollo/core/ConfigConsts.java
+1
-0
No files found.
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/customize/BizLoggingCustomizer.java
View file @
59ad9e90
package
com
.
ctrip
.
framework
.
apollo
.
biz
.
customize
;
import
com.ctrip.framework.apollo.biz.
repository.ServerConfigRepository
;
import
com.ctrip.framework.apollo.biz.
service.ServerConfigService
;
import
com.ctrip.framework.apollo.common.customize.LoggingCustomizer
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -15,7 +15,7 @@ public class BizLoggingCustomizer extends LoggingCustomizer{
private
static
final
String
CLOGGING_SERVER_PORT_KEY
=
"clogging.server.port"
;
@Autowired
private
ServerConfig
Repository
serverConfigRepository
;
private
ServerConfig
Service
serverConfigService
;
private
String
cloggingUrl
;
private
String
cloggingPort
;
...
...
@@ -23,7 +23,7 @@ public class BizLoggingCustomizer extends LoggingCustomizer{
@Override
protected
String
cloggingUrl
()
{
if
(
cloggingUrl
==
null
){
cloggingUrl
=
serverConfig
Repository
.
findByKey
(
CLOGGING_SERVER_URL_KEY
).
getValue
(
);
cloggingUrl
=
serverConfig
Service
.
getValue
(
CLOGGING_SERVER_URL_KEY
);
}
return
cloggingUrl
;
}
...
...
@@ -31,7 +31,7 @@ public class BizLoggingCustomizer extends LoggingCustomizer{
@Override
protected
String
cloggingPort
()
{
if
(
cloggingPort
==
null
){
cloggingPort
=
serverConfig
Repository
.
findByKey
(
CLOGGING_SERVER_PORT_KEY
).
getValue
(
);
cloggingPort
=
serverConfig
Service
.
getValue
(
CLOGGING_SERVER_PORT_KEY
);
}
return
cloggingPort
;
}
...
...
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/entity/ServerConfig.java
View file @
59ad9e90
...
...
@@ -20,6 +20,9 @@ public class ServerConfig extends BaseEntity {
@Column
(
name
=
"Key"
,
nullable
=
false
)
private
String
key
;
@Column
(
name
=
"Cluster"
,
nullable
=
false
)
private
String
cluster
;
@Column
(
name
=
"Value"
,
nullable
=
false
)
private
String
value
;
...
...
@@ -50,6 +53,14 @@ public class ServerConfig extends BaseEntity {
this
.
comment
=
comment
;
}
public
String
getCluster
()
{
return
cluster
;
}
public
void
setCluster
(
String
cluster
)
{
this
.
cluster
=
cluster
;
}
public
String
toString
()
{
return
toStringHelper
().
add
(
"key"
,
key
).
add
(
"value"
,
value
).
add
(
"comment"
,
comment
).
toString
();
}
...
...
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfig.java
View file @
59ad9e90
...
...
@@ -3,8 +3,7 @@ package com.ctrip.framework.apollo.biz.eureka;
import
com.google.common.base.Splitter
;
import
com.google.common.base.Strings
;
import
com.ctrip.framework.apollo.biz.entity.ServerConfig
;
import
com.ctrip.framework.apollo.biz.repository.ServerConfigRepository
;
import
com.ctrip.framework.apollo.biz.service.ServerConfigService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.netflix.eureka.EurekaClientConfigBean
;
...
...
@@ -13,7 +12,6 @@ import org.springframework.core.env.Environment;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
import
java.util.Objects
;
@Component
@Primary
...
...
@@ -22,7 +20,7 @@ public class ApolloEurekaClientConfig extends EurekaClientConfigBean {
private
static
final
Splitter
URL_SPLITTER
=
Splitter
.
on
(
","
).
omitEmptyStrings
();
@Autowired
private
ServerConfig
Repository
serverConfigRepository
;
private
ServerConfig
Service
serverConfigService
;
@Autowired
private
Environment
environment
;
...
...
@@ -37,10 +35,10 @@ public class ApolloEurekaClientConfig extends EurekaClientConfigBean {
}
//Second check if it is configured in database
S
erverConfig
eurekaUrl
=
serverConfigRepository
.
findByKey
(
EUREKA_URL_CONFIG
);
S
tring
eurekaUrl
=
serverConfigService
.
getValue
(
EUREKA_URL_CONFIG
);
if
(!
Objects
.
isNull
(
eurekaUrl
)
&&
!
Strings
.
isNullOrEmpty
(
eurekaUrl
.
getValue
()
))
{
return
URL_SPLITTER
.
splitToList
(
eurekaUrl
.
getValue
()
);
if
(!
Strings
.
isNullOrEmpty
(
eurekaUrl
))
{
return
URL_SPLITTER
.
splitToList
(
eurekaUrl
);
}
...
...
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/repository/ServerConfigRepository.java
View file @
59ad9e90
...
...
@@ -8,5 +8,5 @@ import org.springframework.data.repository.PagingAndSortingRepository;
* @author Jason Song(song_s@ctrip.com)
*/
public
interface
ServerConfigRepository
extends
PagingAndSortingRepository
<
ServerConfig
,
Long
>
{
ServerConfig
find
ByKey
(
String
key
);
ServerConfig
find
TopByKeyAndCluster
(
String
key
,
String
cluster
);
}
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/ServerConfigService.java
0 → 100644
View file @
59ad9e90
package
com
.
ctrip
.
framework
.
apollo
.
biz
.
service
;
import
com.google.common.base.Strings
;
import
com.ctrip.framework.apollo.biz.entity.ServerConfig
;
import
com.ctrip.framework.apollo.biz.repository.ServerConfigRepository
;
import
com.ctrip.framework.apollo.core.ConfigConsts
;
import
com.ctrip.framework.foundation.Foundation
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@Service
public
class
ServerConfigService
{
@Autowired
private
ServerConfigRepository
serverConfigRepository
;
public
String
getValue
(
String
key
)
{
ServerConfig
serverConfig
=
null
;
//1. Load from cluster config
if
(!
Strings
.
isNullOrEmpty
(
System
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
)))
{
serverConfig
=
serverConfigRepository
.
findTopByKeyAndCluster
(
key
,
System
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
));
}
//2. Fall back to data center config
if
(
serverConfig
==
null
&&
!
Strings
.
isNullOrEmpty
(
getDataCenter
()))
{
serverConfig
=
serverConfigRepository
.
findTopByKeyAndCluster
(
key
,
getDataCenter
());
}
//3. Fall back to default cluster config
if
(
serverConfig
==
null
)
{
serverConfig
=
serverConfigRepository
.
findTopByKeyAndCluster
(
key
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
}
return
serverConfig
==
null
?
null
:
serverConfig
.
getValue
();
}
String
getDataCenter
()
{
return
Foundation
.
server
().
getDataCenter
();
}
}
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/AllTests.java
View file @
59ad9e90
package
com
.
ctrip
.
framework
.
apollo
.
biz
;
import
com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfigTest
;
import
com.ctrip.framework.apollo.biz.message.DatabaseMessageSenderTest
;
import
com.ctrip.framework.apollo.biz.message.ReleaseMessageScannerTest
;
import
com.ctrip.framework.apollo.biz.repository.AppNamespaceRepositoryTest
;
import
com.ctrip.framework.apollo.biz.repository.AppRepositoryTest
;
import
com.ctrip.framework.apollo.biz.service.AdminServiceTest
;
import
com.ctrip.framework.apollo.biz.service.AdminServiceTransactionTest
;
import
com.ctrip.framework.apollo.biz.service.ClusterServiceTest
;
import
com.ctrip.framework.apollo.biz.service.ConfigServiceTest
;
import
com.ctrip.framework.apollo.biz.service.PrivilegeServiceTest
;
import
com.ctrip.framework.apollo.biz.service.ServerConfigServiceTest
;
import
com.ctrip.framework.apollo.biz.utils.ReleaseKeyGeneratorTest
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.Suite
;
...
...
@@ -20,7 +25,13 @@ import org.junit.runners.Suite.SuiteClasses;
ConfigServiceTest
.
class
,
PrivilegeServiceTest
.
class
,
AdminServiceTransactionTest
.
class
,
DatabaseMessageSenderTest
.
class
})
DatabaseMessageSenderTest
.
class
,
ServerConfigServiceTest
.
class
,
ApolloEurekaClientConfigTest
.
class
,
ReleaseMessageScannerTest
.
class
,
ClusterServiceTest
.
class
,
ReleaseKeyGeneratorTest
.
class
})
public
class
AllTests
{
}
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfigTest.java
View file @
59ad9e90
package
com
.
ctrip
.
framework
.
apollo
.
biz
.
eureka
;
import
com.ctrip.framework.apollo.biz.entity.ServerConfig
;
import
com.ctrip.framework.apollo.biz.repository.ServerConfigRepository
;
import
com.ctrip.framework.apollo.biz.service.ServerConfigService
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -13,7 +12,8 @@ import org.springframework.test.util.ReflectionTestUtils;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
...
...
@@ -23,14 +23,14 @@ import static org.mockito.Mockito.when;
public
class
ApolloEurekaClientConfigTest
{
private
ApolloEurekaClientConfig
eurekaClientConfig
;
@Mock
private
ServerConfig
Repository
serverConfigRepository
;
private
ServerConfig
Service
serverConfigService
;
@Mock
private
Environment
environment
;
@Before
public
void
setUp
()
throws
Exception
{
eurekaClientConfig
=
new
ApolloEurekaClientConfig
();
ReflectionTestUtils
.
setField
(
eurekaClientConfig
,
"serverConfig
Repository"
,
serverConfigRepository
);
ReflectionTestUtils
.
setField
(
eurekaClientConfig
,
"serverConfig
Service"
,
serverConfigService
);
ReflectionTestUtils
.
setField
(
eurekaClientConfig
,
"environment"
,
environment
);
}
...
...
@@ -39,8 +39,8 @@ public class ApolloEurekaClientConfigTest {
String
someEurekaUrl
=
"http://xxx,http://yyy"
;
String
myZone
=
"xx"
;
when
(
serverConfig
Repository
.
findByKey
(
ApolloEurekaClientConfig
.
EUREKA_URL_CONFIG
))
.
thenReturn
(
assembleServerConfig
(
ApolloEurekaClientConfig
.
EUREKA_URL_CONFIG
,
someEurekaUrl
)
);
when
(
serverConfig
Service
.
getValue
(
ApolloEurekaClientConfig
.
EUREKA_URL_CONFIG
))
.
thenReturn
(
someEurekaUrl
);
List
<
String
>
eurekaUrls
=
eurekaClientConfig
.
getEurekaServerServiceUrls
(
myZone
);
String
[]
expected
=
someEurekaUrl
.
split
(
","
);
...
...
@@ -59,8 +59,8 @@ public class ApolloEurekaClientConfigTest {
when
(
environment
.
getProperty
(
ApolloEurekaClientConfig
.
EUREKA_URL_CONFIG
))
.
thenReturn
(
someEurekaUrlFromSystemProperty
);
when
(
serverConfig
Repository
.
findByKey
(
ApolloEurekaClientConfig
.
EUREKA_URL_CONFIG
))
.
thenReturn
(
assembleServerConfig
(
ApolloEurekaClientConfig
.
EUREKA_URL_CONFIG
,
someEurekaUrl
)
);
when
(
serverConfig
Service
.
getValue
(
ApolloEurekaClientConfig
.
EUREKA_URL_CONFIG
))
.
thenReturn
(
someEurekaUrl
);
List
<
String
>
eurekaUrls
=
eurekaClientConfig
.
getEurekaServerServiceUrls
(
myZone
);
...
...
@@ -70,11 +70,4 @@ public class ApolloEurekaClientConfigTest {
assertTrue
(
eurekaUrls
.
contains
(
url
));
}
}
private
ServerConfig
assembleServerConfig
(
String
key
,
String
value
)
{
ServerConfig
config
=
new
ServerConfig
();
config
.
setKey
(
key
);
config
.
setValue
(
value
);
return
config
;
}
}
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/ServerConfigServiceTest.java
0 → 100644
View file @
59ad9e90
package
com
.
ctrip
.
framework
.
apollo
.
biz
.
service
;
import
com.ctrip.framework.apollo.biz.entity.ServerConfig
;
import
com.ctrip.framework.apollo.biz.repository.ServerConfigRepository
;
import
com.ctrip.framework.apollo.core.ConfigConsts
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.test.util.ReflectionTestUtils
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.
spy
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
ServerConfigServiceTest
{
private
ServerConfigService
serverConfigService
;
@Mock
private
ServerConfigRepository
serverConfigRepository
;
private
String
someCluster
;
private
String
someDC
;
private
String
someKey
;
private
String
someClusterValue
;
private
String
someDCValue
;
private
String
defaultClusterValue
;
@Before
public
void
setUp
()
throws
Exception
{
serverConfigService
=
spy
(
new
ServerConfigService
());
ReflectionTestUtils
.
setField
(
serverConfigService
,
"serverConfigRepository"
,
serverConfigRepository
);
someCluster
=
"someCluster"
;
someDC
=
"someDC"
;
someKey
=
"someKey"
;
someClusterValue
=
"someClusterValue"
;
someDCValue
=
"someDCValue"
;
defaultClusterValue
=
"defaultClusterValue"
;
when
(
serverConfigRepository
.
findTopByKeyAndCluster
(
someKey
,
someCluster
))
.
thenReturn
(
assembleServerConfig
(
someKey
,
someClusterValue
));
when
(
serverConfigRepository
.
findTopByKeyAndCluster
(
someKey
,
someDC
))
.
thenReturn
(
assembleServerConfig
(
someKey
,
someDCValue
));
when
(
serverConfigRepository
.
findTopByKeyAndCluster
(
someKey
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
))
.
thenReturn
(
assembleServerConfig
(
someKey
,
defaultClusterValue
));
}
@After
public
void
tearDown
()
throws
Exception
{
System
.
clearProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
);
}
@Test
public
void
testGetValueWithNoCluster
()
throws
Exception
{
when
(
serverConfigService
.
getDataCenter
()).
thenReturn
(
null
);
assertEquals
(
defaultClusterValue
,
serverConfigService
.
getValue
(
someKey
));
}
@Test
public
void
testGetValueWithCluster
()
throws
Exception
{
System
.
setProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
,
someCluster
);
assertEquals
(
someClusterValue
,
serverConfigService
.
getValue
(
someKey
));
}
@Test
public
void
testGetValueWithDataCenter
()
throws
Exception
{
when
(
serverConfigService
.
getDataCenter
()).
thenReturn
(
someDC
);
assertEquals
(
someDCValue
,
serverConfigService
.
getValue
(
someKey
));
}
@Test
public
void
testGetValueWithKeyNotExists
()
throws
Exception
{
String
someKeyNotExists
=
"someKeyNotExists"
;
System
.
setProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
,
someCluster
);
when
(
serverConfigService
.
getDataCenter
()).
thenReturn
(
someDC
);
assertNull
(
serverConfigService
.
getValue
(
someKeyNotExists
));
}
@Test
public
void
testGetValueWithClusterNotExists
()
throws
Exception
{
String
someClusterNotExists
=
"someClusterNotExists"
;
System
.
setProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
,
someClusterNotExists
);
assertEquals
(
defaultClusterValue
,
serverConfigService
.
getValue
(
someKey
));
}
@Test
public
void
testGetValueWithDCNotExists
()
throws
Exception
{
String
someDCNotExists
=
"someDCNotExists"
;
when
(
serverConfigService
.
getDataCenter
()).
thenReturn
(
someDCNotExists
);
assertEquals
(
defaultClusterValue
,
serverConfigService
.
getValue
(
someKey
));
}
private
ServerConfig
assembleServerConfig
(
String
key
,
String
value
)
{
ServerConfig
serverConfig
=
new
ServerConfig
();
serverConfig
.
setKey
(
key
);
serverConfig
.
setValue
(
value
);
return
serverConfig
;
}
}
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
View file @
59ad9e90
...
...
@@ -58,7 +58,7 @@ public class ConfigUtil {
private
void
initCluster
()
{
//Load data center from system property
cluster
=
System
.
getProperty
(
"apollo.cluster"
);
cluster
=
System
.
getProperty
(
ConfigConsts
.
APOLLO_CLUSTER_KEY
);
//Use data center as cluster
if
(
Strings
.
isNullOrEmpty
(
cluster
))
{
...
...
apollo-core/src/main/java/com/ctrip/framework/apollo/core/ConfigConsts.java
View file @
59ad9e90
...
...
@@ -4,4 +4,5 @@ public interface ConfigConsts {
String
NAMESPACE_APPLICATION
=
"application"
;
String
CLUSTER_NAME_DEFAULT
=
"default"
;
String
CLUSTER_NAMESPACE_SEPARATOR
=
"+"
;
String
APOLLO_CLUSTER_KEY
=
"apollo.cluster"
;
}
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