mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
merge from 2.2.4.RELEASE
This commit is contained in:
@@ -183,17 +183,17 @@ public class NacosDiscoveryProperties {
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* Heart beat interval. Time unit: second.
|
||||
* Heart beat interval. Time unit: millisecond.
|
||||
*/
|
||||
private Integer heartBeatInterval;
|
||||
|
||||
/**
|
||||
* Heart beat timeout. Time unit: second.
|
||||
* Heart beat timeout. Time unit: millisecond.
|
||||
*/
|
||||
private Integer heartBeatTimeout;
|
||||
|
||||
/**
|
||||
* Ip delete timeout. Time unit: second.
|
||||
* Ip delete timeout. Time unit: millisecond.
|
||||
*/
|
||||
private Integer ipDeleteTimeout;
|
||||
|
||||
|
@@ -31,7 +31,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
@@ -58,7 +58,7 @@ public class NacosDiscoveryClientConfiguration {
|
||||
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled", matchIfMissing = true)
|
||||
public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager,
|
||||
NacosDiscoveryProperties nacosDiscoveryProperties,
|
||||
ObjectProvider<TaskScheduler> taskExecutorObjectProvider) {
|
||||
ObjectProvider<ThreadPoolTaskScheduler> taskExecutorObjectProvider) {
|
||||
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties,
|
||||
taskExecutorObjectProvider);
|
||||
}
|
||||
|
@@ -27,7 +27,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.cloud.nacos.NacosServiceManager;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.listener.Event;
|
||||
import com.alibaba.nacos.api.naming.listener.EventListener;
|
||||
@@ -41,7 +40,6 @@ import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
/**
|
||||
@@ -66,14 +64,15 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
|
||||
private final NacosDiscoveryProperties properties;
|
||||
|
||||
private final TaskScheduler taskScheduler;
|
||||
private final ThreadPoolTaskScheduler taskScheduler;
|
||||
|
||||
public NacosWatch(NacosServiceManager nacosServiceManager,
|
||||
NacosDiscoveryProperties properties,
|
||||
ObjectProvider<TaskScheduler> taskScheduler) {
|
||||
ObjectProvider<ThreadPoolTaskScheduler> taskScheduler) {
|
||||
this.nacosServiceManager = nacosServiceManager;
|
||||
this.properties = properties;
|
||||
this.taskScheduler = taskScheduler.getIfAvailable(NacosWatch::getTaskScheduler);
|
||||
this.taskScheduler = taskScheduler.stream().findAny()
|
||||
.orElseGet(NacosWatch::getTaskScheduler);
|
||||
}
|
||||
|
||||
private static ThreadPoolTaskScheduler getTaskScheduler() {
|
||||
@@ -156,7 +155,7 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
if (this.watchFuture != null) {
|
||||
// shutdown current user-thread,
|
||||
// then the other daemon-threads will terminate automatic.
|
||||
((ThreadPoolTaskScheduler) this.taskScheduler).shutdown();
|
||||
this.taskScheduler.shutdown();
|
||||
this.watchFuture.cancel(true);
|
||||
}
|
||||
|
||||
@@ -167,7 +166,7 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
namingService.unsubscribe(properties.getService(), properties.getGroup(),
|
||||
Arrays.asList(properties.getClusterName()), eventListener);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
catch (Exception e) {
|
||||
log.error("namingService unsubscribe failed, properties:{}", properties,
|
||||
e);
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
* The {@link HealthIndicator} for Nacos Discovery.
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
* @since 2.2.0
|
||||
* @see HealthIndicator
|
||||
*/
|
||||
public class NacosDiscoveryHealthIndicator extends AbstractHealthIndicator {
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.alibaba.cloud.nacos.discovery.configclient;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
|
||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
|
||||
|
||||
@@ -34,7 +35,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
@ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false)
|
||||
@Configuration
|
||||
@ImportAutoConfiguration({ NacosDiscoveryAutoConfiguration.class,
|
||||
NacosDiscoveryClientConfiguration.class })
|
||||
NacosServiceAutoConfiguration.class, NacosDiscoveryClientConfiguration.class })
|
||||
public class NacosDiscoveryClientConfigServiceBootstrapConfiguration {
|
||||
|
||||
}
|
||||
|
@@ -36,7 +36,7 @@ import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
*
|
||||
* @author xiaojing
|
||||
*/
|
||||
@Endpoint(id = "nacos-discovery")
|
||||
@Endpoint(id = "nacosdiscovery")
|
||||
public class NacosDiscoveryEndpoint {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
|
@@ -41,6 +41,10 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
|
||||
*/
|
||||
public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
private static final String STATUS_UP = "UP";
|
||||
|
||||
private static final String STATUS_DOWN = "DOWN";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NacosServiceRegistry.class);
|
||||
|
||||
private final NacosDiscoveryProperties nacosDiscoveryProperties;
|
||||
@@ -119,7 +123,8 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
@Override
|
||||
public void setStatus(Registration registration, String status) {
|
||||
|
||||
if (!status.equalsIgnoreCase("UP") && !status.equalsIgnoreCase("DOWN")) {
|
||||
if (!STATUS_UP.equalsIgnoreCase(status)
|
||||
&& !STATUS_DOWN.equalsIgnoreCase(status)) {
|
||||
log.warn("can't support status {},please choose UP or DOWN", status);
|
||||
return;
|
||||
}
|
||||
@@ -128,7 +133,7 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
Instance instance = getNacosInstanceFromRegistration(registration);
|
||||
|
||||
if (status.equalsIgnoreCase("DOWN")) {
|
||||
if (STATUS_DOWN.equalsIgnoreCase(status)) {
|
||||
instance.setEnabled(false);
|
||||
}
|
||||
else {
|
||||
@@ -137,8 +142,8 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
try {
|
||||
Properties nacosProperties = nacosDiscoveryProperties.getNacosProperties();
|
||||
nacosServiceManager.getNamingMaintainService(nacosProperties)
|
||||
.updateInstance(serviceId, instance);
|
||||
nacosServiceManager.getNamingMaintainService(nacosProperties).updateInstance(
|
||||
serviceId, nacosDiscoveryProperties.getGroup(), instance);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException("update nacos instance status fail", e);
|
||||
|
@@ -28,7 +28,7 @@ import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
|
||||
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@@ -1,72 +1,72 @@
|
||||
{"properties": [
|
||||
{
|
||||
"name": "spring.cloud.nacos.server-addr",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "localhost:8848",
|
||||
"description": "nacos server address."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.server-addr",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.server-addr}",
|
||||
"description": "nacos discovery server address."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.service",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.application.name}",
|
||||
"description": "the service name to register, default value is ${spring.application.name}."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "enable nacos discovery or not."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.instance-enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "If instance is enabled to accept request. The default value is true."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.ephemeral",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "If instance is ephemeral.The default value is true."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.namingLoadCacheAtStart",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": "false",
|
||||
"description": "naming load from local cache at application start ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.watch.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": "true",
|
||||
"description": "enable nacos discovery watch or not ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.username",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.username}",
|
||||
"description": "nacos discovery service's username to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.password",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.password}",
|
||||
"description": "nacos discovery service's password to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.username",
|
||||
"type": "java.lang.String",
|
||||
"description": "nacos userName to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.password",
|
||||
"type": "java.lang.String",
|
||||
"description": "nacos password to authenticate."
|
||||
}
|
||||
{
|
||||
"name": "spring.cloud.nacos.server-addr",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "localhost:8848",
|
||||
"description": "nacos server address."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.server-addr",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.server-addr}",
|
||||
"description": "nacos discovery server address."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.service",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.application.name}",
|
||||
"description": "the service name to register, default value is ${spring.application.name}."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "enable nacos discovery or not."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.instance-enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "If instance is enabled to accept request. The default value is true."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.ephemeral",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "If instance is ephemeral.The default value is true."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.namingLoadCacheAtStart",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": "false",
|
||||
"description": "naming load from local cache at application start ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.watch.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": "true",
|
||||
"description": "enable nacos discovery watch or not ."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.username",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.username}",
|
||||
"description": "nacos discovery service's username to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.discovery.password",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.cloud.nacos.password}",
|
||||
"description": "nacos discovery service's password to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.username",
|
||||
"type": "java.lang.String",
|
||||
"description": "nacos userName to authenticate."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.password",
|
||||
"type": "java.lang.String",
|
||||
"description": "nacos password to authenticate."
|
||||
}
|
||||
]}
|
||||
|
@@ -4,7 +4,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
|
||||
com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration,\
|
||||
com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration,\
|
||||
com.alibaba.cloud.nacos.NacosServiceAutoConfiguration,\
|
||||
com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration
|
||||
com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\
|
||||
com.alibaba.cloud.nacos.NacosServiceAutoConfiguration
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
|
||||
|
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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.nacos.registry;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author L.cm
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore("javax.management.*")
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ NacosFactory.class })
|
||||
@SpringBootTest(classes = NacosRegistrationCustomizerTest.TestConfig.class, properties = {
|
||||
"spring.application.name=myTestService1",
|
||||
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" }, webEnvironment = RANDOM_PORT)
|
||||
public class NacosRegistrationCustomizerTest {
|
||||
|
||||
@Autowired
|
||||
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
|
||||
|
||||
static {
|
||||
try {
|
||||
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
|
||||
Properties.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
return new MockNamingService();
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
NacosRegistration registration = nacosAutoServiceRegistration.getRegistration();
|
||||
Map<String, String> metadata = registration.getMetadata();
|
||||
Assert.assertEquals("test1", metadata.get("test1"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class,
|
||||
NacosDiscoveryClientConfiguration.class,
|
||||
NacosServiceRegistryAutoConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
@Bean
|
||||
public NacosRegistrationCustomizer nacosRegistrationCustomizer() {
|
||||
return registration -> {
|
||||
Map<String, String> metadata = registration.getMetadata();
|
||||
metadata.put("test1", "test1");
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user