Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
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
spring-cloud-netflix
Commits
eb0681f9
Unverified
Commit
eb0681f9
authored
Jan 29, 2018
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Get dynamic instanceinfo in ServiceRegistry.getStatus()
See
https://github.com/spring-cloud/spring-cloud-commons/issues/310
parent
041b493a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
94 additions
and
5 deletions
+94
-5
CloudEurekaClient.java
...ringframework/cloud/netflix/eureka/CloudEurekaClient.java
+12
-1
EurekaServiceRegistry.java
...netflix/eureka/serviceregistry/EurekaServiceRegistry.java
+11
-3
EurekaServiceRegistryTests.java
...ix/eureka/serviceregistry/EurekaServiceRegistryTests.java
+71
-1
No files found.
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaClient.java
View file @
eb0681f9
/*
* Copyright 2013-201
5
the original author or authors.
* Copyright 2013-201
8
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -24,6 +24,7 @@ import org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.cloud.client.discovery.event.HeartbeatEvent
;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.util.ReflectionUtils
;
import
com.netflix.appinfo.ApplicationInfoManager
;
...
...
@@ -33,6 +34,7 @@ import com.netflix.discovery.AbstractDiscoveryClientOptionalArgs;
import
com.netflix.discovery.DiscoveryClient
;
import
com.netflix.discovery.EurekaClientConfig
;
import
com.netflix.discovery.shared.transport.EurekaHttpClient
;
import
com.netflix.discovery.shared.transport.EurekaHttpResponse
;
/**
* Subclass of {@link DiscoveryClient} that sends a {@link HeartbeatEvent} when
...
...
@@ -73,6 +75,15 @@ public class CloudEurekaClient extends DiscoveryClient {
getEurekaHttpClient
().
deleteStatusOverride
(
info
.
getAppName
(),
info
.
getId
(),
info
);
}
public
InstanceInfo
getInstanceInfo
(
String
appname
,
String
instanceId
)
{
EurekaHttpResponse
<
InstanceInfo
>
response
=
getEurekaHttpClient
().
getInstance
(
appname
,
instanceId
);
HttpStatus
httpStatus
=
HttpStatus
.
valueOf
(
response
.
getStatusCode
());
if
(
httpStatus
.
is2xxSuccessful
()
&&
response
.
getEntity
()
!=
null
)
{
return
response
.
getEntity
();
}
return
null
;
}
EurekaHttpClient
getEurekaHttpClient
()
{
if
(
this
.
eurekaHttpClient
.
get
()
==
null
)
{
try
{
...
...
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaServiceRegistry.java
View file @
eb0681f9
/*
* Copyright 2013-201
6
the original author or authors.
* Copyright 2013-201
8
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -25,6 +25,8 @@ import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import
com.netflix.appinfo.InstanceInfo
;
import
static
com
.
netflix
.
appinfo
.
InstanceInfo
.
InstanceStatus
.
UNKNOWN
;
/**
* @author Spencer Gibb
*/
...
...
@@ -89,11 +91,17 @@ public class EurekaServiceRegistry implements ServiceRegistry<EurekaRegistration
@Override
public
Object
getStatus
(
EurekaRegistration
registration
)
{
HashMap
<
String
,
Object
>
status
=
new
HashMap
<>();
String
appname
=
registration
.
getInstanceConfig
().
getAppname
();
String
instanceId
=
registration
.
getInstanceConfig
().
getInstanceId
();
InstanceInfo
info
=
registration
.
getEurekaClient
().
getInstanceInfo
(
appname
,
instanceId
);
InstanceInfo
info
=
registration
.
getApplicationInfoManager
().
getInfo
();
HashMap
<
String
,
Object
>
status
=
new
HashMap
<>();
if
(
info
!=
null
)
{
status
.
put
(
"status"
,
info
.
getStatus
().
toString
());
status
.
put
(
"overriddenStatus"
,
info
.
getOverriddenStatus
().
toString
());
}
else
{
status
.
put
(
"status"
,
UNKNOWN
.
toString
());
}
return
status
;
}
...
...
spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaServiceRegistryTests.java
View file @
eb0681f9
/*
* Copyright 2013-201
7
the original author or authors.
* Copyright 2013-201
8
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -17,6 +17,8 @@
package
org
.
springframework
.
cloud
.
netflix
.
eureka
.
serviceregistry
;
import
java.util.Map
;
import
org.junit.Test
;
import
org.springframework.cloud.commons.util.InetUtils
;
import
org.springframework.cloud.commons.util.InetUtilsProperties
;
...
...
@@ -28,6 +30,9 @@ import org.springframework.context.ApplicationEventPublisher;
import
com.netflix.appinfo.ApplicationInfoManager
;
import
com.netflix.appinfo.InstanceInfo
;
import
static
com
.
netflix
.
appinfo
.
InstanceInfo
.
InstanceStatus
.
DOWN
;
import
static
com
.
netflix
.
appinfo
.
InstanceInfo
.
InstanceStatus
.
UNKNOWN
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verifyZeroInteractions
;
import
static
org
.
mockito
.
Mockito
.
when
;
...
...
@@ -57,4 +62,69 @@ public class EurekaServiceRegistryTests {
verifyZeroInteractions
(
eurekaClient
);
}
@Test
public
void
eurekaClientGetStatus
()
{
EurekaServiceRegistry
registry
=
new
EurekaServiceRegistry
();
EurekaInstanceConfigBean
config
=
new
EurekaInstanceConfigBean
(
new
InetUtils
(
new
InetUtilsProperties
()));
config
.
setAppname
(
"myapp"
);
config
.
setInstanceId
(
"1234"
);
CloudEurekaClient
eurekaClient
=
mock
(
CloudEurekaClient
.
class
);
InstanceInfo
instanceInfo
=
InstanceInfo
.
Builder
.
newBuilder
()
.
setAppName
(
"myapp"
)
.
setInstanceId
(
"1234"
)
.
setStatus
(
DOWN
)
.
setOverriddenStatus
(
UNKNOWN
)
.
build
();
when
(
eurekaClient
.
getInstanceInfo
(
"myapp"
,
"1234"
))
.
thenReturn
(
instanceInfo
);
EurekaRegistration
registration
=
EurekaRegistration
.
builder
(
config
)
.
with
(
eurekaClient
)
.
with
(
mock
(
ApplicationInfoManager
.
class
))
.
with
(
new
EurekaClientConfigBean
(),
mock
(
ApplicationEventPublisher
.
class
))
.
build
();
Object
status
=
registry
.
getStatus
(
registration
);
assertThat
(
status
).
isInstanceOf
(
Map
.
class
);
Map
<
Object
,
Object
>
map
=
(
Map
<
Object
,
Object
>)
status
;
assertThat
(
map
).
hasSize
(
2
)
.
containsEntry
(
"status"
,
DOWN
.
toString
())
.
containsEntry
(
"overriddenStatus"
,
UNKNOWN
.
toString
());
}
@Test
public
void
eurekaClientGetStatusNoInstance
()
{
EurekaServiceRegistry
registry
=
new
EurekaServiceRegistry
();
EurekaInstanceConfigBean
config
=
new
EurekaInstanceConfigBean
(
new
InetUtils
(
new
InetUtilsProperties
()));
config
.
setAppname
(
"myapp"
);
config
.
setInstanceId
(
"1234"
);
CloudEurekaClient
eurekaClient
=
mock
(
CloudEurekaClient
.
class
);
when
(
eurekaClient
.
getInstanceInfo
(
"myapp"
,
"1234"
))
.
thenReturn
(
null
);
EurekaRegistration
registration
=
EurekaRegistration
.
builder
(
config
)
.
with
(
eurekaClient
)
.
with
(
mock
(
ApplicationInfoManager
.
class
))
.
with
(
new
EurekaClientConfigBean
(),
mock
(
ApplicationEventPublisher
.
class
))
.
build
();
Object
status
=
registry
.
getStatus
(
registration
);
assertThat
(
status
).
isInstanceOf
(
Map
.
class
);
Map
<
Object
,
Object
>
map
=
(
Map
<
Object
,
Object
>)
status
;
assertThat
(
map
).
hasSize
(
1
)
.
containsEntry
(
"status"
,
UNKNOWN
.
toString
());
}
}
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