Eureka集群,事件监听解决方案

问题:Eureka集群后监听事件会推送多次,比如说你集群配置了3个地址,这样服务注册事件就会推送3次


解决方案:

    

/*
 * 服务注册事件
 */
@EventListener
public void listen(EurekaInstanceRegisteredEvent event) {  
	//处理Eureka集群监听多次问题
    if(!event.isReplication()){
		InstanceInfo instanceInfo = event.getInstanceInfo();  
		System.out.println(instanceInfo);  
	}
}

加上isReplication()判断,这个属性的意思为是否复制,也就是主注册地址为false,另外的都为true,我们只要反过来判断就行了,注意event是参数EurekaInstanceRegisteredEvent event,大家可以去看下EurekaInstanceRegisteredEvent类的源码有

replication这个属性。


猜你喜欢

转载自blog.csdn.net/u011974797/article/details/80064800