Skip to content

Commit 317c236

Browse files
authored
Merge pull request #51 from sail-y/master
add muti consul support
2 parents 962a58b + 60026e8 commit 317c236

File tree

18 files changed

+825
-0
lines changed

18 files changed

+825
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<groupId>org.xujin.moss</groupId>
8+
<artifactId>moss-adapter</artifactId>
9+
<version>1.0.0.RELEASE</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<groupId>org.xujin.moss</groupId>
14+
<artifactId>moss-adapter-multi-consul</artifactId>
15+
<version>1.0.0.RELEASE</version>
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>com.google.guava</groupId>
21+
<artifactId>guava</artifactId>
22+
<version>25.1-jre</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.xujin.moss</groupId>
27+
<artifactId>moss-core</artifactId>
28+
<version>1.0.0.RELEASE</version>
29+
</dependency>
30+
31+
<!-- Optional Discovery Client -->
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-starter</artifactId>
35+
<optional>true</optional>
36+
</dependency>
37+
<!-- Optional consul Discovery Client -->
38+
<dependency>
39+
<groupId>org.springframework.cloud</groupId>
40+
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-autoconfigure-processor</artifactId>
45+
<optional>true</optional>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.google.code.findbugs</groupId>
49+
<artifactId>jsr305</artifactId>
50+
</dependency>
51+
<!-- Optional Configuration Processor for metadata -->
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-configuration-processor</artifactId>
55+
<optional>true</optional>
56+
</dependency>
57+
<!-- Test -->
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-test</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.fasterxml.jackson.datatype</groupId>
65+
<artifactId>jackson-datatype-json-org</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>io.projectreactor</groupId>
70+
<artifactId>reactor-test</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
</dependencies>
74+
75+
<build>
76+
<plugins>
77+
<plugin>
78+
<groupId>org.apache.maven.plugins</groupId>
79+
<artifactId>maven-resources-plugin</artifactId>
80+
<version>3.0.2</version>
81+
<executions>
82+
<execution>
83+
<id>copy-resources</id>
84+
<phase>validate</phase>
85+
<goals>
86+
<goal>copy-resources</goal>
87+
</goals>
88+
<configuration>
89+
<outputDirectory>${basedir}/target/classes</outputDirectory>
90+
<includeEmptyDirs>true</includeEmptyDirs>
91+
<resources>
92+
<resource>
93+
<directory>src/main/resource</directory>
94+
<filtering>true</filtering>
95+
</resource>
96+
</resources>
97+
</configuration>
98+
</execution>
99+
</executions>
100+
</plugin>
101+
<plugin>
102+
<artifactId>maven-compiler-plugin</artifactId>
103+
<version>3.7.0</version>
104+
<configuration>
105+
<source>1.8</source>
106+
<target>1.8</target>
107+
<encoding>UTF-8</encoding>
108+
</configuration>
109+
</plugin>
110+
<plugin>
111+
<groupId>org.apache.maven.plugins</groupId>
112+
<artifactId>maven-source-plugin</artifactId>
113+
<version>3.0.1</version>
114+
<executions>
115+
<execution>
116+
<id>attach-sources</id>
117+
<goals>
118+
<goal>jar-no-fork</goal>
119+
</goals>
120+
</execution>
121+
</executions>
122+
</plugin>
123+
</plugins>
124+
</build>
125+
</project>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.codecentric.boot.admin.server.cloud.discovery;
2+
3+
4+
import de.codecentric.boot.admin.server.domain.entities.Instance;
5+
import de.codecentric.boot.admin.server.domain.values.Registration;
6+
import org.springframework.cloud.client.ServiceInstance;
7+
8+
/**
9+
* Converts {@link ServiceInstance}s to {@link Instance}s.
10+
*
11+
* @author Johannes Edmeier
12+
*/
13+
public interface ServiceInstanceConverter {
14+
15+
/**
16+
* Converts a service instance to a application instance to be registered.
17+
*
18+
* @param instance the service instance.
19+
* @return Instance
20+
*/
21+
Registration convert(ServiceInstance instance);
22+
}

0 commit comments

Comments
 (0)