Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
2837221d
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
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,体验更适合开发者的 AI 搜索 >>
提交
2837221d
编写于
6月 13, 2016
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support server config separated by cluster
上级
f1b91928
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
203 addition
and
30 deletion
+203
-30
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/customize/BizLoggingCustomizer.java
.../framework/apollo/biz/customize/BizLoggingCustomizer.java
+4
-4
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/entity/ServerConfig.java
...a/com/ctrip/framework/apollo/biz/entity/ServerConfig.java
+11
-0
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfig.java
...framework/apollo/biz/eureka/ApolloEurekaClientConfig.java
+5
-7
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/repository/ServerConfigRepository.java
...amework/apollo/biz/repository/ServerConfigRepository.java
+1
-1
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/ServerConfigService.java
...rip/framework/apollo/biz/service/ServerConfigService.java
+48
-0
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/AllTests.java
...rc/test/java/com/ctrip/framework/apollo/biz/AllTests.java
+12
-1
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfigTest.java
...ework/apollo/biz/eureka/ApolloEurekaClientConfigTest.java
+9
-16
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/ServerConfigServiceTest.java
...framework/apollo/biz/service/ServerConfigServiceTest.java
+111
-0
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
...main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+1
-1
apollo-core/src/main/java/com/ctrip/framework/apollo/core/ConfigConsts.java
...in/java/com/ctrip/framework/apollo/core/ConfigConsts.java
+1
-0
未找到文件。
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/customize/BizLoggingCustomizer.java
浏览文件 @
2837221d
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
浏览文件 @
2837221d
...
...
@@ -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
浏览文件 @
2837221d
...
...
@@ -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
浏览文件 @
2837221d
...
...
@@ -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
浏览文件 @
2837221d
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
浏览文件 @
2837221d
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
浏览文件 @
2837221d
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
浏览文件 @
2837221d
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
浏览文件 @
2837221d
...
...
@@ -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
浏览文件 @
2837221d
...
...
@@ -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"
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录