use of org.apache.dubbo.registry.NotifyListener 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 org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class RedisRegistry method doNotify.
private void doNotify(Collection<String> keys, URL url, Collection<NotifyListener> listeners) {
if (keys == null || keys.isEmpty() || listeners == null || listeners.isEmpty()) {
return;
}
long now = System.currentTimeMillis();
List<URL> result = new ArrayList<>();
List<String> categories = Arrays.asList(url.getParameter(CATEGORY_KEY, new String[0]));
String consumerService = url.getServiceInterface();
for (String key : keys) {
if (!ANY_VALUE.equals(consumerService)) {
String providerService = toServiceName(key);
if (!providerService.equals(consumerService)) {
continue;
}
}
String category = toCategoryName(key);
if (!categories.contains(ANY_VALUE) && !categories.contains(category)) {
continue;
}
List<URL> urls = new ArrayList<>();
Set<URL> toDeleteExpireKeys = new HashSet<>(expireCache.keySet());
Map<String, String> values = redisClient.hgetAll(key);
if (CollectionUtils.isNotEmptyMap(values)) {
for (Map.Entry<String, String> entry : values.entrySet()) {
URL u = URL.valueOf(entry.getKey());
long expire = Long.parseLong(entry.getValue());
if (!u.getParameter(DYNAMIC_KEY, true) || expire >= now) {
if (UrlUtils.isMatch(url, u)) {
urls.add(u);
expireCache.put(u, expire);
toDeleteExpireKeys.remove(u);
}
}
}
}
if (!toDeleteExpireKeys.isEmpty()) {
for (URL u : toDeleteExpireKeys) {
expireCache.remove(u);
}
}
if (urls.isEmpty()) {
urls.add(URLBuilder.from(url).setProtocol(EMPTY_PROTOCOL).setAddress(ANYHOST_VALUE).setPath(toServiceName(key)).addParameter(CATEGORY_KEY, category).build());
}
result.addAll(urls);
if (logger.isInfoEnabled()) {
logger.info("redis notify: " + key + " = " + urls);
}
}
if (CollectionUtils.isEmpty(result)) {
return;
}
for (NotifyListener listener : listeners) {
notify(url, listener, result);
}
}
use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class RedisRegistryTest method testSubscribeAndUnsubscribe.
@Test
public void testSubscribeAndUnsubscribe() {
NotifyListener listener = new NotifyListener() {
@Override
public void notify(List<URL> urls) {
}
};
redisRegistry.subscribe(SERVICE_URL, listener);
Map<URL, Set<NotifyListener>> subscribed = redisRegistry.getSubscribed();
assertThat(subscribed.size(), is(1));
assertThat(subscribed.get(SERVICE_URL).size(), is(1));
redisRegistry.unsubscribe(SERVICE_URL, listener);
subscribed = redisRegistry.getSubscribed();
assertThat(subscribed.get(SERVICE_URL).size(), is(0));
}
use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class SimpleRegistryService method disconnect.
public void disconnect() {
String client = RpcContext.getContext().getRemoteAddressString();
if (logger.isInfoEnabled()) {
logger.info("Disconnected " + client);
}
ConcurrentMap<String, URL> urls = remoteRegistered.get(client);
if (urls != null && urls.size() > 0) {
for (Map.Entry<String, URL> entry : urls.entrySet()) {
super.unregister(entry.getKey(), entry.getValue());
}
}
Map<String, NotifyListener> listeners = remoteListeners.get(client);
if (listeners != null && listeners.size() > 0) {
for (Map.Entry<String, NotifyListener> entry : listeners.entrySet()) {
String service = entry.getKey();
super.unsubscribe(service, new URL("subscribe", RpcContext.getContext().getRemoteHost(), RpcContext.getContext().getRemotePort(), org.apache.dubbo.registry.RegistryService.class.getName(), getSubscribed(service)), entry.getValue());
}
}
}
use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class SimpleRegistryService method subscribe.
@Override
public void subscribe(String service, URL url, NotifyListener listener) {
String client = RpcContext.getContext().getRemoteAddressString();
if (logger.isInfoEnabled()) {
logger.info("[subscribe] service: " + service + ",client:" + client);
}
List<URL> urls = getRegistered().get(service);
if ((RegistryService.class.getName() + ":0.0.0").equals(service) && (urls == null || urls.size() == 0)) {
register(service, new URL("dubbo", NetUtils.getLocalHost(), RpcContext.getContext().getLocalPort(), org.apache.dubbo.registry.RegistryService.class.getName(), url.getParameters()));
List<String> rs = registries;
if (rs != null && rs.size() > 0) {
for (String registry : rs) {
register(service, UrlUtils.parseURL(registry, url.getParameters()));
}
}
}
super.subscribe(service, url, listener);
Map<String, NotifyListener> listeners = remoteListeners.computeIfAbsent(client, k -> new ConcurrentHashMap<>());
listeners.put(service, listener);
urls = getRegistered().get(service);
if (urls != null && urls.size() > 0) {
listener.notify(urls);
}
}
Aggregations