use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.
the class NacosRegistryTest method testUnSubscribe.
@Test
public void testUnSubscribe() {
NamingService namingService = mock(NacosNamingService.class);
try {
String serviceName = "providers:org.apache.dubbo.registry.nacos.NacosService:1.0.0:default";
String category = this.serviceUrl.getParameter(CATEGORY_KEY, DEFAULT_CATEGORY);
URL newUrl = this.serviceUrl.addParameter(CATEGORY_KEY, category);
newUrl = newUrl.addParameter(PROTOCOL_KEY, this.serviceUrl.getProtocol());
newUrl = newUrl.addParameter(PATH_KEY, this.serviceUrl.getPath());
newUrl = newUrl.addParameters(NacosNamingServiceUtils.getNacosPreservedParam(this.serviceUrl));
String ip = newUrl.getHost();
int port = newUrl.getPort();
Instance instance = new Instance();
instance.setIp(ip);
instance.setPort(port);
instance.setMetadata(new HashMap<>(newUrl.getParameters()));
List<Instance> instances = new ArrayList<>();
instances.add(instance);
when(namingService.getAllInstances(serviceName, this.registryUrl.getParameter(GROUP_KEY, Constants.DEFAULT_GROUP))).thenReturn(instances);
} catch (NacosException e) {
// ignore
}
NacosNamingServiceWrapper nacosNamingServiceWrapper = new NacosNamingServiceWrapper(namingService);
nacosRegistry = new NacosRegistry(this.registryUrl, nacosNamingServiceWrapper);
NotifyListener listener = mock(NotifyListener.class);
nacosRegistry.subscribe(serviceUrl, listener);
Map<URL, Set<NotifyListener>> subscribed = nacosRegistry.getSubscribed();
assertThat(subscribed.size(), is(1));
assertThat(subscribed.get(serviceUrl).size(), is(1));
nacosRegistry.unsubscribe(serviceUrl, listener);
subscribed = nacosRegistry.getSubscribed();
assertThat(subscribed.size(), is(1));
assertThat(subscribed.get(serviceUrl).size(), is(0));
}
use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.
the class NacosNamingServiceUtils method createNamingService.
/**
* Create an instance of {@link NamingService} from specified {@link URL connection url}
*
* @param connectionURL {@link URL connection url}
* @return {@link NamingService}
* @since 2.7.5
*/
public static NacosNamingServiceWrapper createNamingService(URL connectionURL) {
Properties nacosProperties = buildNacosProperties(connectionURL);
NamingService namingService;
try {
namingService = NacosFactory.createNamingService(nacosProperties);
} catch (NacosException e) {
if (logger.isErrorEnabled()) {
logger.error(e.getErrMsg(), e);
}
throw new IllegalStateException(e);
}
return new NacosNamingServiceWrapper(namingService);
}
use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.
the class NacosServiceDiscovery method addServiceInstancesChangedListener.
@Override
public void addServiceInstancesChangedListener(ServiceInstancesChangedListener listener) throws NullPointerException, IllegalArgumentException {
execute(namingService, service -> {
listener.getServiceNames().forEach(serviceName -> {
try {
service.subscribe(serviceName, e -> {
// Register Nacos EventListener
if (e instanceof NamingEvent) {
NamingEvent event = (NamingEvent) e;
handleEvent(event, listener);
}
});
} catch (NacosException e) {
e.printStackTrace();
}
});
});
}
use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.
the class NacosRegistry method subscribeEventListener.
private void subscribeEventListener(String serviceName, final URL url, final NotifyListener listener) throws NacosException {
ConcurrentMap<NotifyListener, EventListener> listeners = nacosListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
EventListener nacosListener = listeners.computeIfAbsent(listener, k -> {
EventListener eventListener = event -> {
if (event instanceof NamingEvent) {
NamingEvent e = (NamingEvent) event;
List<Instance> instances = e.getInstances();
if (isServiceNamesWithCompatibleMode(url)) {
// Get all instances with corresponding serviceNames to avoid instance overwrite and but with empty instance mentioned
// in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
NacosInstanceManageUtil.initOrRefreshServiceInstanceList(serviceName, instances);
instances = NacosInstanceManageUtil.getAllCorrespondingServiceInstanceList(serviceName);
}
notifySubscriber(url, listener, instances);
}
};
return eventListener;
});
namingService.subscribe(serviceName, getUrl().getParameter(GROUP_KEY, Constants.DEFAULT_GROUP), nacosListener);
}
use of com.alibaba.nacos.api.exception.NacosException in project dubbo by alibaba.
the class NacosDynamicConfigurationTest method setUp.
@BeforeAll
public static void setUp() {
String urlForDubbo = "nacos://" + "127.0.0.1:8848" + "/org.apache.dubbo.nacos.testService";
// timeout in 15 seconds.
URL url = URL.valueOf(urlForDubbo).addParameter(SESSION_TIMEOUT_KEY, 15000);
config = new NacosDynamicConfiguration(url);
try {
nacosClient = NacosFactory.createConfigService("127.0.0.1:8848");
} catch (NacosException e) {
e.printStackTrace();
}
}
Aggregations