当前位置:网站首页>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
边栏推荐
- Interview shock 68: why does TCP need three handshakes?
- 数通基础-网络基础知识
- MySQL 5.7.25 source code installation record
- Use of tabbarcontroller
- The fourth week of summer vacation
- Vectortilelayer replacement style
- 利用原生js实现自定义滚动条(可点击到达,拖动到达)
- Getting started with SQL - combined tables
- Draw arrows with openlayer
- Introduction to latex, EPS picture bounding box
猜你喜欢

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

数通基础-TCPIP参考模型

Cause: could't make a guess for solution

【Halcon视觉】仿射变换

Study on the basis of opencv

数通基础-二层交换原理

【Halcon视觉】图像的傅里叶变换

Uniapp error 7 < Map >: marker ID should be a number

Draw arrows with openlayer
随机推荐
Cause: couldn‘t make a guess for 解决方法
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
Wechat applet learning notes 2
Study notes of the third week of sophomore year
All codes of Tetris
Tableviewcell highly adaptive
In Net 6.0
时间序列异常检测
Use of selectors
Data communication foundation STP principle
PMM (percona monitoring and management) installation record
Wechat H5 payment on WAP, for non wechat browsers
Distributed network communication framework: how to publish local services into RPC services
Use of pclint in vs2013
Netease cloud UI imitation -- & gt; sidebar
Draw arrows with openlayer
How to use Gmail to pick up / send mail on Foxmail
SQL优化的魅力!从 30248s 到 0.001s
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
The problem of four columns of Hanoi Tower