mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #1398 from yuhuangbin/sidecar-enhance
sidecar enhance
This commit is contained in:
commit
2e934f09e9
@ -20,9 +20,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@ -33,10 +34,6 @@
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -6,11 +6,13 @@ spring:
|
||||
username: nacos
|
||||
password: nacos
|
||||
discovery:
|
||||
server-addr: localhost:8848
|
||||
server-addr: 127.0.0.1:8848
|
||||
group: test
|
||||
gateway:
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true
|
||||
|
||||
application:
|
||||
name: node-service
|
||||
sidecar:
|
||||
@ -18,6 +20,7 @@ sidecar:
|
||||
ip: 127.0.0.1
|
||||
# 异构微服务的端口
|
||||
port: 8060
|
||||
|
||||
# 异构微服务的健康检查URL
|
||||
#health-check-url: http://localhost:8060/health.json
|
||||
management:
|
||||
|
@ -18,7 +18,6 @@ package com.alibaba.cloud.sidecar;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
|
||||
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;
|
||||
@ -28,7 +27,6 @@ import org.springframework.web.client.RestTemplate;
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(SidecarProperties.class)
|
||||
public class SidecarAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@ -17,12 +17,15 @@
|
||||
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.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
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;
|
||||
|
||||
@ -30,15 +33,24 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author www.itmuch.com
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureBefore(SidecarAutoConfiguration.class)
|
||||
@ConditionalOnBean(NacosDiscoveryProperties.class)
|
||||
@AutoConfigureBefore({ NacosDiscoveryAutoConfiguration.class,
|
||||
SidecarAutoConfiguration.class })
|
||||
@ConditionalOnClass(NacosDiscoveryProperties.class)
|
||||
@EnableConfigurationProperties(SidecarProperties.class)
|
||||
public class SidecarNacosAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties(
|
||||
SidecarProperties sidecarProperties) {
|
||||
return new SidecarNacosDiscoveryProperties(sidecarProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SidecarDiscoveryClient sidecarDiscoveryClient(
|
||||
NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||
return new SidecarNacosDiscoveryClient(nacosDiscoveryProperties);
|
||||
SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties) {
|
||||
return new SidecarNacosDiscoveryClient(sidecarNacosDiscoveryProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
package com.alibaba.cloud.sidecar.nacos;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.cloud.sidecar.SidecarDiscoveryClient;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.slf4j.Logger;
|
||||
@ -30,18 +29,19 @@ public class SidecarNacosDiscoveryClient implements SidecarDiscoveryClient {
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SidecarNacosDiscoveryClient.class);
|
||||
|
||||
private final NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||
private final SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties;
|
||||
|
||||
public SidecarNacosDiscoveryClient(
|
||||
NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
||||
SidecarNacosDiscoveryProperties sidecarNacosDiscoveryProperties) {
|
||||
this.sidecarNacosDiscoveryProperties = sidecarNacosDiscoveryProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerInstance(String applicationName, String ip, Integer port) {
|
||||
try {
|
||||
this.nacosDiscoveryProperties.namingServiceInstance().registerInstance(
|
||||
applicationName, nacosDiscoveryProperties.getGroup(), ip, port);
|
||||
this.sidecarNacosDiscoveryProperties.namingServiceInstance().registerInstance(
|
||||
applicationName, sidecarNacosDiscoveryProperties.getGroup(), ip,
|
||||
port);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.warn("nacos exception happens", e);
|
||||
@ -51,8 +51,9 @@ public class SidecarNacosDiscoveryClient implements SidecarDiscoveryClient {
|
||||
@Override
|
||||
public void deregisterInstance(String applicationName, String ip, Integer port) {
|
||||
try {
|
||||
this.nacosDiscoveryProperties.namingServiceInstance().deregisterInstance(
|
||||
applicationName, nacosDiscoveryProperties.getGroup(), ip, port);
|
||||
this.sidecarNacosDiscoveryProperties.namingServiceInstance()
|
||||
.deregisterInstance(applicationName,
|
||||
sidecarNacosDiscoveryProperties.getGroup(), ip, port);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
log.warn("nacos exception happens", e);
|
||||
|
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class SidecarNacosDiscoveryProperties extends NacosDiscoveryProperties {
|
||||
|
||||
SidecarProperties sidecarProperties;
|
||||
|
||||
public SidecarNacosDiscoveryProperties(SidecarProperties sidecarProperties) {
|
||||
this.sidecarProperties = sidecarProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() throws SocketException {
|
||||
super.init();
|
||||
|
||||
String ip = sidecarProperties.getIp();
|
||||
if (!StringUtils.isEmpty(ip)) {
|
||||
this.setIp(ip);
|
||||
}
|
||||
|
||||
Integer port = sidecarProperties.getPort();
|
||||
this.setPort(port);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user