mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge branch 'master' of https://github.com/yuhuangbin/spring-cloud-alibaba
This commit is contained in:
commit
9737f0dfbb
@ -19,7 +19,7 @@
|
||||
|
||||
<properties>
|
||||
<revision>2.2.1.RELEASE</revision>
|
||||
<sentinel.version>1.7.1</sentinel.version>
|
||||
<sentinel.version>1.7.2</sentinel.version>
|
||||
<oss.version>3.1.0</oss.version>
|
||||
<seata.version>1.2.0</seata.version>
|
||||
<nacos.client.version>1.2.1</nacos.client.version>
|
||||
|
@ -23,6 +23,11 @@
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@ -36,10 +41,10 @@
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||
<!--<artifactId>sentinel-datasource-nacos</artifactId>-->
|
||||
<!--</dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||
<!--<artifactId>sentinel-datasource-zookeeper</artifactId>-->
|
||||
|
@ -9,11 +9,19 @@ management.health.diskspace.enabled=false
|
||||
|
||||
spring.cloud.sentinel.transport.dashboard=localhost:8080
|
||||
spring.cloud.sentinel.eager=true
|
||||
spring.cloud.sentinel.web-context-unify=true
|
||||
|
||||
#spring.cloud.sentinel.block-page=/errorPage
|
||||
#spring.cloud.sentinel.filter.enabled=false
|
||||
#spring.cloud.sentinel.http-method-specify=false
|
||||
|
||||
#spring.cloud.sentinel.datasource.ds6.nacos.server-addr=127.0.0.1:8848
|
||||
#spring.cloud.sentinel.datasource.ds6.nacos.username=nacos
|
||||
#spring.cloud.sentinel.datasource.ds6.nacos.password=nacos
|
||||
#spring.cloud.sentinel.datasource.ds6.nacos.dataId=flowrule.json
|
||||
#spring.cloud.sentinel.datasource.ds6.nacos.data-type=json
|
||||
#spring.cloud.sentinel.datasource.ds6.nacos.rule-type=flow
|
||||
|
||||
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
|
||||
spring.cloud.sentinel.datasource.ds1.file.data-type=json
|
||||
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
|
||||
|
@ -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:
|
||||
|
@ -32,6 +32,10 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
|
||||
private String serverAddr;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
@NotEmpty
|
||||
private String groupId = "DEFAULT_GROUP";
|
||||
|
||||
@ -67,6 +71,22 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties {
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
@ -35,6 +35,10 @@ public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource>
|
||||
|
||||
private String serverAddr;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String groupId;
|
||||
|
||||
private String dataId;
|
||||
@ -63,6 +67,8 @@ public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource>
|
||||
if (!StringUtils.isEmpty(this.namespace)) {
|
||||
properties.setProperty(PropertyKeyConst.NAMESPACE, this.namespace);
|
||||
}
|
||||
properties.setProperty(PropertyKeyConst.USERNAME, this.username);
|
||||
properties.setProperty(PropertyKeyConst.PASSWORD, this.password);
|
||||
return new NacosDataSource(properties, groupId, dataId, converter);
|
||||
}
|
||||
|
||||
@ -79,6 +85,22 @@ public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource>
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
@ -55,7 +55,9 @@ public class NacosDataPropertiesParser extends AbstractNacosDataParser {
|
||||
log.warn("the config data is invalid {}", dataLine);
|
||||
continue;
|
||||
}
|
||||
result.put(dataLine.substring(0, index), dataLine.substring(index + 1));
|
||||
String key = dataLine.substring(0, index);
|
||||
String value = dataLine.substring(index + 1);
|
||||
result.put(key.trim(), value.trim());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@ -64,8 +64,8 @@ import static com.alibaba.nacos.api.PropertyKeyConst.USERNAME;
|
||||
* @author xiaojing
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
* @author <a href="mailto:lyuzb@lyuzb.com">lyuzb</a>
|
||||
* @author <a href="mailto:78552423@qq.com">eshun</a>
|
||||
*/
|
||||
|
||||
@ConfigurationProperties("spring.cloud.nacos.discovery")
|
||||
public class NacosDiscoveryProperties {
|
||||
|
||||
@ -211,6 +211,7 @@ public class NacosDiscoveryProperties {
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws SocketException {
|
||||
namingService = null;
|
||||
|
||||
metadata.put(PreservedMetadataKeys.REGISTER_SOURCE, "SPRING_CLOUD");
|
||||
if (secure) {
|
||||
|
@ -33,6 +33,7 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
* @author <a href="mailto:78552423@qq.com">eshun</a>
|
||||
*/
|
||||
public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
@ -40,11 +41,8 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
private final NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||
|
||||
private final NamingService namingService;
|
||||
|
||||
public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
||||
this.namingService = nacosDiscoveryProperties.namingServiceInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -55,6 +53,7 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
return;
|
||||
}
|
||||
|
||||
NamingService namingService = namingService();
|
||||
String serviceId = registration.getServiceId();
|
||||
String group = nacosDiscoveryProperties.getGroup();
|
||||
|
||||
@ -84,7 +83,7 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
return;
|
||||
}
|
||||
|
||||
NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
|
||||
NamingService namingService = namingService();
|
||||
String serviceId = registration.getServiceId();
|
||||
String group = nacosDiscoveryProperties.getGroup();
|
||||
|
||||
@ -165,4 +164,8 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private NamingService namingService() {
|
||||
return nacosDiscoveryProperties.namingServiceInstance();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -102,6 +102,20 @@ public class SentinelProperties {
|
||||
*/
|
||||
private Boolean httpMethodSpecify = false;
|
||||
|
||||
/**
|
||||
* Specify whether unify web context(i.e. use the default context name), and is true
|
||||
* by default.
|
||||
*/
|
||||
private Boolean webContextUnify = true;
|
||||
|
||||
public Boolean getWebContextUnify() {
|
||||
return webContextUnify;
|
||||
}
|
||||
|
||||
public void setWebContextUnify(Boolean webContextUnify) {
|
||||
this.webContextUnify = webContextUnify;
|
||||
}
|
||||
|
||||
public boolean isEager() {
|
||||
return eager;
|
||||
}
|
||||
|
@ -96,6 +96,7 @@ public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
|
||||
public SentinelWebMvcConfig sentinelWebMvcConfig() {
|
||||
SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
|
||||
sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify());
|
||||
sentinelWebMvcConfig.setWebContextUnify(properties.getWebContextUnify());
|
||||
|
||||
if (blockExceptionHandlerOptional.isPresent()) {
|
||||
blockExceptionHandlerOptional
|
||||
|
@ -61,7 +61,7 @@ public class SentinelEndpoint {
|
||||
result.put("metricsFileSize", SentinelConfig.singleMetricFileSize());
|
||||
result.put("metricsFileCharset", SentinelConfig.charset());
|
||||
result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount());
|
||||
result.put("consoleServer", TransportConfig.getConsoleServer());
|
||||
result.put("consoleServer", TransportConfig.getConsoleServerList());
|
||||
result.put("clientIp", TransportConfig.getHeartbeatClientIp());
|
||||
result.put("heartbeatIntervalMs", TransportConfig.getHeartbeatIntervalMs());
|
||||
result.put("clientPort", TransportConfig.getPort());
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.alibaba.cloud.sentinel.endpoint;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.sentinel.SentinelProperties;
|
||||
@ -24,13 +25,14 @@ import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
|
||||
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
|
||||
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* A {@link HealthIndicator} for Sentinel, which checks the status of Sentinel Dashboard
|
||||
@ -82,8 +84,9 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
// Check health of Dashboard
|
||||
boolean dashboardUp = true;
|
||||
String consoleServer = TransportConfig.getConsoleServer();
|
||||
if (StringUtils.isEmpty(consoleServer)) {
|
||||
List<Tuple2<String, Integer>> consoleServerList = TransportConfig
|
||||
.getConsoleServerList();
|
||||
if (CollectionUtils.isEmpty(consoleServerList)) {
|
||||
// If Dashboard isn't configured, it's OK and mark the status of Dashboard
|
||||
// with UNKNOWN.
|
||||
detailMap.put("dashboard",
|
||||
@ -101,8 +104,10 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
|
||||
else {
|
||||
// If failed to send heartbeat message, means that the Dashboard is DOWN
|
||||
dashboardUp = false;
|
||||
detailMap.put("dashboard", new Status(Status.DOWN.getCode(),
|
||||
consoleServer + " can't be connected"));
|
||||
detailMap.put("dashboard",
|
||||
new Status(Status.DOWN.getCode(), String.format(
|
||||
"the dashboard servers [%s] one of them can't be connected",
|
||||
consoleServerList)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,12 @@
|
||||
"defaultValue": false,
|
||||
"description": "earlier initialize heart-beat when the spring container starts when the transport dependency is on classpath, the configuration is effective."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.sentinel.web-context-unify",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "Specify whether unify web context(i.e. use the default context name), and is true by default."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.sentinel.transport.port",
|
||||
"type": "java.lang.String",
|
||||
|
@ -33,6 +33,7 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import com.alibaba.csp.sentinel.util.function.Tuple2;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -73,7 +74,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
"spring.cloud.sentinel.flow.coldFactor=3",
|
||||
"spring.cloud.sentinel.eager=true",
|
||||
"spring.cloud.sentinel.log.switchPid=true",
|
||||
"spring.cloud.sentinel.transport.dashboard=http://localhost:8080",
|
||||
"spring.cloud.sentinel.transport.dashboard=http://localhost:8080,http://localhost:8081",
|
||||
"spring.cloud.sentinel.transport.port=9999",
|
||||
"spring.cloud.sentinel.transport.clientIp=1.1.1.1",
|
||||
"spring.cloud.sentinel.transport.heartbeatIntervalMs=20000" },
|
||||
@ -142,7 +143,9 @@ public class SentinelAutoConfigurationTests {
|
||||
Map<String, Object> map = sentinelEndpoint.invoke();
|
||||
|
||||
assertThat(map.get("logUsePid")).isEqualTo(Boolean.TRUE);
|
||||
assertThat(map.get("consoleServer")).isEqualTo("http://localhost:8080");
|
||||
assertThat(map.get("consoleServer").toString()).isEqualTo(
|
||||
Arrays.asList(Tuple2.of("localhost", 8080), Tuple2.of("localhost", 8081))
|
||||
.toString());
|
||||
assertThat(map.get("clientPort")).isEqualTo("9999");
|
||||
assertThat(map.get("heartbeatIntervalMs")).isEqualTo(20000L);
|
||||
assertThat(map.get("clientIp")).isEqualTo("1.1.1.1");
|
||||
@ -174,7 +177,7 @@ public class SentinelAutoConfigurationTests {
|
||||
private void checkSentinelTransport() {
|
||||
assertThat(sentinelProperties.getTransport().getPort()).isEqualTo("9999");
|
||||
assertThat(sentinelProperties.getTransport().getDashboard())
|
||||
.isEqualTo("http://localhost:8080");
|
||||
.isEqualTo("http://localhost:8080,http://localhost:8081");
|
||||
assertThat(sentinelProperties.getTransport().getClientIp()).isEqualTo("1.1.1.1");
|
||||
assertThat(sentinelProperties.getTransport().getHeartbeatIntervalMs())
|
||||
.isEqualTo("20000");
|
||||
@ -191,7 +194,9 @@ public class SentinelAutoConfigurationTests {
|
||||
@Test
|
||||
public void testSentinelSystemProperties() {
|
||||
assertThat(LogBase.isLogNameUsePid()).isEqualTo(true);
|
||||
assertThat(TransportConfig.getConsoleServer()).isEqualTo("http://localhost:8080");
|
||||
assertThat(TransportConfig.getConsoleServerList().toString()).isEqualTo(
|
||||
Arrays.asList(Tuple2.of("localhost", 8080), Tuple2.of("localhost", 8081))
|
||||
.toString());
|
||||
assertThat(TransportConfig.getPort()).isEqualTo("9999");
|
||||
assertThat(TransportConfig.getHeartbeatIntervalMs().longValue())
|
||||
.isEqualTo(20000L);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -65,7 +65,6 @@ public class DubboProtocolConfigSupplier implements Supplier<ProtocolConfig> {
|
||||
if (protocolConfig == null) {
|
||||
protocolConfig = new ProtocolConfig();
|
||||
protocolConfig.setName(DEFAULT_PROTOCOL);
|
||||
protocolConfig.setPort(-1);
|
||||
}
|
||||
|
||||
return protocolConfig;
|
||||
|
@ -23,6 +23,7 @@ import com.alibaba.cloud.dubbo.util.JSONUtils;
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.apache.dubbo.registry.Registry;
|
||||
import org.apache.dubbo.registry.RegistryFactory;
|
||||
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
|
||||
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@ -37,7 +38,7 @@ import static java.lang.System.getProperty;
|
||||
* @see RegistryFactory
|
||||
* @see SpringCloudRegistry
|
||||
*/
|
||||
public class SpringCloudRegistryFactory implements RegistryFactory {
|
||||
public class SpringCloudRegistryFactory extends AbstractRegistryFactory {
|
||||
|
||||
/**
|
||||
* Spring Cloud Protocol.
|
||||
@ -65,8 +66,6 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
|
||||
|
||||
private DubboGenericServiceFactory dubboGenericServiceFactory;
|
||||
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
public SpringCloudRegistryFactory() {
|
||||
}
|
||||
|
||||
@ -76,9 +75,6 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
if (initialized || applicationContext == null) {
|
||||
return;
|
||||
}
|
||||
this.discoveryClient = applicationContext.getBean(DiscoveryClient.class);
|
||||
this.dubboServiceMetadataRepository = applicationContext
|
||||
.getBean(DubboServiceMetadataRepository.class);
|
||||
@ -90,7 +86,7 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Registry getRegistry(URL url) {
|
||||
public Registry createRegistry(URL url) {
|
||||
init();
|
||||
return new SpringCloudRegistry(url, discoveryClient,
|
||||
dubboServiceMetadataRepository, dubboMetadataConfigServiceProxy,
|
||||
|
Loading…
x
Reference in New Issue
Block a user