当前位置:网站首页>Nacos custom service change subscription
Nacos custom service change subscription
2022-07-26 10:17:00 【Whale-52 Hz】
nacos Custom service change subscription
1. Write custom subscribers
The reference here is InstancesChangeNotifier This class .
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.client.naming.event.InstancesChangeEvent;
import com.alibaba.nacos.common.notify.listener.Subscriber;
import com.alibaba.nacos.common.notify.Event;
public class MyChangeNotifier extends Subscriber<InstancesChangeEvent> {
@Override
public void onEvent(InstancesChangeEvent event) {
// TODO Changes in subscribed services will trigger
// Here we get all the information about the service cluster , Include service name 、 Information of each node .
NamingEvent namingEvent = transferToNamingEvent(event);
}
@Override
public Class<? extends Event> subscribeType() {
return InstancesChangeEvent.class;
}
private NamingEvent transferToNamingEvent(
InstancesChangeEvent instancesChangeEvent) {
return new NamingEvent(instancesChangeEvent.getServiceName(), instancesChangeEvent.getGroupName(),
instancesChangeEvent.getClusters(), instancesChangeEvent.getHosts());
}
}
2. Register custom subscribers
// After the project starts , Or register your subscribers when you want to register .
// Be careful : Register different subscriber objects multiple times , Then there will really be many same subscribers . There is no problem registering the same object multiple times . It can be understood as going to set Riga objects
NotifyCenter.registerSubscriber(new MyChangeNotifier());
3. Subscribe to the service you want to subscribe to
Other services used in normal projects will be automatically subscribed , If you want to add additional subscriptions manually , The simplest way is to get it manually .
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
@Component
public class Test {
@Autowired
private DiscoveryClient discoveryClient;
public void test(){
// This is equivalent to subscribing to the specified service
discoveryClient.getInstances("service-name");
}
Subscription service , What is really called is
com.alibaba.nacos.client.naming.NacosNamingService#selectInstances(java.lang.String, java.lang.String, boolean).
complete nacos Subscription logic in com.alibaba.nacos.client.naming.core.HostReactor#getServiceInfo in
边栏推荐
- Flask框架初学-03-模板
- Installation and use of cocoapods
- 30 minutes to thoroughly understand the synchronized lock upgrade process
- 在.NET 6.0中配置WebHostBuilder
- Solve the problem of storing cookies in IE7 & IE8
- 点赞,《新程序员》电子书限时免费领啦!
- Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
- Wechat applet learning notes 1
- PHP one-time request lifecycle
- Learning about tensorflow (I)
猜你喜欢
Flask框架初学-03-模板
Session based recommendations with recurrent neural networks
【Halcon视觉】图像滤波
INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
SQL优化的魅力!从 30248s 到 0.001s
Docker configuring MySQL Cluster
Learning about opencv (3)
Principle analysis and source code interpretation of service discovery
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
点赞,《新程序员》电子书限时免费领啦!
随机推荐
The fourth week of summer vacation
简单化构造函数的继承方法(一)- 组合继承
MySQL 5.7.25 source code installation record
Tableviewcell highly adaptive
SPARK中 DS V2 push down(下推)的一些说明
Study notes of the third week of sophomore year
Replay the snake game with C language (II) end
Show default image when wechat applet image cannot be displayed
Wechat applet learning notes 2
数据库的复习--1.概述
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
Leetcode 504. Hex number
Applet record
Draw arrows with openlayer
SQL Server 2008 R2 installation problems
Data communication foundation STP principle
数通基础-二层交换原理
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
El table implements adding / deleting rows, and a parameter changes accordingly