|
| 1 | +/* |
| 2 | + * Copyright 2014-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package de.codecentric.boot.admin.server.cloud.discovery; |
| 18 | + |
| 19 | +import de.codecentric.boot.admin.server.domain.entities.Instance; |
| 20 | +import de.codecentric.boot.admin.server.domain.values.Registration; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.cloud.client.ServiceInstance; |
| 24 | +import org.springframework.web.util.UriComponentsBuilder; |
| 25 | + |
| 26 | +import java.net.URI; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static java.util.Collections.emptyMap; |
| 30 | +import static org.springframework.util.StringUtils.isEmpty; |
| 31 | + |
| 32 | +/** |
| 33 | + * Converts any {@link ServiceInstance}s to {@link Instance}s. To customize the health- or |
| 34 | + * management-url for all instances you can set healthEndpointPath or managementContextPath |
| 35 | + * respectively. If you want to influence the url per service you can add |
| 36 | + * <code>management.context-path</code>, <code>management.port</code>, <code>management.address</code> or <code>health.path</code> |
| 37 | + * to the instances metadata. |
| 38 | + * |
| 39 | + * @author Johannes Edmeier |
| 40 | + */ |
| 41 | +public class DefaultServiceInstanceConverter implements ServiceInstanceConverter { |
| 42 | + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultServiceInstanceConverter.class); |
| 43 | + private static final String KEY_MANAGEMENT_PORT = "management.port"; |
| 44 | + private static final String KEY_MANAGEMENT_PATH = "management.context-path"; |
| 45 | + private static final String KEY_MANAGEMENT_ADDRESS = "management.address"; |
| 46 | + private static final String KEY_HEALTH_PATH = "health.path"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Default context-path to be appended to the url of the discovered service for the |
| 50 | + * managment-url. |
| 51 | + */ |
| 52 | + private String managementContextPath = "/actuator"; |
| 53 | + /** |
| 54 | + * Default path of the health-endpoint to be used for the health-url of the discovered service. |
| 55 | + */ |
| 56 | + private String healthEndpointPath = "health"; |
| 57 | + |
| 58 | + @Override |
| 59 | + public Registration convert(ServiceInstance instance) { |
| 60 | + LOGGER.debug( |
| 61 | + "Converting service '{}' running at '{}' with metadata {}", |
| 62 | + instance.getServiceId(), |
| 63 | + instance.getUri(), |
| 64 | + instance.getMetadata() |
| 65 | + ); |
| 66 | + |
| 67 | + return Registration.create(instance.getServiceId(), getHealthUrl(instance).toString()) |
| 68 | + .managementUrl(getManagementUrl(instance).toString()) |
| 69 | + .serviceUrl(getServiceUrl(instance).toString()) |
| 70 | + .metadata(getMetadata(instance)) |
| 71 | + .build(); |
| 72 | + } |
| 73 | + |
| 74 | + protected URI getHealthUrl(ServiceInstance instance) { |
| 75 | + String healthPath = instance.getMetadata().get(KEY_HEALTH_PATH); |
| 76 | + if (isEmpty(healthPath)) { |
| 77 | + healthPath = healthEndpointPath; |
| 78 | + } |
| 79 | + |
| 80 | + return UriComponentsBuilder.fromUri(getManagementUrl(instance)).path("/").path(healthPath).build().toUri(); |
| 81 | + } |
| 82 | + |
| 83 | + protected URI getManagementUrl(ServiceInstance instance) { |
| 84 | + String managementPath = instance.getMetadata().get(KEY_MANAGEMENT_PATH); |
| 85 | + if (isEmpty(managementPath)) { |
| 86 | + managementPath = managementContextPath; |
| 87 | + } |
| 88 | + |
| 89 | + URI serviceUrl = getServiceUrl(instance); |
| 90 | + |
| 91 | + String managementServerAddress = instance.getMetadata().get(KEY_MANAGEMENT_ADDRESS); |
| 92 | + if (isEmpty(managementServerAddress)) { |
| 93 | + managementServerAddress = serviceUrl.getHost(); |
| 94 | + } |
| 95 | + |
| 96 | + String managementPort = instance.getMetadata().get(KEY_MANAGEMENT_PORT); |
| 97 | + if (isEmpty(managementPort)) { |
| 98 | + managementPort = String.valueOf(serviceUrl.getPort()); |
| 99 | + } |
| 100 | + |
| 101 | + return UriComponentsBuilder.fromUri(serviceUrl) |
| 102 | + .host(managementServerAddress) |
| 103 | + .port(managementPort) |
| 104 | + .path("/") |
| 105 | + .path(managementPath) |
| 106 | + .build() |
| 107 | + .toUri(); |
| 108 | + } |
| 109 | + |
| 110 | + protected URI getServiceUrl(ServiceInstance instance) { |
| 111 | + return UriComponentsBuilder.fromUri(instance.getUri()).path("/").build().toUri(); |
| 112 | + } |
| 113 | + |
| 114 | + protected Map<String, String> getMetadata(ServiceInstance instance) { |
| 115 | + return instance.getMetadata() != null ? instance.getMetadata() : emptyMap(); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + public void setManagementContextPath(String managementContextPath) { |
| 120 | + this.managementContextPath = managementContextPath; |
| 121 | + } |
| 122 | + |
| 123 | + public String getManagementContextPath() { |
| 124 | + return managementContextPath; |
| 125 | + } |
| 126 | + |
| 127 | + public void setHealthEndpointPath(String healthEndpointPath) { |
| 128 | + this.healthEndpointPath = healthEndpointPath; |
| 129 | + } |
| 130 | + |
| 131 | + public String getHealthEndpointPath() { |
| 132 | + return healthEndpointPath; |
| 133 | + } |
| 134 | +} |
0 commit comments