微信怎么建群-华为nova 4
![nacos](/uploads/image/0960.jpg)
2023年4月4日发(作者:propertyname)
Nacos服务状态监听四种写发
监听服务的四种实现⽅式,以监听Nacos服务为例
1.传统⽅式
publicvoidsubscribe(){
try{
NamingServicenamingService=NamingService("127.0.0.1:8848");
ibe("serverName","groupName",newEventListener(){
@Override
publicvoidonEvent(Eventevent){
if(eventinstanceofNamingEvent){
n("服务名:"+((NamingEvent)event).getServiceName());
n("实例:"+((NamingEvent)event).getInstances());
}
}
});
}catch(NacosExceptione){
(sage(),e);
}
}
2.简写⽅式
publicvoidsubscribe(){
try{
NamingServicenamingService=NamingService("127.0.0.1:8848");
ibe("serverName","groupName",event->{
if(eventinstanceofNamingEvent){
n("服务名:"+((NamingEvent)event).getServiceName());
n("实例:"+((NamingEvent)event).getInstances());
}
});
}catch(NacosExceptione){
(sage(),e);
}
}
3.独⽴⽅法
publicvoidsubscribe(){
try{
NamingServicenamingService=NamingService("127.0.0.1:8848");
ibe("serverName","groupName",serviceListener);
}catch(NacosExceptione){
(sage(),e);
}
}
EventListenerserviceListener=(event)->
{
if(eventinstanceofNamingEvent){
n("服务名:"+((NamingEvent)event).getServiceName());
n("实例:"+((NamingEvent)event).getInstances());
}
};
4.独⽴⽂件
publicvoidsubscribe(){
try{
NamingServicenamingService=NamingService("127.0.0.1:8848");
ibe("serverName","groupName",newNacosListener());
}catch(NacosExceptione){
(sage(),e);
}
}
//单独Class类
publicclassNacosListenerimplementsEventListener{
@Override
publicvoidonEvent(Eventevent){
if(eventinstanceofNamingEvent){
n(((NamingEvent)event).getServiceName());
n(((NamingEvent)event).getInstances());
}
}
}
更多推荐
nacos
发布评论