Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
superrain51
apollo
提交
28929a3f
apollo
项目概览
superrain51
/
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,发现更多精彩内容 >>
提交
28929a3f
编写于
8月 01, 2016
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
adapt for situations when no appid is provided in client side
上级
0a5424ed
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
132 addition
and
13 deletion
+132
-13
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
...main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+8
-4
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigController.java
...ork/apollo/configservice/controller/ConfigController.java
+12
-6
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/util/WatchKeysUtil.java
...ip/framework/apollo/configservice/util/WatchKeysUtil.java
+7
-1
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/ConfigControllerTest.java
...apollo/configservice/controller/ConfigControllerTest.java
+47
-2
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/integration/ConfigControllerIntegrationTest.java
...gservice/integration/ConfigControllerIntegrationTest.java
+33
-0
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/util/WatchKeysUtilTest.java
...ramework/apollo/configservice/util/WatchKeysUtilTest.java
+24
-0
apollo-core/src/main/java/com/ctrip/framework/apollo/core/ConfigConsts.java
...in/java/com/ctrip/framework/apollo/core/ConfigConsts.java
+1
-0
未找到文件。
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
浏览文件 @
28929a3f
...
...
@@ -7,12 +7,13 @@ import com.ctrip.framework.apollo.core.ConfigConsts;
import
com.ctrip.framework.apollo.core.MetaDomainConsts
;
import
com.ctrip.framework.apollo.core.enums.Env
;
import
com.ctrip.framework.apollo.core.enums.EnvUtils
;
import
com.ctrip.framework.apollo.exceptions.ApolloConfigException
;
import
com.ctrip.framework.foundation.Foundation
;
import
com.dianping.cat.Cat
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.unidal.lookup.annotation.Named
;
import
org.unidal.net.Networks
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -41,12 +42,15 @@ public class ConfigUtil {
/**
* Get the app id for the current application.
*
* @return the app id
* @throws IllegalStateException if app id is not set
* @return the app id or ConfigConsts.NO_APPID_PLACEHOLDER if app id is not available
*/
public
String
getAppId
()
{
String
appId
=
Foundation
.
app
().
getAppId
();
Preconditions
.
checkState
(
appId
!=
null
,
"app.id is not set"
);
if
(
Strings
.
isNullOrEmpty
(
appId
))
{
appId
=
ConfigConsts
.
NO_APPID_PLACEHOLDER
;
logger
.
error
(
"app.id is not set, apollo will only load public namespace configurations!"
);
Cat
.
logError
(
new
ApolloConfigException
(
"app.id is not set"
));
}
return
appId
;
}
...
...
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigController.java
浏览文件 @
28929a3f
...
...
@@ -59,19 +59,20 @@ public class ConfigController {
@RequestParam
(
value
=
"ip"
,
required
=
false
)
String
clientIp
,
HttpServletResponse
response
)
throws
IOException
{
String
originalNamespace
=
namespace
;
//strip out .properties suffix
namespace
=
namespaceUtil
.
filterNamespaceName
(
namespace
);
List
<
Release
>
releases
=
Lists
.
newLinkedList
();
Release
currentAppRelease
=
loadConfig
(
appId
,
clusterName
,
namespace
,
dataCenter
);
String
appClusterNameLoaded
=
clusterName
;
if
(!
ConfigConsts
.
NO_APPID_PLACEHOLDER
.
equalsIgnoreCase
(
appId
))
{
Release
currentAppRelease
=
loadConfig
(
appId
,
clusterName
,
namespace
,
dataCenter
);
if
(
currentAppRelease
!=
null
)
{
releases
.
add
(
currentAppRelease
);
//we have cluster search process, so the cluster name might be overridden
appClusterNameLoaded
=
currentAppRelease
.
getClusterName
();
if
(
currentAppRelease
!=
null
)
{
releases
.
add
(
currentAppRelease
);
//we have cluster search process, so the cluster name might be overridden
appClusterNameLoaded
=
currentAppRelease
.
getClusterName
();
}
}
//if namespace does not belong to this appId, should check if there is a public configuration
...
...
@@ -116,6 +117,11 @@ public class ConfigController {
return
true
;
}
//if no appId is present, then no other namespace belongs to it
if
(
ConfigConsts
.
NO_APPID_PLACEHOLDER
.
equalsIgnoreCase
(
appId
))
{
return
false
;
}
AppNamespace
appNamespace
=
appNamespaceService
.
findOne
(
appId
,
namespaceName
);
return
appNamespace
!=
null
;
...
...
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/util/WatchKeysUtil.java
浏览文件 @
28929a3f
...
...
@@ -40,6 +40,7 @@ public class WatchKeysUtil {
/**
* Assemble watch keys for the given appId, cluster, namespaces, dataCenter combination
*
* @return a multimap with namespace as the key and watch keys as the value
*/
public
Multimap
<
String
,
String
>
assembleAllWatchKeys
(
String
appId
,
String
clusterName
,
...
...
@@ -91,7 +92,9 @@ public class WatchKeysUtil {
private
Set
<
String
>
assembleWatchKeys
(
String
appId
,
String
clusterName
,
String
namespace
,
String
dataCenter
)
{
if
(
ConfigConsts
.
NO_APPID_PLACEHOLDER
.
equalsIgnoreCase
(
appId
))
{
return
Collections
.
emptySet
();
}
Set
<
String
>
watchedKeys
=
Sets
.
newHashSet
();
//watch specified cluster config change
...
...
@@ -124,6 +127,9 @@ public class WatchKeysUtil {
}
private
Set
<
String
>
namespacesBelongToAppId
(
String
appId
,
Set
<
String
>
namespaces
)
{
if
(
ConfigConsts
.
NO_APPID_PLACEHOLDER
.
equalsIgnoreCase
(
appId
))
{
return
Collections
.
emptySet
();
}
List
<
AppNamespace
>
appNamespaces
=
appNamespaceService
.
findByAppIdAndNamespaces
(
appId
,
namespaces
);
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/ConfigControllerTest.java
浏览文件 @
28929a3f
...
...
@@ -30,6 +30,7 @@ import static org.junit.Assert.assertNull;
import
static
org
.
mockito
.
Matchers
.
anyString
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
...
...
@@ -283,8 +284,8 @@ public class ConfigControllerTest {
when
(
somePublicRelease
.
getReleaseKey
()).
thenReturn
(
someServerSideReleaseKey
);
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
somePublicNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someClientIp
,
someResponse
);
.
queryConfig
(
someAppId
,
someClusterName
,
somePublicNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someClientIp
,
someResponse
);
assertEquals
(
someServerSideReleaseKey
,
result
.
getReleaseKey
());
assertEquals
(
someAppId
,
result
.
getAppId
());
...
...
@@ -431,6 +432,50 @@ public class ConfigControllerTest {
configController
.
mergeReleaseConfigurations
(
Lists
.
newArrayList
(
someRelease
));
}
@Test
public
void
testQueryConfigForNoAppIdPlaceHolder
()
throws
Exception
{
String
someClientSideReleaseKey
=
"1"
;
HttpServletResponse
someResponse
=
mock
(
HttpServletResponse
.
class
);
String
appId
=
ConfigConsts
.
NO_APPID_PLACEHOLDER
;
ApolloConfig
result
=
configController
.
queryConfig
(
appId
,
someClusterName
,
defaultNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someClientIp
,
someResponse
);
verify
(
releaseService
,
never
()).
findLatestActiveRelease
(
appId
,
someClusterName
,
defaultNamespaceName
);
verify
(
appNamespaceService
,
never
()).
findPublicNamespaceByName
(
defaultNamespaceName
);
assertNull
(
result
);
verify
(
someResponse
,
times
(
1
)).
sendError
(
eq
(
HttpServletResponse
.
SC_NOT_FOUND
),
anyString
());
}
@Test
public
void
testQueryConfigForNoAppIdPlaceHolderWithPublicNamespace
()
throws
Exception
{
String
someClientSideReleaseKey
=
"1"
;
String
someServerSideReleaseKey
=
"2"
;
HttpServletResponse
someResponse
=
mock
(
HttpServletResponse
.
class
);
String
somePublicAppId
=
"somePublicAppId"
;
AppNamespace
somePublicAppNamespace
=
assemblePublicAppNamespace
(
somePublicAppId
,
somePublicNamespaceName
);
String
appId
=
ConfigConsts
.
NO_APPID_PLACEHOLDER
;
when
(
appNamespaceService
.
findPublicNamespaceByName
(
somePublicNamespaceName
))
.
thenReturn
(
somePublicAppNamespace
);
when
(
releaseService
.
findLatestActiveRelease
(
somePublicAppId
,
someDataCenter
,
somePublicNamespaceName
))
.
thenReturn
(
somePublicRelease
);
when
(
somePublicRelease
.
getReleaseKey
()).
thenReturn
(
someServerSideReleaseKey
);
ApolloConfig
result
=
configController
.
queryConfig
(
appId
,
someClusterName
,
somePublicNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someClientIp
,
someResponse
);
verify
(
releaseService
,
never
()).
findLatestActiveRelease
(
appId
,
someClusterName
,
somePublicNamespaceName
);
assertEquals
(
someServerSideReleaseKey
,
result
.
getReleaseKey
());
assertEquals
(
appId
,
result
.
getAppId
());
assertEquals
(
someClusterName
,
result
.
getCluster
());
assertEquals
(
somePublicNamespaceName
,
result
.
getNamespaceName
());
assertEquals
(
"foo"
,
result
.
getConfigurations
().
get
(
"apollo.public.bar"
));
}
private
AppNamespace
assemblePublicAppNamespace
(
String
appId
,
String
namespace
)
{
return
assembleAppNamespace
(
appId
,
namespace
,
true
);
}
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/integration/ConfigControllerIntegrationTest.java
浏览文件 @
28929a3f
...
...
@@ -217,4 +217,37 @@ public class ConfigControllerIntegrationTest extends AbstractBaseIntegrationTest
assertEquals
(
"v1-file"
,
result
.
getConfigurations
().
get
(
"k1"
));
assertEquals
(
null
,
result
.
getConfigurations
().
get
(
"k2"
));
}
@Test
public
void
testQueryConfigForNoAppIdPlaceHolderWithPrivateNamespace
()
throws
Exception
{
HttpStatusCodeException
httpException
=
null
;
try
{
ResponseEntity
<
ApolloConfig
>
response
=
restTemplate
.
getForEntity
(
"{baseurl}/configs/{appId}/{clusterName}/{namespace}"
,
ApolloConfig
.
class
,
getHostUrl
(),
ConfigConsts
.
NO_APPID_PLACEHOLDER
,
someCluster
,
ConfigConsts
.
NAMESPACE_APPLICATION
);
}
catch
(
HttpStatusCodeException
ex
)
{
httpException
=
ex
;
}
assertEquals
(
HttpStatus
.
NOT_FOUND
,
httpException
.
getStatusCode
());
}
@Test
@Sql
(
scripts
=
"/integration-test/test-release.sql"
,
executionPhase
=
Sql
.
ExecutionPhase
.
BEFORE_TEST_METHOD
)
@Sql
(
scripts
=
"/integration-test/cleanup.sql"
,
executionPhase
=
Sql
.
ExecutionPhase
.
AFTER_TEST_METHOD
)
public
void
testQueryPublicConfigForNoAppIdPlaceHolder
()
throws
Exception
{
ResponseEntity
<
ApolloConfig
>
response
=
restTemplate
.
getForEntity
(
"{baseurl}/configs/{appId}/{clusterName}/{namespace}?dataCenter={dateCenter}"
,
ApolloConfig
.
class
,
getHostUrl
(),
ConfigConsts
.
NO_APPID_PLACEHOLDER
,
someCluster
,
somePublicNamespace
,
someDC
);
ApolloConfig
result
=
response
.
getBody
();
assertEquals
(
"TEST-RELEASE-KEY4"
,
result
.
getReleaseKey
());
assertEquals
(
ConfigConsts
.
NO_APPID_PLACEHOLDER
,
result
.
getAppId
());
assertEquals
(
someCluster
,
result
.
getCluster
());
assertEquals
(
somePublicNamespace
,
result
.
getNamespaceName
());
assertEquals
(
"someDC-v1"
,
result
.
getConfigurations
().
get
(
"k1"
));
assertEquals
(
"someDC-v2"
,
result
.
getConfigurations
().
get
(
"k2"
));
}
}
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/util/WatchKeysUtilTest.java
浏览文件 @
28929a3f
...
...
@@ -75,6 +75,8 @@ public class WatchKeysUtilTest {
when
(
somePublicAppNamespace
.
getName
()).
thenReturn
(
somePublicNamespace
);
when
(
appNamespaceService
.
findPublicNamespacesByNames
(
Sets
.
newHashSet
(
somePublicNamespace
)))
.
thenReturn
(
Lists
.
newArrayList
(
somePublicAppNamespace
));
when
(
appNamespaceService
.
findPublicNamespacesByNames
(
Sets
.
newHashSet
(
someNamespace
,
somePublicNamespace
)))
.
thenReturn
(
Lists
.
newArrayList
(
somePublicAppNamespace
));
ReflectionTestUtils
.
setField
(
watchKeysUtil
,
"appNamespaceService"
,
appNamespaceService
);
}
...
...
@@ -141,6 +143,28 @@ public class WatchKeysUtilTest {
assertWatchKeys
(
somePublicAppId
,
clusters
,
somePublicNamespace
,
watchKeysMap
.
get
(
somePublicNamespace
));
}
@Test
public
void
testAssembleWatchKeysForNoAppIdPlaceHolder
()
throws
Exception
{
Multimap
<
String
,
String
>
watchKeysMap
=
watchKeysUtil
.
assembleAllWatchKeys
(
ConfigConsts
.
NO_APPID_PLACEHOLDER
,
someCluster
,
Sets
.
newHashSet
(
someNamespace
,
anotherNamespace
),
someDC
);
assertTrue
(
watchKeysMap
.
isEmpty
());
}
@Test
public
void
testAssembleWatchKeysForNoAppIdPlaceHolderAndPublicNamespace
()
throws
Exception
{
Multimap
<
String
,
String
>
watchKeysMap
=
watchKeysUtil
.
assembleAllWatchKeys
(
ConfigConsts
.
NO_APPID_PLACEHOLDER
,
someCluster
,
Sets
.
newHashSet
(
someNamespace
,
somePublicNamespace
),
someDC
);
Set
<
String
>
clusters
=
Sets
.
newHashSet
(
defaultCluster
,
someCluster
,
someDC
);
assertEquals
(
clusters
.
size
(),
watchKeysMap
.
size
());
assertWatchKeys
(
somePublicAppId
,
clusters
,
somePublicNamespace
,
watchKeysMap
.
get
(
somePublicNamespace
));
}
private
void
assertWatchKeys
(
String
appId
,
Set
<
String
>
clusters
,
String
namespaceName
,
Collection
<
String
>
watchedKeys
)
{
for
(
String
cluster
:
clusters
)
{
...
...
apollo-core/src/main/java/com/ctrip/framework/apollo/core/ConfigConsts.java
浏览文件 @
28929a3f
...
...
@@ -6,4 +6,5 @@ public interface ConfigConsts {
String
CLUSTER_NAMESPACE_SEPARATOR
=
"+"
;
String
APOLLO_CLUSTER_KEY
=
"apollo.cluster"
;
String
CONFIG_FILE_CONTENT_KEY
=
"content"
;
String
NO_APPID_PLACEHOLDER
=
"ApolloNoAppIdPlaceHolder"
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录