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
e9bba5d6
Commit
e9bba5d6
authored
Apr 17, 2015
by
Johannes Stelzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename SpringBootAdminRegistrator to ApplicationRegistrator; Do some polish and add deregister()
parent
7ad11421
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
86 additions
and
59 deletions
+86
-59
application.yml
...ring-boot-admin-sample/src/main/resources/application.yml
+1
-1
SpringBootAdminClientAutoConfiguration.java
.../admin/config/SpringBootAdminClientAutoConfiguration.java
+4
-4
ApplicationRegistrator.java
...decentric/boot/admin/services/ApplicationRegistrator.java
+48
-19
SpringBootAdminClientAutoConfigurationTest.java
...in/config/SpringBootAdminClientAutoConfigurationTest.java
+5
-5
ApplicationRegistratorTest.java
...ntric/boot/admin/services/ApplicationRegistratorTest.java
+28
-30
No files found.
spring-boot-admin-samples/spring-boot-admin-sample/src/main/resources/application.yml
View file @
e9bba5d6
...
...
@@ -3,7 +3,7 @@ info:
stage
:
test
logging
:
file
:
boot-admin-sample.log
file
:
target/
boot-admin-sample.log
spring
:
application
:
...
...
spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/config/SpringBootAdminClientAutoConfiguration.java
View file @
e9bba5d6
...
...
@@ -29,7 +29,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import
org.springframework.web.client.RestTemplate
;
import
de.codecentric.boot.admin.actuate.LogfileMvcEndpoint
;
import
de.codecentric.boot.admin.services.
SpringBootAdmi
nRegistrator
;
import
de.codecentric.boot.admin.services.
Applicatio
nRegistrator
;
import
de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor
;
/**
...
...
@@ -46,8 +46,8 @@ public class SpringBootAdminClientAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public
SpringBootAdmi
nRegistrator
registrator
(
AdminProperties
adminProps
,
AdminClientProperties
clientProps
)
{
return
new
SpringBootAdmi
nRegistrator
(
createRestTemplate
(
adminProps
),
adminProps
,
clientProps
);
public
Applicatio
nRegistrator
registrator
(
AdminProperties
adminProps
,
AdminClientProperties
clientProps
)
{
return
new
Applicatio
nRegistrator
(
createRestTemplate
(
adminProps
),
adminProps
,
clientProps
);
}
protected
RestTemplate
createRestTemplate
(
AdminProperties
adminProps
)
{
...
...
@@ -66,7 +66,7 @@ public class SpringBootAdminClientAutoConfiguration {
* TaskRegistrar that triggers the RegistratorTask every ten seconds.
*/
@Bean
public
ScheduledTaskRegistrar
taskRegistrar
(
final
SpringBootAdmi
nRegistrator
registrator
,
AdminProperties
adminProps
)
{
public
ScheduledTaskRegistrar
taskRegistrar
(
final
Applicatio
nRegistrator
registrator
,
AdminProperties
adminProps
)
{
ScheduledTaskRegistrar
registrar
=
new
ScheduledTaskRegistrar
();
Runnable
registratorTask
=
new
Runnable
()
{
...
...
spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/services/
SpringBootAdmi
nRegistrator.java
→
spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/services/
Applicatio
nRegistrator.java
View file @
e9bba5d6
...
...
@@ -16,6 +16,7 @@
package
de
.
codecentric
.
boot
.
admin
.
services
;
import
java.util.Collections
;
import
java.util.concurrent.atomic.AtomicReference
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -33,22 +34,25 @@ import de.codecentric.boot.admin.model.Application;
/**
* Registers the client application at spring-boot-admin-server
*/
public
class
SpringBootAdmi
nRegistrator
{
public
class
Applicatio
nRegistrator
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
SpringBootAdminRegistrator
.
class
);
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ApplicationRegistrator
.
class
);
private
static
HttpHeaders
HTTP_HEADERS
=
createHttpHeaders
();
private
AdminClientProperties
clientProps
;
private
final
AtomicReference
<
Application
>
registeredSelf
=
new
AtomicReference
<
Application
>()
;
private
AdminProperties
adminProps
;
private
AdminClientProperties
client
;
private
AdminProperties
admin
;
private
final
RestTemplate
template
;
public
SpringBootAdminRegistrator
(
RestTemplate
template
,
AdminProperties
adminProps
,
AdminClientProperties
client
Props
)
{
this
.
client
Props
=
clientProps
;
this
.
admin
Props
=
adminProps
;
public
ApplicationRegistrator
(
RestTemplate
template
,
AdminProperties
admin
,
AdminClientProperties
client
)
{
this
.
client
=
client
;
this
.
admin
=
admin
;
this
.
template
=
template
;
}
...
...
@@ -64,34 +68,59 @@ public class SpringBootAdminRegistrator {
* @return true if successful
*/
public
boolean
register
()
{
Application
app
=
createApplication
();
String
adminUrl
=
admin
Props
.
getUrl
()
+
'/'
+
adminProps
.
getContextPath
();
Application
self
=
createApplication
();
String
adminUrl
=
admin
.
getUrl
()
+
'/'
+
admin
.
getContextPath
();
try
{
ResponseEntity
<
Application
>
response
=
template
.
postForEntity
(
adminUrl
,
new
HttpEntity
<
Application
>(
app
,
HTTP_HEADERS
),
Application
.
class
);
new
HttpEntity
<
Application
>(
self
,
HTTP_HEADERS
),
Application
.
class
);
if
(
response
.
getStatusCode
().
equals
(
HttpStatus
.
CREATED
))
{
LOGGER
.
debug
(
"Application registered itself as {}"
,
response
.
getBody
());
if
(
registeredSelf
.
get
()
==
null
)
{
if
(
registeredSelf
.
compareAndSet
(
null
,
response
.
getBody
()))
{
LOGGER
.
info
(
"Application registered itself as {}"
,
response
.
getBody
());
return
true
;
}
else
if
(
response
.
getStatusCode
().
equals
(
HttpStatus
.
CONFLICT
))
{
LOGGER
.
warn
(
"Application failed to registered itself as {} because of conflict in registry."
,
app
);
}
LOGGER
.
debug
(
"Application refreshed itself as {}"
,
response
.
getBody
());
return
true
;
}
else
{
LOGGER
.
warn
(
"Application failed to registered itself as {}. Response: {}"
,
app
,
response
.
toString
());
LOGGER
.
warn
(
"Application failed to registered itself as {}. Response: {}"
,
self
,
response
.
toString
());
}
}
catch
(
Exception
ex
)
{
LOGGER
.
warn
(
"Failed to register application as {} at spring-boot-admin ({}): {}"
,
app
,
adminUrl
,
ex
.
getMessage
());
LOGGER
.
warn
(
"Failed to register application as {} at spring-boot-admin ({}): {}"
,
self
,
adminUrl
,
ex
.
getMessage
());
}
return
false
;
}
public
void
deregister
()
{
Application
self
=
registeredSelf
.
get
();
if
(
self
!=
null
)
{
String
adminUrl
=
admin
.
getUrl
()
+
'/'
+
admin
.
getContextPath
()
+
"/"
+
self
.
getId
();
registeredSelf
.
set
(
null
);
try
{
template
.
delete
(
adminUrl
);
}
catch
(
Exception
ex
)
{
LOGGER
.
warn
(
"Failed to deregister application as {} at spring-boot-admin ({}): {}"
,
self
,
adminUrl
,
ex
.
getMessage
());
}
}
}
protected
Application
createApplication
()
{
Application
app
=
new
Application
(
clientProps
.
getUrl
(),
clientProps
.
getName
());
return
app
;
return
new
Application
(
client
.
getUrl
(),
client
.
getName
());
}
}
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/config/SpringBootAdminClientAutoConfigurationTest.java
View file @
e9bba5d6
...
...
@@ -10,7 +10,7 @@ import org.springframework.boot.test.EnvironmentTestUtils;
import
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
;
import
de.codecentric.boot.admin.actuate.LogfileMvcEndpoint
;
import
de.codecentric.boot.admin.services.
SpringBootAdmi
nRegistrator
;
import
de.codecentric.boot.admin.services.
Applicatio
nRegistrator
;
public
class
SpringBootAdminClientAutoConfigurationTest
{
...
...
@@ -26,13 +26,13 @@ public class SpringBootAdminClientAutoConfigurationTest {
@Test
public
void
not_active
()
{
load
();
assertTrue
(
context
.
getBeansOfType
(
SpringBootAdmi
nRegistrator
.
class
).
isEmpty
());
assertTrue
(
context
.
getBeansOfType
(
Applicatio
nRegistrator
.
class
).
isEmpty
());
}
@Test
public
void
active_nologfile
()
{
load
(
"spring.boot.admin.url:http://localhost:8081"
);
context
.
getBean
(
SpringBootAdmi
nRegistrator
.
class
);
context
.
getBean
(
Applicatio
nRegistrator
.
class
);
assertTrue
(
context
.
getBeansOfType
(
LogfileMvcEndpoint
.
class
).
isEmpty
());
}
...
...
@@ -40,14 +40,14 @@ public class SpringBootAdminClientAutoConfigurationTest {
public
void
active_logfile
()
{
load
(
"spring.boot.admin.url:http://localhost:8081"
,
"logging.file:spring.log"
);
context
.
getBean
(
LogfileMvcEndpoint
.
class
);
context
.
getBean
(
SpringBootAdmi
nRegistrator
.
class
);
context
.
getBean
(
Applicatio
nRegistrator
.
class
);
}
@Test
public
void
active_logfile_supressed
()
{
load
(
"spring.boot.admin.url:http://localhost:8081"
,
"logging.file:spring.log"
,
"endpoints.logfile.enabled:false"
);
context
.
getBean
(
SpringBootAdmi
nRegistrator
.
class
);
context
.
getBean
(
Applicatio
nRegistrator
.
class
);
assertTrue
(
context
.
getBeansOfType
(
LogfileMvcEndpoint
.
class
).
isEmpty
());
}
...
...
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/services/
SpringBootAdmi
nRegistratorTest.java
→
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/services/
Applicatio
nRegistratorTest.java
View file @
e9bba5d6
...
...
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.when;
import
java.util.Collections
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
...
...
@@ -38,10 +39,16 @@ import de.codecentric.boot.admin.config.AdminClientProperties;
import
de.codecentric.boot.admin.config.AdminProperties
;
import
de.codecentric.boot.admin.model.Application
;
public
class
SpringBootAdminRegistratorTest
{
public
class
ApplicationRegistratorTest
{
private
ApplicationRegistrator
registrator
;
private
RestTemplate
restTemplate
;
private
HttpHeaders
headers
;
@Before
public
void
setup
()
{
restTemplate
=
mock
(
RestTemplate
.
class
);
@Test
public
void
register_successful
()
{
AdminProperties
adminProps
=
new
AdminProperties
();
adminProps
.
setUrl
(
"http://sba:8080"
);
...
...
@@ -49,19 +56,22 @@ public class SpringBootAdminRegistratorTest {
clientProps
.
setUrl
(
"http://localhost:8080"
);
clientProps
.
setName
(
"AppName"
);
RestTemplate
restTemplate
=
mock
(
RestTemplate
.
class
);
registrator
=
new
ApplicationRegistrator
(
restTemplate
,
adminProps
,
clientProps
);
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
headers
.
setAccept
(
Collections
.
singletonList
(
MediaType
.
APPLICATION_JSON
));
}
@Test
public
void
register_successful
()
{
when
(
restTemplate
.
postForEntity
(
isA
(
String
.
class
),
isA
(
HttpEntity
.
class
),
eq
(
Application
.
class
))).
thenReturn
(
new
ResponseEntity
<
Application
>(
HttpStatus
.
CREATED
));
SpringBootAdminRegistrator
registrator
=
new
SpringBootAdminRegistrator
(
restTemplate
,
adminProps
,
clientProps
);
boolean
result
=
registrator
.
register
();
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
headers
.
setAccept
(
Collections
.
singletonList
(
MediaType
.
APPLICATION_JSON
));
assertTrue
(
result
);
verify
(
restTemplate
).
postForEntity
(
"http://sba:8080/api/applications"
,
new
HttpEntity
<
Application
>(
new
Application
(
"http://localhost:8080"
,
...
...
@@ -71,38 +81,26 @@ public class SpringBootAdminRegistratorTest {
@Test
public
void
register_failed
()
{
AdminProperties
adminProps
=
new
AdminProperties
();
adminProps
.
setUrl
(
"http://sba:8080"
);
AdminClientProperties
clientProps
=
new
AdminClientProperties
();
clientProps
.
setUrl
(
"http://localhost:8080"
);
clientProps
.
setName
(
"AppName"
);
RestTemplate
restTemplate
=
mock
(
RestTemplate
.
class
);
when
(
restTemplate
.
postForEntity
(
isA
(
String
.
class
),
isA
(
Application
.
class
),
eq
(
Application
.
class
))).
thenThrow
(
new
RestClientException
(
"Error"
));
SpringBootAdminRegistrator
registrator
=
new
SpringBootAdminRegistrator
(
restTemplate
,
adminProps
,
clientProps
);
boolean
result
=
registrator
.
register
();
assertFalse
(
result
);
}
@Test
public
void
register_failed_conflict
()
{
AdminProperties
adminProps
=
new
AdminProperties
();
adminProps
.
setUrl
(
"http://sba:8080"
);
AdminClientProperties
clientProps
=
new
AdminClientProperties
();
clientProps
.
setUrl
(
"http://localhost:8080"
);
clientProps
.
setName
(
"AppName"
);
RestTemplate
restTemplate
=
mock
(
RestTemplate
.
class
);
when
(
restTemplate
.
postForEntity
(
isA
(
String
.
class
),
isA
(
Application
.
class
),
eq
(
Application
.
class
))).
thenReturn
(
new
ResponseEntity
<
Application
>(
HttpStatus
.
CONFLICT
));
public
void
deregister
()
{
when
(
restTemplate
.
postForEntity
(
isA
(
String
.
class
),
isA
(
HttpEntity
.
class
),
eq
(
Application
.
class
))).
thenReturn
(
new
ResponseEntity
<
Application
>(
new
Application
(
"http://test"
,
"url"
,
"-id-"
),
HttpStatus
.
CREATED
));
SpringBootAdminRegistrator
registrator
=
new
SpringBootAdminRegistrator
(
restTemplate
,
adminProps
,
clientProps
);
boolean
result
=
registrator
.
register
();
registrator
.
register
(
);
registrator
.
de
register
();
assertFalse
(
result
);
verify
(
restTemplate
).
delete
(
"http://sba:8080/api/applications/-id-"
);
}
}
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