Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-boot-admin
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-boot-admin
Commits
991119e9
Commit
991119e9
authored
Jun 10, 2016
by
Niklas Herder
Committed by
Johannes Edmeier
Jun 11, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove stale apps from registry.
Checks the DiscoveryClient's services on each heartbeat and removes all services, which are no longer registered.
parent
c9226b46
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
5 deletions
+43
-5
ApplicationDiscoveryListener.java
...ic/boot/admin/discovery/ApplicationDiscoveryListener.java
+21
-5
ApplicationDiscoveryListenerTest.java
...oot/admin/discovery/ApplicationDiscoveryListenerTest.java
+22
-0
No files found.
spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListener.java
View file @
991119e9
...
...
@@ -77,26 +77,42 @@ public class ApplicationDiscoveryListener {
}
protected
void
discover
()
{
final
Set
<
String
>
staleApplicationIds
=
getAllApplicationIdsFromRegistry
();
for
(
String
serviceId
:
discoveryClient
.
getServices
())
{
for
(
ServiceInstance
instance
:
discoveryClient
.
getInstances
(
serviceId
))
{
if
(!
ignoredServices
.
contains
(
serviceId
))
{
register
(
instance
);
if
(!
ignoredServices
.
contains
(
serviceId
))
{
for
(
ServiceInstance
instance
:
discoveryClient
.
getInstances
(
serviceId
))
{
String
applicationId
=
register
(
instance
);
staleApplicationIds
.
remove
(
applicationId
);
}
}
}
for
(
String
staleApplicationId
:
staleApplicationIds
)
{
LOGGER
.
info
(
"Application ({}) missing in DiscoveryClient services "
,
staleApplicationId
);
registry
.
deregister
(
staleApplicationId
);
}
}
protected
final
Set
<
String
>
getAllApplicationIdsFromRegistry
()
{
Set
<
String
>
result
=
new
HashSet
<>();
for
(
Application
application
:
registry
.
getApplications
())
{
result
.
add
(
application
.
getId
());
}
return
result
;
}
protected
void
register
(
ServiceInstance
instance
)
{
protected
String
register
(
ServiceInstance
instance
)
{
try
{
Application
application
=
converter
.
convert
(
instance
);
if
(
application
!=
null
)
{
re
gistry
.
register
(
application
);
re
turn
registry
.
register
(
application
).
getId
(
);
}
else
{
LOGGER
.
warn
(
"No application for service {} registered"
,
instance
);
}
}
catch
(
Exception
ex
)
{
LOGGER
.
error
(
"Couldn't register application for service {}"
,
instance
,
ex
);
}
return
null
;
}
public
void
setConverter
(
ServiceInstanceConverter
converter
)
{
...
...
spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/discovery/ApplicationDiscoveryListenerTest.java
View file @
991119e9
...
...
@@ -19,7 +19,9 @@ import static org.junit.Assert.assertEquals;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -95,4 +97,24 @@ public class ApplicationDiscoveryListenerTest {
assertEquals
(
1
,
registry
.
getApplications
().
size
());
}
@Test
public
void
deregister_removed_app
()
{
Object
heartbeat
=
new
Object
();
List
<
ServiceInstance
>
instances
=
new
ArrayList
<>();
instances
.
add
(
new
DefaultServiceInstance
(
"service"
,
"localhost"
,
80
,
false
));
instances
.
add
(
new
DefaultServiceInstance
(
"service"
,
"example.net"
,
80
,
false
));
when
(
discovery
.
getServices
()).
thenReturn
(
Collections
.
singletonList
(
"service"
));
when
(
discovery
.
getInstances
(
"service"
)).
thenReturn
(
instances
);
listener
.
onApplicationEvent
(
new
HeartbeatEvent
(
new
Object
(),
heartbeat
));
assertEquals
(
2
,
registry
.
getApplications
().
size
());
instances
.
remove
(
0
);
listener
.
onApplicationEvent
(
new
HeartbeatEvent
(
new
Object
(),
new
Object
()));
assertEquals
(
1
,
registry
.
getApplications
().
size
());
}
}
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