|
1 | 1 | /* |
2 | | - * Copyright 2014 the original author or authors. |
| 2 | + * Copyright 2014-2017 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
15 | 15 | */ |
16 | 16 | package de.codecentric.boot.admin.client.registration; |
17 | 17 |
|
| 18 | +import de.codecentric.boot.admin.client.config.AdminProperties; |
| 19 | + |
18 | 20 | import java.util.Collections; |
19 | 21 | import java.util.Map; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
20 | 23 | import java.util.concurrent.atomic.AtomicReference; |
21 | | - |
22 | 24 | import org.slf4j.Logger; |
23 | 25 | import org.slf4j.LoggerFactory; |
24 | 26 | import org.springframework.http.HttpEntity; |
|
28 | 30 | import org.springframework.http.ResponseEntity; |
29 | 31 | import org.springframework.web.client.RestTemplate; |
30 | 32 |
|
31 | | -import de.codecentric.boot.admin.client.config.AdminProperties; |
32 | | - |
33 | 33 | /** |
34 | 34 | * Registers the client application at spring-boot-admin-server |
35 | 35 | */ |
36 | 36 | public class ApplicationRegistrator { |
| 37 | + private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistrator.class); |
| 38 | + private static final HttpHeaders HTTP_HEADERS = createHttpHeaders(); |
| 39 | + private final AtomicInteger unsuccessfulAttempts = new AtomicInteger(0); |
| 40 | + private final AtomicReference<String> registeredId = new AtomicReference<>(); |
| 41 | + private final AdminProperties admin; |
| 42 | + private final RestTemplate template; |
| 43 | + private final ApplicationFactory applicationFactory; |
37 | 44 |
|
38 | | - private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistrator.class); |
39 | | - private static final HttpHeaders HTTP_HEADERS = createHttpHeaders(); |
40 | | - private final AtomicReference<String> registeredId = new AtomicReference<>(); |
41 | | - private final AdminProperties admin; |
42 | | - private final RestTemplate template; |
43 | | - private final ApplicationFactory applicationFactory; |
44 | | - |
45 | | - public ApplicationRegistrator(RestTemplate template, AdminProperties admin, ApplicationFactory applicationFactory) { |
46 | | - this.admin = admin; |
47 | | - this.template = template; |
48 | | - this.applicationFactory = applicationFactory; |
49 | | - } |
| 45 | + public ApplicationRegistrator(RestTemplate template, AdminProperties admin, ApplicationFactory applicationFactory) { |
| 46 | + this.admin = admin; |
| 47 | + this.template = template; |
| 48 | + this.applicationFactory = applicationFactory; |
| 49 | + } |
50 | 50 |
|
51 | | - private static HttpHeaders createHttpHeaders() { |
52 | | - HttpHeaders headers = new HttpHeaders(); |
53 | | - headers.setContentType(MediaType.APPLICATION_JSON); |
54 | | - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); |
55 | | - return HttpHeaders.readOnlyHttpHeaders(headers); |
56 | | - } |
| 51 | + private static HttpHeaders createHttpHeaders() { |
| 52 | + HttpHeaders headers = new HttpHeaders(); |
| 53 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 54 | + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); |
| 55 | + return HttpHeaders.readOnlyHttpHeaders(headers); |
| 56 | + } |
57 | 57 |
|
58 | | - /** |
59 | | - * Registers the client application at spring-boot-admin-server. |
60 | | - * |
61 | | - * @return true if successful registration on at least one admin server |
62 | | - */ |
63 | | - public boolean register() { |
64 | | - boolean isRegistrationSuccessful = false; |
65 | | - Application self = createApplication(); |
66 | | - for (String adminUrl : admin.getAdminUrl()) { |
67 | | - try { |
68 | | - @SuppressWarnings("rawtypes") |
69 | | - ResponseEntity<Map> response = template.postForEntity(adminUrl, |
70 | | - new HttpEntity<>(self, HTTP_HEADERS), Map.class); |
| 58 | + /** |
| 59 | + * Registers the client application at spring-boot-admin-server. |
| 60 | + * |
| 61 | + * @return true if successful registration on at least one admin server |
| 62 | + */ |
| 63 | + public boolean register() { |
| 64 | + boolean isRegistrationSuccessful = false; |
| 65 | + Application self = createApplication(); |
| 66 | + for (String adminUrl : admin.getAdminUrl()) { |
| 67 | + try { |
| 68 | + @SuppressWarnings("rawtypes") ResponseEntity<Map> response = template.postForEntity(adminUrl, |
| 69 | + new HttpEntity<>(self, HTTP_HEADERS), Map.class); |
71 | 70 |
|
72 | | - if (response.getStatusCode().equals(HttpStatus.CREATED)) { |
73 | | - if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) { |
74 | | - LOGGER.info("Application registered itself as {}", response.getBody()); |
75 | | - } else { |
76 | | - LOGGER.debug("Application refreshed itself as {}", response.getBody()); |
77 | | - } |
| 71 | + if (response.getStatusCode().equals(HttpStatus.CREATED)) { |
| 72 | + if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) { |
| 73 | + LOGGER.info("Application registered itself as {}", response.getBody()); |
| 74 | + } else { |
| 75 | + LOGGER.debug("Application refreshed itself as {}", response.getBody()); |
| 76 | + } |
78 | 77 |
|
79 | | - isRegistrationSuccessful = true; |
80 | | - if (admin.isRegisterOnce()) { |
81 | | - break; |
82 | | - } |
83 | | - } else { |
84 | | - LOGGER.warn("Application failed to registered itself as {}. Response: {}", self, |
85 | | - response.toString()); |
86 | | - } |
87 | | - } catch (Exception ex) { |
88 | | - LOGGER.warn("Failed to register application as {} at spring-boot-admin ({}): {}", |
89 | | - self, admin.getAdminUrl(), ex.getMessage()); |
90 | | - } |
91 | | - } |
| 78 | + isRegistrationSuccessful = true; |
| 79 | + if (admin.isRegisterOnce()) { |
| 80 | + break; |
| 81 | + } |
| 82 | + } else { |
| 83 | + if (unsuccessfulAttempts.get() == 0) { |
| 84 | + LOGGER.warn( |
| 85 | + "Application failed to registered itself as {}. Response: {}. Further attempts are logged on DEBUG level", |
| 86 | + self, response.toString()); |
| 87 | + } else { |
| 88 | + LOGGER.debug("Application failed to registered itself as {}. Response: {}", self, |
| 89 | + response.toString()); |
| 90 | + } |
| 91 | + } |
| 92 | + } catch (Exception ex) { |
| 93 | + if (unsuccessfulAttempts.get() == 0) { |
| 94 | + LOGGER.warn( |
| 95 | + "Failed to register application as {} at spring-boot-admin ({}): {}. Further attempts are logged on DEBUG level", |
| 96 | + self, admin.getAdminUrl(), ex.getMessage()); |
| 97 | + } else { |
| 98 | + LOGGER.debug("Failed to register application as {} at spring-boot-admin ({}): {}", self, |
| 99 | + admin.getAdminUrl(), ex.getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + if (!isRegistrationSuccessful) { |
| 104 | + unsuccessfulAttempts.incrementAndGet(); |
| 105 | + } else { |
| 106 | + unsuccessfulAttempts.set(0); |
| 107 | + } |
| 108 | + return isRegistrationSuccessful; |
| 109 | + } |
92 | 110 |
|
93 | | - return isRegistrationSuccessful; |
94 | | - } |
| 111 | + public void deregister() { |
| 112 | + String id = registeredId.get(); |
| 113 | + if (id != null) { |
| 114 | + for (String adminUrl : admin.getAdminUrl()) { |
| 115 | + try { |
| 116 | + template.delete(adminUrl + "/" + id); |
| 117 | + registeredId.compareAndSet(id, null); |
| 118 | + if (admin.isRegisterOnce()) { |
| 119 | + break; |
| 120 | + } |
| 121 | + } catch (Exception ex) { |
| 122 | + LOGGER.warn("Failed to deregister application (id={}) at spring-boot-admin ({}): {}", id, adminUrl, |
| 123 | + ex.getMessage()); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
95 | 128 |
|
96 | | - public void deregister() { |
97 | | - String id = registeredId.get(); |
98 | | - if (id != null) { |
99 | | - for (String adminUrl : admin.getAdminUrl()) { |
100 | | - try { |
101 | | - template.delete(adminUrl + "/" + id); |
102 | | - registeredId.compareAndSet(id, null); |
103 | | - if (admin.isRegisterOnce()) { |
104 | | - break; |
105 | | - } |
106 | | - } catch (Exception ex) { |
107 | | - LOGGER.warn( |
108 | | - "Failed to deregister application (id={}) at spring-boot-admin ({}): {}", |
109 | | - id, adminUrl, ex.getMessage()); |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - } |
114 | | - |
115 | | - /** |
116 | | - * Returns the id of this client as given by the admin server. |
117 | | - * Returns null if the client has not registered against the admin server yet. |
118 | | - * |
119 | | - * @return |
120 | | - */ |
121 | | - public String getRegisteredId() { |
122 | | - return registeredId.get(); |
123 | | - } |
| 129 | + /** |
| 130 | + * Returns the id of this client as given by the admin server. |
| 131 | + * Returns null if the client has not registered against the admin server yet. |
| 132 | + * |
| 133 | + * @return |
| 134 | + */ |
| 135 | + public String getRegisteredId() { |
| 136 | + return registeredId.get(); |
| 137 | + } |
124 | 138 |
|
125 | | - protected Application createApplication() { |
126 | | - return applicationFactory.createApplication(); |
127 | | - } |
| 139 | + protected Application createApplication() { |
| 140 | + return applicationFactory.createApplication(); |
| 141 | + } |
128 | 142 | } |
0 commit comments