Fix conflict for Nacos
This commit is contained in:
parent
44fc2a6c2e
commit
7237e8baae
|
|
@ -16,15 +16,6 @@
|
|||
*/
|
||||
package org.apache.dubbo.registry.nacos;
|
||||
|
||||
import org.apache.dubbo.common.utils.MethodUtils;
|
||||
import org.apache.dubbo.common.utils.StringUtils;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.listener.EventListener;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -36,7 +27,22 @@ import java.util.concurrent.locks.Lock;
|
|||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.dubbo.common.constants.LoggerCodeConstants;
|
||||
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
|
||||
import org.apache.dubbo.common.logger.LoggerFactory;
|
||||
import org.apache.dubbo.common.utils.MethodUtils;
|
||||
import org.apache.dubbo.common.utils.StringUtils;
|
||||
import org.apache.dubbo.registry.nacos.function.NacosConsumer;
|
||||
import org.apache.dubbo.registry.nacos.function.NacosFunction;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.listener.EventListener;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||
|
||||
public class NacosNamingServiceWrapper {
|
||||
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NacosNamingServiceWrapper.class);
|
||||
|
||||
private static final String INNERCLASS_SYMBOL = "$";
|
||||
|
||||
|
|
@ -44,24 +50,32 @@ public class NacosNamingServiceWrapper {
|
|||
|
||||
private final NacosConnectionManager nacosConnectionManager;
|
||||
|
||||
private final int retryTimes;
|
||||
|
||||
private final int sleepMsBetweenRetries;
|
||||
|
||||
private final boolean isSupportBatchRegister;
|
||||
|
||||
private final Map<InstanceId, InstancesInfo> registerStatus = new ConcurrentHashMap<>();
|
||||
private final Map<SubscribeInfo, NamingService> subscribeStatus = new ConcurrentHashMap<>();
|
||||
private final Lock mapLock = new ReentrantLock();
|
||||
|
||||
public NacosNamingServiceWrapper(NacosConnectionManager nacosConnectionManager) {
|
||||
public NacosNamingServiceWrapper(NacosConnectionManager nacosConnectionManager, int retryTimes, int sleepMsBetweenRetries) {
|
||||
this.nacosConnectionManager = nacosConnectionManager;
|
||||
this.isSupportBatchRegister = MethodUtils.findMethod(NamingService.class, "batchRegisterInstance", String.class, String.class, List.class) != null;
|
||||
this.retryTimes = Math.max(retryTimes, 0);
|
||||
this.sleepMsBetweenRetries = sleepMsBetweenRetries;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated for uts only
|
||||
*/
|
||||
@Deprecated
|
||||
protected NacosNamingServiceWrapper(NacosConnectionManager nacosConnectionManager, boolean isSupportBatchRegister) {
|
||||
protected NacosNamingServiceWrapper(NacosConnectionManager nacosConnectionManager, boolean isSupportBatchRegister, int retryTimes, int sleepMsBetweenRetries) {
|
||||
this.nacosConnectionManager = nacosConnectionManager;
|
||||
this.isSupportBatchRegister = isSupportBatchRegister;
|
||||
this.retryTimes = Math.max(retryTimes, 0);
|
||||
this.sleepMsBetweenRetries = sleepMsBetweenRetries;
|
||||
}
|
||||
|
||||
public String getServerStatus() {
|
||||
|
|
@ -72,7 +86,7 @@ public class NacosNamingServiceWrapper {
|
|||
String nacosServiceName = handleInnerSymbol(serviceName);
|
||||
SubscribeInfo subscribeInfo = new SubscribeInfo(nacosServiceName, group, eventListener);
|
||||
NamingService namingService = subscribeStatus.computeIfAbsent(subscribeInfo, info -> nacosConnectionManager.getNamingService());
|
||||
namingService.subscribe(nacosServiceName, group, eventListener);
|
||||
accept(() -> namingService.subscribe(nacosServiceName, group, eventListener));
|
||||
}
|
||||
|
||||
public void unsubscribe(String serviceName, String group, EventListener eventListener) throws NacosException {
|
||||
|
|
@ -80,13 +94,13 @@ public class NacosNamingServiceWrapper {
|
|||
SubscribeInfo subscribeInfo = new SubscribeInfo(nacosServiceName, group, eventListener);
|
||||
NamingService namingService = subscribeStatus.get(subscribeInfo);
|
||||
if (namingService != null) {
|
||||
namingService.unsubscribe(nacosServiceName, group, eventListener);
|
||||
accept(() -> namingService.unsubscribe(nacosServiceName, group, eventListener));
|
||||
subscribeStatus.remove(subscribeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Instance> getAllInstances(String serviceName, String group) throws NacosException {
|
||||
return nacosConnectionManager.getNamingService().getAllInstances(handleInnerSymbol(serviceName), group);
|
||||
return apply(() -> nacosConnectionManager.getNamingService().getAllInstances(handleInnerSymbol(serviceName), group));
|
||||
}
|
||||
|
||||
public void registerInstance(String serviceName, String group, Instance instance) throws NacosException {
|
||||
|
|
@ -108,7 +122,7 @@ public class NacosNamingServiceWrapper {
|
|||
if (instancesInfo.getInstances().isEmpty()) {
|
||||
// directly register
|
||||
NamingService namingService = nacosConnectionManager.getNamingService();
|
||||
namingService.registerInstance(nacosServiceName, group, instance);
|
||||
accept(() -> namingService.registerInstance(nacosServiceName, group, instance));
|
||||
instancesInfo.getInstances().add(new InstanceInfo(instance, namingService));
|
||||
return;
|
||||
}
|
||||
|
|
@ -122,7 +136,7 @@ public class NacosNamingServiceWrapper {
|
|||
instanceListToRegister.add(instance);
|
||||
|
||||
try {
|
||||
namingService.batchRegisterInstance(nacosServiceName, group, instanceListToRegister);
|
||||
accept(() -> namingService.batchRegisterInstance(nacosServiceName, group, instanceListToRegister));
|
||||
instancesInfo.getInstances().add(new InstanceInfo(instance, namingService));
|
||||
instancesInfo.setBatchRegistered(true);
|
||||
return;
|
||||
|
|
@ -138,7 +152,7 @@ public class NacosNamingServiceWrapper {
|
|||
instanceListToRegister.add(instanceInfo.getInstance());
|
||||
}
|
||||
instanceListToRegister.add(instance);
|
||||
namingService.batchRegisterInstance(nacosServiceName, group, instanceListToRegister);
|
||||
accept(() -> namingService.batchRegisterInstance(nacosServiceName, group, instanceListToRegister));
|
||||
instancesInfo.getInstances().add(new InstanceInfo(instance, namingService));
|
||||
return;
|
||||
}
|
||||
|
|
@ -149,7 +163,7 @@ public class NacosNamingServiceWrapper {
|
|||
.map(InstanceInfo::getNamingService)
|
||||
.collect(Collectors.toSet());
|
||||
NamingService namingService = nacosConnectionManager.getNamingService(selectedNamingServices);
|
||||
namingService.registerInstance(nacosServiceName, group, instance);
|
||||
accept(() -> namingService.registerInstance(nacosServiceName, group, instance));
|
||||
instancesInfo.getInstances().add(new InstanceInfo(instance, namingService));
|
||||
} finally {
|
||||
instancesInfo.unlock();
|
||||
|
|
@ -218,7 +232,7 @@ public class NacosNamingServiceWrapper {
|
|||
// only one registered
|
||||
if (instancesInfo.getInstances().isEmpty()) {
|
||||
// directly unregister
|
||||
instanceInfo.getNamingService().deregisterInstance(nacosServiceName, group, instance);
|
||||
accept(() -> instanceInfo.getNamingService().deregisterInstance(nacosServiceName, group, instance));
|
||||
instancesInfo.setBatchRegistered(false);
|
||||
return;
|
||||
}
|
||||
|
|
@ -229,10 +243,10 @@ public class NacosNamingServiceWrapper {
|
|||
for (InstanceInfo info : instancesInfo.getInstances()) {
|
||||
instanceListToRegister.add(info.getInstance());
|
||||
}
|
||||
instanceInfo.getNamingService().batchRegisterInstance(nacosServiceName, group, instanceListToRegister);
|
||||
accept(() -> instanceInfo.getNamingService().batchRegisterInstance(nacosServiceName, group, instanceListToRegister));
|
||||
} else {
|
||||
// unregister one
|
||||
instanceInfo.getNamingService().deregisterInstance(nacosServiceName, group, instance);
|
||||
accept(() -> instanceInfo.getNamingService().deregisterInstance(nacosServiceName, group, instance));
|
||||
}
|
||||
} finally {
|
||||
instancesInfo.unlock();
|
||||
|
|
@ -240,11 +254,11 @@ public class NacosNamingServiceWrapper {
|
|||
}
|
||||
|
||||
public ListView<String> getServicesOfServer(int pageNo, int pageSize, String group) throws NacosException {
|
||||
return nacosConnectionManager.getNamingService().getServicesOfServer(pageNo, pageSize, group);
|
||||
return apply(() -> nacosConnectionManager.getNamingService().getServicesOfServer(pageNo, pageSize, group));
|
||||
}
|
||||
|
||||
public List<Instance> selectInstances(String serviceName, String group, boolean healthy) throws NacosException {
|
||||
return nacosConnectionManager.getNamingService().selectInstances(handleInnerSymbol(serviceName), group, healthy);
|
||||
return apply(() -> nacosConnectionManager.getNamingService().selectInstances(handleInnerSymbol(serviceName), group, healthy));
|
||||
}
|
||||
|
||||
public void shutdown() throws NacosException {
|
||||
|
|
@ -386,4 +400,73 @@ public class NacosNamingServiceWrapper {
|
|||
protected Map<InstanceId, InstancesInfo> getRegisterStatus() {
|
||||
return registerStatus;
|
||||
}
|
||||
|
||||
|
||||
private <R> R apply(NacosFunction<R> command) throws NacosException {
|
||||
NacosException le = null;
|
||||
R result = null;
|
||||
int times = 0;
|
||||
for (; times < retryTimes + 1; times++) {
|
||||
try {
|
||||
result = command.apply();
|
||||
le = null;
|
||||
break;
|
||||
} catch (NacosException e) {
|
||||
le = e;
|
||||
logger.warn(LoggerCodeConstants.REGISTRY_NACOS_EXCEPTION, "", "",
|
||||
"Failed to request nacos naming server. " +
|
||||
(times < retryTimes ? "Dubbo will try to retry in " + sleepMsBetweenRetries + ". " : "Exceed retry max times.") +
|
||||
"Try times: " + times + 1, e);
|
||||
if (times < retryTimes) {
|
||||
try {
|
||||
Thread.sleep(sleepMsBetweenRetries);
|
||||
} catch (InterruptedException ex) {
|
||||
logger.warn(LoggerCodeConstants.INTERNAL_INTERRUPTED, "", "", "Interrupted when waiting to retry.", ex);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (le != null) {
|
||||
throw le;
|
||||
}
|
||||
if (times > 1) {
|
||||
logger.info("Failed to request nacos naming server for " + (times - 1) + " times and finally success. " +
|
||||
"This may caused by high stress of nacos server.");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void accept(NacosConsumer command) throws NacosException {
|
||||
NacosException le = null;
|
||||
int times = 0;
|
||||
for (; times < retryTimes + 1; times++) {
|
||||
try {
|
||||
command.accept();
|
||||
le = null;
|
||||
break;
|
||||
} catch (NacosException e) {
|
||||
le = e;
|
||||
logger.warn(LoggerCodeConstants.REGISTRY_NACOS_EXCEPTION, "", "",
|
||||
"Failed to request nacos naming server. " +
|
||||
(times < retryTimes ? "Dubbo will try to retry in " + sleepMsBetweenRetries + ". " : "Exceed retry max times.") +
|
||||
"Try times: " + times + 1, e);
|
||||
if (times < retryTimes) {
|
||||
try {
|
||||
Thread.sleep(sleepMsBetweenRetries);
|
||||
} catch (InterruptedException ex) {
|
||||
logger.warn(LoggerCodeConstants.INTERNAL_INTERRUPTED, "", "", "Interrupted when waiting to retry.", ex);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (le != null) {
|
||||
throw le;
|
||||
}
|
||||
if (times > 1) {
|
||||
logger.info("Failed to request nacos naming server for " + (times - 1) + " times and finally success. " +
|
||||
"This may caused by high stress of nacos server.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,13 +28,12 @@ import com.alibaba.nacos.api.exception.NacosException;
|
|||
* @since 3.1.5
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface NacosConsumer<T> {
|
||||
public interface NacosConsumer {
|
||||
|
||||
/**
|
||||
* Applies this function to the given argument.
|
||||
*
|
||||
* @param t the function argument
|
||||
* @throws NacosException if met with any error
|
||||
*/
|
||||
void accept(T t) throws NacosException;
|
||||
void accept() throws NacosException;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,14 +28,13 @@ import com.alibaba.nacos.api.exception.NacosException;
|
|||
* @since 3.1.5
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface NacosFunction<T, R> {
|
||||
public interface NacosFunction<R> {
|
||||
|
||||
/**
|
||||
* Applies this function to the given argument.
|
||||
*
|
||||
* @param t the function argument
|
||||
* @return the function result
|
||||
* @throws NacosException if met with any error
|
||||
*/
|
||||
R apply(T t) throws NacosException;
|
||||
R apply() throws NacosException;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,6 +42,10 @@ public class NacosNamingServiceUtils {
|
|||
private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(NacosNamingServiceUtils.class);
|
||||
private static final String NACOS_GROUP_KEY = "nacos.group";
|
||||
|
||||
private static final String NACOS_RETRY_KEY = "nacos.retry";
|
||||
|
||||
private static final String NACOS_RETRY_WAIT_KEY = "nacos.retry-wait";
|
||||
|
||||
private NacosNamingServiceUtils() {
|
||||
throw new IllegalStateException("NacosNamingServiceUtils should not be instantiated");
|
||||
}
|
||||
|
|
@ -104,6 +108,8 @@ public class NacosNamingServiceUtils {
|
|||
* @since 2.7.5
|
||||
*/
|
||||
public static NacosNamingServiceWrapper createNamingService(URL connectionURL) {
|
||||
return new NacosNamingServiceWrapper(new NacosConnectionManager(connectionURL));
|
||||
int retryTimes = connectionURL.getParameter(NACOS_RETRY_KEY, 10);
|
||||
int sleepMsBetweenRetries = connectionURL.getParameter(NACOS_RETRY_WAIT_KEY, 10);
|
||||
return new NacosNamingServiceWrapper(new NacosConnectionManager(connectionURL), retryTimes, sleepMsBetweenRetries);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,22 +16,23 @@
|
|||
*/
|
||||
package org.apache.dubbo.registry.nacos;
|
||||
|
||||
import org.apache.dubbo.common.URL;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.listener.EventListener;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.listener.EventListener;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
|
||||
class NacosNamingServiceWrapperTest {
|
||||
@Test
|
||||
|
|
@ -40,7 +41,7 @@ class NacosNamingServiceWrapperTest {
|
|||
NamingService namingService = Mockito.mock(NamingService.class);
|
||||
Mockito.when(connectionManager.getNamingService()).thenReturn(namingService);
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(connectionManager);
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(connectionManager, 0, 0);
|
||||
|
||||
EventListener eventListener = Mockito.mock(EventListener.class);
|
||||
nacosNamingServiceWrapper.subscribe("service_name", "test", eventListener);
|
||||
|
|
@ -65,7 +66,7 @@ class NacosNamingServiceWrapperTest {
|
|||
NamingService namingService1 = Mockito.mock(NamingService.class);
|
||||
NamingService namingService2 = Mockito.mock(NamingService.class);
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(connectionManager);
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(connectionManager, 0, 0);
|
||||
|
||||
EventListener eventListener = Mockito.mock(EventListener.class);
|
||||
Mockito.when(connectionManager.getNamingService()).thenReturn(namingService1);
|
||||
|
|
@ -101,7 +102,7 @@ class NacosNamingServiceWrapperTest {
|
|||
|
||||
Assertions.assertEquals(1, namingServiceList.size());
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, false);
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, false, 0, 0);
|
||||
|
||||
Instance instance1 = new Instance();
|
||||
instance1.setIp("ip1");
|
||||
|
|
@ -155,7 +156,7 @@ class NacosNamingServiceWrapperTest {
|
|||
|
||||
Assertions.assertEquals(1, namingServiceList.size());
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, true);
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, true, 0, 0);
|
||||
|
||||
Instance instance1 = new Instance();
|
||||
instance1.setIp("ip1");
|
||||
|
|
@ -214,7 +215,7 @@ class NacosNamingServiceWrapperTest {
|
|||
|
||||
Assertions.assertEquals(1, namingServiceList.size());
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, true);
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, true, 0, 0);
|
||||
|
||||
Instance instance1 = new Instance();
|
||||
instance1.setIp("ip1");
|
||||
|
|
@ -317,7 +318,7 @@ class NacosNamingServiceWrapperTest {
|
|||
|
||||
Assertions.assertEquals(1, namingServiceList.size());
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, true);
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(nacosConnectionManager, true, 0, 0);
|
||||
|
||||
Instance instance1 = new Instance();
|
||||
instance1.setIp("ip1");
|
||||
|
|
@ -414,7 +415,7 @@ class NacosNamingServiceWrapperTest {
|
|||
NamingService namingService = Mockito.mock(NamingService.class);
|
||||
Mockito.when(connectionManager.getNamingService()).thenReturn(namingService);
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(connectionManager, false);
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(connectionManager, false, 0, 0);
|
||||
|
||||
Instance instance = new Instance();
|
||||
nacosNamingServiceWrapper.registerInstance("service_name", "test", instance);
|
||||
|
|
@ -494,4 +495,91 @@ class NacosNamingServiceWrapperTest {
|
|||
|
||||
Assertions.assertNotEquals(instancesInfo, instancesInfoNew);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testSuccess() {
|
||||
NamingService namingService = new MockNamingService() {
|
||||
@Override
|
||||
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
|
||||
try {
|
||||
nacosNamingServiceWrapper.registerInstance("Test", "Test", null);
|
||||
} catch (NacosException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
try {
|
||||
nacosNamingServiceWrapper.getAllInstances("Test", "Test");
|
||||
} catch (NacosException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFailNoRetry() {
|
||||
NamingService namingService = new MockNamingService() {
|
||||
@Override
|
||||
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
|
||||
throw new NacosException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
|
||||
throw new NacosException();
|
||||
}
|
||||
};
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
|
||||
Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.registerInstance("Test", "Test", null));
|
||||
Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.getAllInstances("Test", "Test"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testFailRetry() {
|
||||
NamingService namingService = new MockNamingService() {
|
||||
private final AtomicInteger count1 = new AtomicInteger(0);
|
||||
private final AtomicInteger count2 = new AtomicInteger(0);
|
||||
|
||||
@Override
|
||||
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
|
||||
if (count1.incrementAndGet() < 10) {
|
||||
throw new NacosException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Instance> getAllInstances(String serviceName, String groupName) throws NacosException {
|
||||
if (count2.incrementAndGet() < 10) {
|
||||
throw new NacosException();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 5, 10);
|
||||
Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.registerInstance("Test", "Test", null));
|
||||
try {
|
||||
nacosNamingServiceWrapper.registerInstance("Test", "Test", null);
|
||||
} catch (NacosException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
|
||||
Assertions.assertThrows(NacosException.class, () -> nacosNamingServiceWrapper.getAllInstances("Test", "Test"));
|
||||
try {
|
||||
nacosNamingServiceWrapper.getAllInstances("Test", "Test");
|
||||
} catch (NacosException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ class NacosRegistryTest {
|
|||
// ignore
|
||||
}
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService, 0, 0));
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
|
||||
nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
|
||||
|
||||
Set<URL> registered;
|
||||
|
|
@ -143,7 +143,7 @@ class NacosRegistryTest {
|
|||
// ignore
|
||||
}
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService, 0, 0));
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
|
||||
nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
|
||||
|
||||
nacosRegistry.register(serviceUrl);
|
||||
|
|
@ -183,7 +183,7 @@ class NacosRegistryTest {
|
|||
// ignore
|
||||
}
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService, 0, 0));
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
|
||||
nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
|
||||
|
||||
NotifyListener listener = mock(NotifyListener.class);
|
||||
|
|
@ -222,7 +222,7 @@ class NacosRegistryTest {
|
|||
}
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new
|
||||
NacosNamingServiceWrapper(new NacosConnectionManager(namingService, 0, 0));
|
||||
NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
|
||||
nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
|
||||
|
||||
NotifyListener listener = mock(NotifyListener.class);
|
||||
|
|
@ -276,7 +276,7 @@ class NacosRegistryTest {
|
|||
}
|
||||
|
||||
NacosNamingServiceWrapper nacosNamingServiceWrapper = new
|
||||
NacosNamingServiceWrapper(new NacosConnectionManager(namingService, 0, 0));
|
||||
NacosNamingServiceWrapper(new NacosConnectionManager(namingService), 0, 0);
|
||||
nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
|
||||
|
||||
Set<URL> registered;
|
||||
|
|
|
|||
Loading…
Reference in New Issue