mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish alibaba/spring-cloud-alibaba/#1283 : Renaming spring-cloud-starter-alibaba to be spring-cloud-alibaba-starters
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-starters</artifactId>
|
||||
<version>2.2.1.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-cloud-starter-alibaba-sidecar</artifactId>
|
||||
<name>Spring Cloud Starter Alibaba Sidecar</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(SidecarProperties.class)
|
||||
public class SidecarAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SidecarHealthIndicator sidecarHealthIndicator(
|
||||
SidecarProperties sidecarProperties, RestTemplate restTemplate) {
|
||||
return new SidecarHealthIndicator(sidecarProperties, restTemplate);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SidecarHealthChecker sidecarHealthChecker(
|
||||
SidecarDiscoveryClient sidecarDiscoveryClient,
|
||||
SidecarHealthIndicator sidecarHealthIndicator,
|
||||
SidecarProperties sidecarProperties, ConfigurableEnvironment environment) {
|
||||
SidecarHealthChecker cleaner = new SidecarHealthChecker(sidecarDiscoveryClient,
|
||||
sidecarHealthIndicator, sidecarProperties, environment);
|
||||
cleaner.check();
|
||||
return cleaner;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
public interface SidecarDiscoveryClient {
|
||||
|
||||
/**
|
||||
* register instance.
|
||||
* @param applicationName applicationName
|
||||
* @param ip ip
|
||||
* @param port port
|
||||
*/
|
||||
void registerInstance(String applicationName, String ip, Integer port);
|
||||
|
||||
/**
|
||||
* deregister instance.
|
||||
* @param applicationName applicationName
|
||||
* @param ip ip
|
||||
* @param port port
|
||||
*/
|
||||
void deregisterInstance(String applicationName, String ip, Integer port);
|
||||
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
public class SidecarHealthChecker {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SidecarHealthChecker.class);
|
||||
|
||||
private final SidecarDiscoveryClient sidecarDiscoveryClient;
|
||||
|
||||
private final HealthIndicator healthIndicator;
|
||||
|
||||
private final SidecarProperties sidecarProperties;
|
||||
|
||||
private final ConfigurableEnvironment environment;
|
||||
|
||||
public SidecarHealthChecker(SidecarDiscoveryClient sidecarDiscoveryClient,
|
||||
HealthIndicator healthIndicator, SidecarProperties sidecarProperties,
|
||||
ConfigurableEnvironment environment) {
|
||||
this.sidecarDiscoveryClient = sidecarDiscoveryClient;
|
||||
this.healthIndicator = healthIndicator;
|
||||
this.sidecarProperties = sidecarProperties;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public void check() {
|
||||
Schedulers.single().schedulePeriodically(() -> {
|
||||
String ip = sidecarProperties.getIp();
|
||||
Integer port = sidecarProperties.getPort();
|
||||
|
||||
Status status = healthIndicator.health().getStatus();
|
||||
String applicationName = environment.getProperty("spring.application.name");
|
||||
|
||||
if (status.equals(Status.UP)) {
|
||||
this.sidecarDiscoveryClient.registerInstance(applicationName, ip, port);
|
||||
log.debug(
|
||||
"Health check success. register this instance. applicationName = {}, ip = {}, port = {}, status = {}",
|
||||
applicationName, ip, port, status);
|
||||
}
|
||||
else {
|
||||
log.warn(
|
||||
"Health check failed. unregister this instance. applicationName = {}, ip = {}, port = {}, status = {}",
|
||||
applicationName, ip, port, status);
|
||||
this.sidecarDiscoveryClient.deregisterInstance(applicationName, ip, port);
|
||||
}
|
||||
|
||||
}, 0, sidecarProperties.getHealthCheckInterval(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
public class SidecarHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private final SidecarProperties sidecarProperties;
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public SidecarHealthIndicator(SidecarProperties sidecarProperties,
|
||||
RestTemplate restTemplate) {
|
||||
this.sidecarProperties = sidecarProperties;
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
try {
|
||||
URI uri = this.sidecarProperties.getHealthCheckUrl();
|
||||
if (uri == null) {
|
||||
builder.up();
|
||||
return;
|
||||
}
|
||||
|
||||
ResponseEntity<Map<String, Object>> exchange = this.restTemplate.exchange(uri,
|
||||
HttpMethod.GET, null,
|
||||
new ParameterizedTypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
Map<String, Object> map = exchange.getBody();
|
||||
|
||||
if (map == null) {
|
||||
this.getWarning(builder);
|
||||
return;
|
||||
}
|
||||
Object status = map.get("status");
|
||||
if (status instanceof String) {
|
||||
builder.status(status.toString());
|
||||
}
|
||||
else {
|
||||
this.getWarning(builder);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
builder.down().withDetail("error", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void getWarning(Health.Builder builder) {
|
||||
builder.unknown().withDetail("warning", "no status field in response");
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
@ConfigurationProperties("sidecar")
|
||||
@Validated
|
||||
public class SidecarProperties {
|
||||
|
||||
/**
|
||||
* polyglot service's ip.
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* polyglot service's port.
|
||||
*/
|
||||
@NotNull
|
||||
@Max(65535)
|
||||
@Min(1)
|
||||
private Integer port;
|
||||
|
||||
/**
|
||||
* polyglot service's health check url. this endpoint must return json and the format
|
||||
* must follow spring boot actuator's health endpoint. eg. {"status": "UP"}.
|
||||
*/
|
||||
private URI healthCheckUrl;
|
||||
|
||||
/**
|
||||
* interval of health check.
|
||||
*/
|
||||
private long healthCheckInterval = 30000L;
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public URI getHealthCheckUrl() {
|
||||
return healthCheckUrl;
|
||||
}
|
||||
|
||||
public void setHealthCheckUrl(URI healthCheckUrl) {
|
||||
this.healthCheckUrl = healthCheckUrl;
|
||||
}
|
||||
|
||||
public long getHealthCheckInterval() {
|
||||
return healthCheckInterval;
|
||||
}
|
||||
|
||||
public void setHealthCheckInterval(long healthCheckInterval) {
|
||||
this.healthCheckInterval = healthCheckInterval;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar.consul;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.cloud.sidecar.SidecarAutoConfiguration;
|
||||
import com.alibaba.cloud.sidecar.SidecarDiscoveryClient;
|
||||
import com.alibaba.cloud.sidecar.SidecarProperties;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
|
||||
import org.springframework.cloud.consul.discovery.HeartbeatProperties;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationAutoConfiguration;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulManagementRegistrationCustomizer;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulRegistrationCustomizer;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistryAutoConfiguration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(ConsulServiceRegistryAutoConfiguration.class)
|
||||
@AutoConfigureBefore({ ConsulAutoServiceRegistrationAutoConfiguration.class,
|
||||
SidecarAutoConfiguration.class })
|
||||
public class SidecarConsulAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public ConsulAutoRegistration consulRegistration(
|
||||
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
||||
ConsulDiscoveryProperties properties, ApplicationContext applicationContext,
|
||||
ObjectProvider<List<ConsulRegistrationCustomizer>> registrationCustomizers,
|
||||
ObjectProvider<List<ConsulManagementRegistrationCustomizer>> managementRegistrationCustomizers,
|
||||
HeartbeatProperties heartbeatProperties,
|
||||
SidecarProperties sidecarProperties) {
|
||||
return SidecarConsulAutoRegistration.registration(
|
||||
autoServiceRegistrationProperties, properties, applicationContext,
|
||||
registrationCustomizers.getIfAvailable(),
|
||||
managementRegistrationCustomizers.getIfAvailable(), heartbeatProperties,
|
||||
sidecarProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SidecarDiscoveryClient sidecarDiscoveryClient(
|
||||
ConsulDiscoveryProperties properties, ConsulServiceRegistry serviceRegistry,
|
||||
ConsulAutoRegistration registration) {
|
||||
return new SidecarConsulDiscoveryClient(properties, serviceRegistry,
|
||||
registration);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar.consul;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.cloud.sidecar.SidecarProperties;
|
||||
import com.ecwid.consul.v1.agent.model.NewService;
|
||||
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
|
||||
import org.springframework.cloud.consul.discovery.HeartbeatProperties;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulManagementRegistrationCustomizer;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulRegistrationCustomizer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
public class SidecarConsulAutoRegistration extends ConsulAutoRegistration {
|
||||
|
||||
public SidecarConsulAutoRegistration(NewService service,
|
||||
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
||||
ConsulDiscoveryProperties properties, ApplicationContext context,
|
||||
HeartbeatProperties heartbeatProperties,
|
||||
List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers) {
|
||||
super(service, autoServiceRegistrationProperties, properties, context,
|
||||
heartbeatProperties, managementRegistrationCustomizers);
|
||||
}
|
||||
|
||||
public static ConsulAutoRegistration registration(
|
||||
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
|
||||
ConsulDiscoveryProperties properties, ApplicationContext context,
|
||||
List<ConsulRegistrationCustomizer> registrationCustomizers,
|
||||
List<ConsulManagementRegistrationCustomizer> managementRegistrationCustomizers,
|
||||
HeartbeatProperties heartbeatProperties,
|
||||
SidecarProperties sidecarProperties) {
|
||||
|
||||
NewService service = new NewService();
|
||||
String appName = getAppName(properties, context.getEnvironment());
|
||||
service.setId(getInstanceId(sidecarProperties, context.getEnvironment()));
|
||||
if (!properties.isPreferAgentAddress()) {
|
||||
service.setAddress(sidecarProperties.getIp());
|
||||
}
|
||||
service.setName(normalizeForDns(appName));
|
||||
service.setTags(createTags(properties));
|
||||
|
||||
// set health check, use alibaba sidecar self's port rather than polyglot app's
|
||||
// port.
|
||||
service.setPort(
|
||||
Integer.valueOf(context.getEnvironment().getProperty("server.port")));
|
||||
setCheck(service, autoServiceRegistrationProperties, properties, context,
|
||||
heartbeatProperties);
|
||||
|
||||
service.setPort(sidecarProperties.getPort());
|
||||
|
||||
ConsulAutoRegistration registration = new ConsulAutoRegistration(service,
|
||||
autoServiceRegistrationProperties, properties, context,
|
||||
heartbeatProperties, managementRegistrationCustomizers);
|
||||
customize(registrationCustomizers, registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
public static String getInstanceId(SidecarProperties sidecarProperties,
|
||||
Environment environment) {
|
||||
return String.format("%s-%s-%s",
|
||||
environment.getProperty("spring.application.name"),
|
||||
sidecarProperties.getIp(), sidecarProperties.getPort());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar.consul;
|
||||
|
||||
import com.alibaba.cloud.sidecar.SidecarDiscoveryClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
public class SidecarConsulDiscoveryClient implements SidecarDiscoveryClient {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SidecarConsulDiscoveryClient.class);
|
||||
|
||||
private final ConsulDiscoveryProperties properties;
|
||||
|
||||
private final ConsulServiceRegistry serviceRegistry;
|
||||
|
||||
private final ConsulAutoRegistration registration;
|
||||
|
||||
public SidecarConsulDiscoveryClient(ConsulDiscoveryProperties properties,
|
||||
ConsulServiceRegistry serviceRegistry, ConsulAutoRegistration registration) {
|
||||
this.properties = properties;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.registration = registration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerInstance(String applicationName, String ip, Integer port) {
|
||||
|
||||
if (!this.properties.isRegister()) {
|
||||
log.debug("Registration disabled.");
|
||||
return;
|
||||
}
|
||||
|
||||
serviceRegistry.register(registration);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregisterInstance(String applicationName, String ip, Integer port) {
|
||||
if (!this.properties.isRegister() || !this.properties.isDeregister()) {
|
||||
return;
|
||||
}
|
||||
serviceRegistry.deregister(registration);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
|
||||
import com.alibaba.cloud.sidecar.SidecarAutoConfiguration;
|
||||
import com.alibaba.cloud.sidecar.SidecarDiscoveryClient;
|
||||
import com.alibaba.cloud.sidecar.SidecarProperties;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureBefore({ NacosDiscoveryAutoConfiguration.class,
|
||||
SidecarAutoConfiguration.class })
|
||||
@ConditionalOnClass(NacosDiscoveryProperties.class)
|
||||
public class SidecarNacosAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties(
|
||||
SidecarProperties sidecarProperties) {
|
||||
return new SidecarNacosDiscoveryProperties(sidecarProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SidecarDiscoveryClient sidecarDiscoveryClient(
|
||||
SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties) {
|
||||
return new SidecarNacosDiscoveryClient(sidecarNacosDiscoveryProperties);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar.nacos;
|
||||
|
||||
import com.alibaba.cloud.sidecar.SidecarDiscoveryClient;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
public class SidecarNacosDiscoveryClient implements SidecarDiscoveryClient {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SidecarNacosDiscoveryClient.class);
|
||||
|
||||
private final SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties;
|
||||
|
||||
public SidecarNacosDiscoveryClient(
|
||||
SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties) {
|
||||
this.sidecarNacosDiscoveryProperties = sidecarNacosDiscoveryProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerInstance(String applicationName, String ip, Integer port) {
|
||||
try {
|
||||
this.sidecarNacosDiscoveryProperties.namingServiceInstance()
|
||||
.registerInstance(applicationName, ip, port);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.warn("nacos exception happens", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregisterInstance(String applicationName, String ip, Integer port) {
|
||||
try {
|
||||
this.sidecarNacosDiscoveryProperties.namingServiceInstance()
|
||||
.deregisterInstance(applicationName, ip, port);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.warn("nacos exception happens", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.sidecar.nacos;
|
||||
|
||||
import java.net.SocketException;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.cloud.sidecar.SidecarProperties;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* @author itmuch.com
|
||||
*/
|
||||
public class SidecarNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
|
||||
private final SidecarProperties sidecarProperties;
|
||||
|
||||
public SidecarNacosDiscoveryProperties(SidecarProperties sidecarProperties) {
|
||||
this.sidecarProperties = sidecarProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() throws SocketException {
|
||||
super.init();
|
||||
|
||||
String ip = sidecarProperties.getIp();
|
||||
if (StringUtils.isNotBlank(ip)) {
|
||||
this.setIp(ip);
|
||||
}
|
||||
|
||||
Integer port = sidecarProperties.getPort();
|
||||
this.setPort(port);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.alibaba.cloud.sidecar.nacos.SidecarNacosAutoConfiguration,\
|
||||
com.alibaba.cloud.sidecar.SidecarAutoConfiguration,\
|
||||
com.alibaba.cloud.sidecar.consul.SidecarConsulAutoConfiguration
|
Reference in New Issue
Block a user