Skip to content

Commit bef1043

Browse files
authored
Merge pull request #34 from VancySavoki/dev
支持单节点 ZooKeeper、注册信息优化、h2 支持
2 parents e6dffb0 + 4b1e6f9 commit bef1043

File tree

108 files changed

+3459
-2083
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+3459
-2083
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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-zookeeper</artifactId>
15+
<version>1.0.0.RELEASE</version>
16+
<dependencies>
17+
18+
<dependency>
19+
<groupId>com.google.guava</groupId>
20+
<artifactId>guava</artifactId>
21+
<version>25.1-jre</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.xujin.moss</groupId>
26+
<artifactId>moss-core</artifactId>
27+
<version>1.0.0.RELEASE</version>
28+
</dependency>
29+
30+
<!-- Optional Discovery Client -->
31+
<dependency>
32+
<groupId>org.springframework.cloud</groupId>
33+
<artifactId>spring-cloud-starter</artifactId>
34+
<optional>true</optional>
35+
</dependency>
36+
<!-- Optional ZooKeeper Discovery Client -->
37+
<dependency>
38+
<groupId>org.springframework.cloud</groupId>
39+
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
40+
<exclusions>
41+
<exclusion>
42+
<artifactId>commons-logging</artifactId>
43+
<groupId>commons-logging</groupId>
44+
</exclusion>
45+
</exclusions>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-autoconfigure-processor</artifactId>
50+
<optional>true</optional>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.code.findbugs</groupId>
54+
<artifactId>jsr305</artifactId>
55+
</dependency>
56+
<!-- Optional Configuration Processor for metadata -->
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-configuration-processor</artifactId>
60+
<optional>true</optional>
61+
</dependency>
62+
<!-- Test -->
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-test</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.fasterxml.jackson.datatype</groupId>
70+
<artifactId>jackson-datatype-json-org</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>io.projectreactor</groupId>
75+
<artifactId>reactor-test</artifactId>
76+
<scope>test</scope>
77+
</dependency>
78+
</dependencies>
79+
80+
81+
<build>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-resources-plugin</artifactId>
86+
<version>3.0.2</version>
87+
<executions>
88+
<execution>
89+
<id>copy-resources</id>
90+
<phase>validate</phase>
91+
<goals>
92+
<goal>copy-resources</goal>
93+
</goals>
94+
<configuration>
95+
<outputDirectory>${basedir}/target/classes</outputDirectory>
96+
<includeEmptyDirs>true</includeEmptyDirs>
97+
<resources>
98+
<resource>
99+
<directory>src/main/resource</directory>
100+
<filtering>true</filtering>
101+
</resource>
102+
</resources>
103+
</configuration>
104+
</execution>
105+
</executions>
106+
</plugin>
107+
<plugin>
108+
<artifactId>maven-compiler-plugin</artifactId>
109+
<version>3.7.0</version>
110+
<configuration>
111+
<source>1.8</source>
112+
<target>1.8</target>
113+
<encoding>UTF-8</encoding>
114+
</configuration>
115+
</plugin>
116+
<plugin>
117+
<groupId>org.apache.maven.plugins</groupId>
118+
<artifactId>maven-source-plugin</artifactId>
119+
<version>3.0.1</version>
120+
<executions>
121+
<execution>
122+
<id>attach-sources</id>
123+
<goals>
124+
<goal>jar-no-fork</goal>
125+
</goals>
126+
</execution>
127+
</executions>
128+
</plugin>
129+
</plugins>
130+
</build>
131+
</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+
22+
import java.net.URI;
23+
import java.util.Map;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.cloud.client.ServiceInstance;
27+
import org.springframework.web.util.UriComponentsBuilder;
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
7+
import org.springframework.cloud.client.ServiceInstance;
8+
9+
/**
10+
* Converts {@link ServiceInstance}s to {@link Instance}s.
11+
*
12+
* @author Johannes Edmeier
13+
*/
14+
public interface ServiceInstanceConverter {
15+
16+
/**
17+
* Converts a service instance to a application instance to be registered.
18+
*
19+
* @param instance the service instance.
20+
* @return Instance
21+
*/
22+
Registration convert(ServiceInstance instance);
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package de.codecentric.boot.admin.server.cloud.discovery;
2+
3+
import de.codecentric.boot.admin.server.domain.entities.Instance;
4+
5+
import java.net.URI;
6+
import org.springframework.cloud.client.ServiceInstance;
7+
8+
9+
import org.springframework.cloud.zookeeper.discovery.ZookeeperServiceInstance;
10+
import org.springframework.util.Assert;
11+
import org.springframework.web.util.UriComponentsBuilder;
12+
13+
import static org.springframework.util.StringUtils.isEmpty;
14+
15+
/**
16+
* Converts {@link ZookeeperServiceInstance}s to {@link Instance}s
17+
*
18+
* @author Trivia
19+
*/
20+
public class ZookeeperServiceInstanceConverter extends DefaultServiceInstanceConverter {
21+
private static final String KEY_HEALTH_PATH = "health.path";
22+
23+
/**
24+
* Default context-path to be appended to the url of the discovered service for the
25+
* managment-url.
26+
*/
27+
private String managementContextPath = "/actuator";
28+
/**
29+
* Default path of the health-endpoint to be used for the health-url of the discovered service.
30+
*/
31+
private String healthEndpointPath = "health";
32+
@Override
33+
protected URI getHealthUrl(ServiceInstance instance) {
34+
Assert.isInstanceOf(ZookeeperServiceInstance.class, instance,
35+
"serviceInstance must be of type ZookeeperServiceInstance");
36+
String healthPath = instance.getMetadata().get(KEY_HEALTH_PATH);
37+
if (isEmpty(healthPath)) {
38+
healthPath = healthEndpointPath;
39+
}
40+
41+
return UriComponentsBuilder.fromUri(getManagementUrl(instance)).path("/").path(healthPath).build().toUri();
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//package de.codecentric.boot.admin.server.cloud.extension;
2+
//
3+
//import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
4+
//import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
5+
//import org.springframework.cloud.client.serviceregistry.Registration;
6+
//import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
7+
//import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
8+
//
9+
//public class MossAutoServiceRegistration extends AbstractAutoServiceRegistration {
10+
//
11+
// private final AutoServiceRegistrationProperties properties;
12+
// private Registration registration;
13+
//
14+
// public MossAutoServiceRegistration(ServiceRegistry serviceRegistry,
15+
// AutoServiceRegistrationProperties properties,
16+
// Registration registration) {
17+
// super(serviceRegistry, properties);
18+
// this.registration = registration;
19+
// this.properties = properties;
20+
// }
21+
//
22+
//
23+
// @Override
24+
// protected AutoServiceRegistrationProperties getConfiguration() {
25+
// return properties;
26+
// }
27+
//
28+
// @Override
29+
// protected boolean isEnabled() {
30+
// return this.properties.isEnabled();
31+
// }
32+
//
33+
// public Registration getRegistration() {
34+
// return registration;
35+
// }
36+
//
37+
// @Override
38+
// protected Registration getManagementRegistration() {
39+
// return null;
40+
// }
41+
//
42+
// public void setRegistration(ZookeeperRegistration registration) {
43+
// this.registration = registration;
44+
// }
45+
//}

0 commit comments

Comments
 (0)