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
ea3563c5
Commit
ea3563c5
authored
May 27, 2016
by
lepdou
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #229 from nobodyiam/log-client-ip
add ip for client-server interactions so that we could have more info…
parents
64a1d15b
fd0f2ca4
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
53 additions
and
37 deletions
+53
-37
ConfigServiceLocator.java
...trip/framework/apollo/internals/ConfigServiceLocator.java
+7
-4
RemoteConfigRepository.java
...ip/framework/apollo/internals/RemoteConfigRepository.java
+11
-1
ConfigUtil.java
...main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+5
-0
ConfigController.java
...ork/apollo/configservice/controller/ConfigController.java
+2
-10
NotificationController.java
...ollo/configservice/controller/NotificationController.java
+3
-2
ServiceController.java
...work/apollo/metaservice/controller/ServiceController.java
+1
-1
ConfigControllerTest.java
...apollo/configservice/controller/ConfigControllerTest.java
+14
-11
NotificationControllerTest.java
.../configservice/controller/NotificationControllerTest.java
+8
-6
ConfigControllerIntegrationTest.java
...gservice/integration/ConfigControllerIntegrationTest.java
+2
-2
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/ConfigServiceLocator.java
View file @
ea3563c5
package
com
.
ctrip
.
framework
.
apollo
.
internals
;
import
com.google.common.base.Joiner
;
import
com.google.common.base.Strings
;
import
com.google.common.collect.Lists
;
import
com.google.common.collect.Maps
;
import
com.google.common.escape.Escaper
;
...
...
@@ -23,7 +24,6 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.unidal.lookup.annotation.Inject
;
import
org.unidal.lookup.annotation.Named
;
import
org.unidal.net.Networks
;
import
java.lang.reflect.Type
;
import
java.util.List
;
...
...
@@ -138,18 +138,21 @@ public class ConfigServiceLocator implements Initializable {
}
}
throw
new
RuntimeException
(
"Get config services failed"
,
exception
);
throw
new
RuntimeException
(
String
.
format
(
"Get config services failed from %s"
,
url
),
exception
);
}
private
String
assembleMetaServiceUrl
()
{
String
domainName
=
m_configUtil
.
getMetaServerDomainName
();
String
appId
=
m_configUtil
.
getAppId
();
String
localIp
=
Networks
.
forIp
().
getLocalHostAddress
();
String
localIp
=
m_configUtil
.
getLocalIp
();
Escaper
escaper
=
UrlEscapers
.
urlPathSegmentEscaper
();
Map
<
String
,
String
>
queryParams
=
Maps
.
newHashMap
();
queryParams
.
put
(
"appId"
,
escaper
.
escape
(
appId
));
queryParams
.
put
(
"ip"
,
escaper
.
escape
(
localIp
));
if
(!
Strings
.
isNullOrEmpty
(
localIp
))
{
queryParams
.
put
(
"ip"
,
escaper
.
escape
(
localIp
));
}
return
domainName
+
"/services/config?"
+
MAP_JOINER
.
join
(
queryParams
);
}
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java
View file @
ea3563c5
...
...
@@ -181,7 +181,8 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
ApolloConfig
result
=
response
.
getBody
();
Cat
.
logEvent
(
"Apollo.Client.ConfigLoaded."
+
result
.
getNamespaceName
(),
result
.
getReleaseKey
());
Cat
.
logEvent
(
"Apollo.Client.ConfigLoaded."
+
result
.
getNamespaceName
(),
result
.
getReleaseKey
());
logger
.
debug
(
"Loaded config for {}: {}"
,
m_namespace
,
result
);
return
result
;
...
...
@@ -227,6 +228,11 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
queryParams
.
put
(
"dataCenter"
,
escaper
.
escape
(
dataCenter
));
}
String
localIp
=
m_configUtil
.
getLocalIp
();
if
(!
Strings
.
isNullOrEmpty
(
localIp
))
{
queryParams
.
put
(
"ip"
,
escaper
.
escape
(
localIp
));
}
String
pathExpanded
=
String
.
format
(
path
,
pathParams
.
toArray
());
if
(!
queryParams
.
isEmpty
())
{
...
...
@@ -329,6 +335,10 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
if
(!
Strings
.
isNullOrEmpty
(
dataCenter
))
{
queryParams
.
put
(
"dataCenter"
,
escaper
.
escape
(
dataCenter
));
}
String
localIp
=
m_configUtil
.
getLocalIp
();
if
(!
Strings
.
isNullOrEmpty
(
localIp
))
{
queryParams
.
put
(
"ip"
,
escaper
.
escape
(
localIp
));
}
String
params
=
MAP_JOINER
.
join
(
queryParams
);
if
(!
uri
.
endsWith
(
"/"
))
{
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
View file @
ea3563c5
...
...
@@ -9,6 +9,7 @@ import com.ctrip.framework.apollo.core.enums.EnvUtils;
import
com.ctrip.framework.foundation.Foundation
;
import
org.unidal.lookup.annotation.Named
;
import
org.unidal.net.Networks
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -79,6 +80,10 @@ public class ConfigUtil {
return
env
;
}
public
String
getLocalIp
()
{
return
Networks
.
forIp
().
getLocalHostAddress
();
}
public
String
getMetaServerDomainName
()
{
return
MetaDomainConsts
.
getDomain
(
getApolloEnv
());
}
...
...
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigController.java
View file @
ea3563c5
...
...
@@ -48,20 +48,12 @@ public class ConfigController {
}.
getType
();
private
static
final
Joiner
STRING_JOINER
=
Joiner
.
on
(
ConfigConsts
.
CLUSTER_NAMESPACE_SEPARATOR
);
@RequestMapping
(
value
=
"/{appId}/{clusterName}"
,
method
=
RequestMethod
.
GET
)
public
ApolloConfig
queryConfig
(
@PathVariable
String
appId
,
@PathVariable
String
clusterName
,
@RequestParam
(
value
=
"dataCenter"
,
required
=
false
)
String
dataCenter
,
@RequestParam
(
value
=
"releaseKey"
,
defaultValue
=
"-1"
)
String
clientSideReleaseKey
,
HttpServletResponse
response
)
throws
IOException
{
return
this
.
queryConfig
(
appId
,
clusterName
,
ConfigConsts
.
NAMESPACE_DEFAULT
,
dataCenter
,
clientSideReleaseKey
,
response
);
}
@RequestMapping
(
value
=
"/{appId}/{clusterName}/{namespace}"
,
method
=
RequestMethod
.
GET
)
public
ApolloConfig
queryConfig
(
@PathVariable
String
appId
,
@PathVariable
String
clusterName
,
@PathVariable
String
namespace
,
@RequestParam
(
value
=
"dataCenter"
,
required
=
false
)
String
dataCenter
,
@RequestParam
(
value
=
"releaseKey"
,
defaultValue
=
"-1"
)
String
clientSideReleaseKey
,
@RequestParam
(
value
=
"ip"
,
required
=
false
)
String
clientIp
,
HttpServletResponse
response
)
throws
IOException
{
List
<
Release
>
releases
=
Lists
.
newLinkedList
();
...
...
@@ -140,7 +132,7 @@ public class ConfigController {
}
//try to load via data center
if
(!
Objects
.
isNull
(
dataCenter
)
&&
!
Objects
.
equals
(
dataCenter
,
clusterName
))
{
if
(!
Strings
.
isNullOrEmpty
(
dataCenter
)
&&
!
Objects
.
equals
(
dataCenter
,
clusterName
))
{
Release
dataCenterRelease
=
configService
.
findRelease
(
appId
,
dataCenter
,
namespace
);
if
(!
Objects
.
isNull
(
dataCenterRelease
))
{
...
...
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/NotificationController.java
View file @
ea3563c5
...
...
@@ -60,7 +60,8 @@ public class NotificationController implements MessageListener {
@RequestParam
(
value
=
"appId"
)
String
appId
,
@RequestParam
(
value
=
"cluster"
)
String
cluster
,
@RequestParam
(
value
=
"namespace"
,
defaultValue
=
ConfigConsts
.
NAMESPACE_DEFAULT
)
String
namespace
,
@RequestParam
(
value
=
"dataCenter"
,
required
=
false
)
String
dataCenter
)
{
@RequestParam
(
value
=
"dataCenter"
,
required
=
false
)
String
dataCenter
,
@RequestParam
(
value
=
"ip"
,
required
=
false
)
String
clientIp
)
{
Set
<
String
>
watchedKeys
=
assembleWatchKeys
(
appId
,
cluster
,
namespace
,
dataCenter
);
//Listen on more namespaces, since it's not the default namespace
...
...
@@ -128,7 +129,7 @@ public class NotificationController implements MessageListener {
}
//watch data center config change
if
(!
Objects
.
isNull
(
dataCenter
)
&&
!
Objects
.
equals
(
dataCenter
,
clusterName
))
{
if
(!
Strings
.
isNullOrEmpty
(
dataCenter
)
&&
!
Objects
.
equals
(
dataCenter
,
clusterName
))
{
watchedKeys
.
add
(
assembleKey
(
appId
,
dataCenter
,
namespace
));
}
...
...
apollo-configservice/src/main/java/com/ctrip/framework/apollo/metaservice/controller/ServiceController.java
View file @
ea3563c5
...
...
@@ -42,7 +42,7 @@ public class ServiceController {
@RequestMapping
(
"/config"
)
public
List
<
ServiceDTO
>
getConfigService
(
@RequestParam
(
value
=
"appId"
,
defaultValue
=
""
)
String
appId
,
@RequestParam
(
value
=
"ip"
,
defaultValue
=
""
)
String
clientIp
)
{
@RequestParam
(
value
=
"ip"
,
required
=
false
)
String
clientIp
)
{
List
<
InstanceInfo
>
instances
=
discoveryService
.
getConfigServiceInstances
();
List
<
ServiceDTO
>
result
=
instances
.
stream
().
map
(
new
Function
<
InstanceInfo
,
ServiceDTO
>()
{
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/ConfigControllerTest.java
View file @
ea3563c5
...
...
@@ -49,6 +49,7 @@ public class ConfigControllerTest {
private
String
defaultNamespaceName
;
private
String
somePublicNamespaceName
;
private
String
someDataCenter
;
private
String
someClientIp
;
@Mock
private
Release
someRelease
;
@Mock
...
...
@@ -66,6 +67,7 @@ public class ConfigControllerTest {
defaultNamespaceName
=
ConfigConsts
.
NAMESPACE_DEFAULT
;
somePublicNamespaceName
=
"somePublicNamespace"
;
someDataCenter
=
"someDC"
;
someClientIp
=
"someClientIp"
;
String
someValidConfiguration
=
"{\"apollo.bar\": \"foo\"}"
;
String
somePublicConfiguration
=
"{\"apollo.public.bar\": \"foo\"}"
;
...
...
@@ -85,8 +87,8 @@ public class ConfigControllerTest {
when
(
someRelease
.
getReleaseKey
()).
thenReturn
(
someServerSideNewReleaseKey
);
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
defaultNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someResponse
);
defaultNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someClientIp
,
someResponse
);
verify
(
configService
,
times
(
1
)).
findRelease
(
someAppId
,
someClusterName
,
defaultNamespaceName
);
assertEquals
(
someAppId
,
result
.
getAppId
());
...
...
@@ -106,7 +108,7 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
defaultNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someResponse
);
some
ClientIp
,
some
Response
);
assertNull
(
result
);
verify
(
someResponse
,
times
(
1
)).
sendError
(
eq
(
HttpServletResponse
.
SC_NOT_FOUND
),
anyString
());
...
...
@@ -125,7 +127,7 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
defaultNamespaceName
,
someDataCenter
,
String
.
valueOf
(
someClientSideReleaseKey
),
someResponse
);
someDataCenter
,
String
.
valueOf
(
someClientSideReleaseKey
),
some
ClientIp
,
some
Response
);
assertNull
(
result
);
verify
(
someResponse
,
times
(
1
)).
setStatus
(
HttpServletResponse
.
SC_NOT_MODIFIED
);
...
...
@@ -144,7 +146,7 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
defaultClusterName
,
defaultNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someResponse
);
some
ClientIp
,
some
Response
);
verify
(
configService
,
times
(
1
)).
findRelease
(
someAppId
,
someDataCenter
,
defaultNamespaceName
);
assertEquals
(
someAppId
,
result
.
getAppId
());
...
...
@@ -168,10 +170,11 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
defaultClusterName
,
defaultNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someResponse
);
some
ClientIp
,
some
Response
);
verify
(
configService
,
times
(
1
)).
findRelease
(
someAppId
,
someDataCenter
,
defaultNamespaceName
);
verify
(
configService
,
times
(
1
)).
findRelease
(
someAppId
,
defaultClusterName
,
defaultNamespaceName
);
verify
(
configService
,
times
(
1
))
.
findRelease
(
someAppId
,
defaultClusterName
,
defaultNamespaceName
);
assertEquals
(
someAppId
,
result
.
getAppId
());
assertEquals
(
defaultClusterName
,
result
.
getCluster
());
assertEquals
(
defaultNamespaceName
,
result
.
getNamespaceName
());
...
...
@@ -198,7 +201,7 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
someAppOwnNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someResponse
);
someClientSideReleaseKey
,
some
ClientIp
,
some
Response
);
assertEquals
(
someServerSideReleaseKey
,
result
.
getReleaseKey
());
assertEquals
(
someAppId
,
result
.
getAppId
());
...
...
@@ -227,7 +230,7 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
somePublicNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someResponse
);
someClientSideReleaseKey
,
some
ClientIp
,
some
Response
);
assertEquals
(
someServerSideReleaseKey
,
result
.
getReleaseKey
());
assertEquals
(
someAppId
,
result
.
getAppId
());
...
...
@@ -259,7 +262,7 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
somePublicNamespaceName
,
someDataCenter
,
someClientSideReleaseKey
,
someResponse
);
someClientSideReleaseKey
,
some
ClientIp
,
some
Response
);
assertEquals
(
someServerSideReleaseKey
,
result
.
getReleaseKey
());
assertEquals
(
someAppId
,
result
.
getAppId
());
...
...
@@ -294,7 +297,7 @@ public class ConfigControllerTest {
ApolloConfig
result
=
configController
.
queryConfig
(
someAppId
,
someClusterName
,
somePublicNamespaceName
,
someDataCenter
,
someAppSideReleaseKey
,
someResponse
);
someAppSideReleaseKey
,
some
ClientIp
,
some
Response
);
assertEquals
(
Joiner
.
on
(
ConfigConsts
.
CLUSTER_NAMESPACE_SEPARATOR
)
.
join
(
someAppSideReleaseKey
,
somePublicAppSideReleaseKey
),
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerTest.java
View file @
ea3563c5
...
...
@@ -39,6 +39,7 @@ public class NotificationControllerTest {
private
String
defaultNamespace
;
private
String
somePublicNamespace
;
private
String
someDataCenter
;
private
String
someClientIp
;
@Mock
private
AppNamespaceService
appNamespaceService
;
@Mock
...
...
@@ -58,6 +59,7 @@ public class NotificationControllerTest {
defaultNamespace
=
ConfigConsts
.
NAMESPACE_DEFAULT
;
somePublicNamespace
=
"somePublicNamespace"
;
someDataCenter
=
"someDC"
;
someClientIp
=
"someClientIp"
;
deferredResults
=
(
Multimap
<
String
,
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>>)
ReflectionTestUtils
...
...
@@ -68,7 +70,7 @@ public class NotificationControllerTest {
public
void
testPollNotificationWithDefaultNamespace
()
throws
Exception
{
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>
deferredResult
=
controller
.
pollNotification
(
someAppId
,
someCluster
,
defaultNamespace
,
someDataCenter
);
.
pollNotification
(
someAppId
,
someCluster
,
defaultNamespace
,
someDataCenter
,
someClientIp
);
List
<
String
>
clusters
=
Lists
.
newArrayList
(
someCluster
,
someDataCenter
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
...
...
@@ -88,7 +90,7 @@ public class NotificationControllerTest {
throws
Exception
{
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>
deferredResult
=
controller
.
pollNotification
(
someAppId
,
defaultCluster
,
defaultNamespace
,
someDataCenter
);
.
pollNotification
(
someAppId
,
defaultCluster
,
defaultNamespace
,
someDataCenter
,
someClientIp
);
List
<
String
>
clusters
=
Lists
.
newArrayList
(
someDataCenter
,
defaultCluster
);
...
...
@@ -108,7 +110,7 @@ public class NotificationControllerTest {
throws
Exception
{
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>
deferredResult
=
controller
.
pollNotification
(
someAppId
,
defaultCluster
,
defaultNamespace
,
null
);
.
pollNotification
(
someAppId
,
defaultCluster
,
defaultNamespace
,
null
,
someClientIp
);
List
<
String
>
clusters
=
Lists
.
newArrayList
(
defaultCluster
);
...
...
@@ -135,7 +137,7 @@ public class NotificationControllerTest {
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>
deferredResult
=
controller
.
pollNotification
(
someAppId
,
someCluster
,
somePublicNamespace
,
someDataCenter
);
.
pollNotification
(
someAppId
,
someCluster
,
somePublicNamespace
,
someDataCenter
,
someClientIp
);
List
<
String
>
clusters
=
Lists
.
newArrayList
(
someCluster
,
someDataCenter
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
...
...
@@ -161,7 +163,7 @@ public class NotificationControllerTest {
public
void
testPollNotificationWithDefaultNamespaceAndHandleMessage
()
throws
Exception
{
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>
deferredResult
=
controller
.
pollNotification
(
someAppId
,
someCluster
,
defaultNamespace
,
someDataCenter
);
.
pollNotification
(
someAppId
,
someCluster
,
defaultNamespace
,
someDataCenter
,
someClientIp
);
String
key
=
Joiner
.
on
(
ConfigConsts
.
CLUSTER_NAMESPACE_SEPARATOR
)
...
...
@@ -188,7 +190,7 @@ public class NotificationControllerTest {
DeferredResult
<
ResponseEntity
<
ApolloConfigNotification
>>
deferredResult
=
controller
.
pollNotification
(
someAppId
,
someCluster
,
somePublicNamespace
,
someDataCenter
);
.
pollNotification
(
someAppId
,
someCluster
,
somePublicNamespace
,
someDataCenter
,
someClientIp
);
String
key
=
Joiner
.
on
(
ConfigConsts
.
CLUSTER_NAMESPACE_SEPARATOR
)
...
...
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/integration/ConfigControllerIntegrationTest.java
View file @
ea3563c5
...
...
@@ -38,8 +38,8 @@ public class ConfigControllerIntegrationTest extends AbstractBaseIntegrationTest
@Sql
(
scripts
=
"/integration-test/cleanup.sql"
,
executionPhase
=
Sql
.
ExecutionPhase
.
AFTER_TEST_METHOD
)
public
void
testQueryConfigWithDefaultClusterAndDefaultNamespaceOK
()
throws
Exception
{
ResponseEntity
<
ApolloConfig
>
response
=
restTemplate
.
getForEntity
(
"{baseurl}/configs/{appId}/{clusterName}"
,
ApolloConfig
.
class
,
getHostUrl
(),
someAppId
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
);
.
getForEntity
(
"{baseurl}/configs/{appId}/{clusterName}
/{namespace}
"
,
ApolloConfig
.
class
,
getHostUrl
(),
someAppId
,
ConfigConsts
.
CLUSTER_NAME_DEFAULT
,
ConfigConsts
.
NAMESPACE_DEFAULT
);
ApolloConfig
result
=
response
.
getBody
();
assertEquals
(
HttpStatus
.
OK
,
response
.
getStatusCode
());
...
...
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