## 添加微服务注册成功刷新nacos缓存;
This commit is contained in:
		
							parent
							
								
									ed7cf86412
								
							
						
					
					
						commit
						321c56547f
					
				|  | @ -12,11 +12,7 @@ import org.springframework.messaging.Message; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * @Description: 订单秒接 |  * 订单秒接 | ||||||
|  * @Author: sui q |  | ||||||
|  * @Time: 2023/3/1 16:30 |  | ||||||
|  * @Classname: SaOrderSecondListener |  | ||||||
|  * @PackageName: com.hzs.third.mqcall.listener |  | ||||||
|  */ |  */ | ||||||
| @Slf4j | @Slf4j | ||||||
| @Component | @Component | ||||||
|  | @ -42,6 +38,5 @@ public class SaOrderSecondListener { | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             e.printStackTrace(); |             e.printStackTrace(); | ||||||
|         } |         } | ||||||
|         // todo 失败的时候记录日志 |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,43 @@ | ||||||
|  | package com.hzs.listener; | ||||||
|  | 
 | ||||||
|  | import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; | ||||||
|  | import com.alibaba.nacos.common.notify.NotifyCenter; | ||||||
|  | import com.alibaba.nacos.common.notify.listener.Subscriber; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.cache.Cache; | ||||||
|  | import org.springframework.cache.CacheManager; | ||||||
|  | import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | import javax.annotation.PostConstruct; | ||||||
|  | import javax.annotation.Resource; | ||||||
|  | 
 | ||||||
|  | @Component | ||||||
|  | @Slf4j | ||||||
|  | public class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> { | ||||||
|  | 
 | ||||||
|  |     @Resource | ||||||
|  |     private CacheManager defaultLoadBalancerCacheManager; | ||||||
|  | 
 | ||||||
|  |     @PostConstruct | ||||||
|  |     public void registerToNotifyCenter(){ | ||||||
|  |         NotifyCenter.registerSubscriber(this); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onEvent(InstancesChangeEvent event) { | ||||||
|  |         log.info("bonus 接收微服务刷新事件:{}, 开始刷新本地存储的微服务缓存", event.getServiceName()); | ||||||
|  |         Cache cache = defaultLoadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME); | ||||||
|  |         if (cache != null) { | ||||||
|  |             cache.evict(event.getServiceName()); | ||||||
|  |             log.info("{} 微服务刷新", event.getServiceName()); | ||||||
|  |         } | ||||||
|  |         log.info("bonus 微服务缓存刷新完成"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() { | ||||||
|  |         return InstancesChangeEvent.class; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1,43 @@ | ||||||
|  | package com.hzs.listener; | ||||||
|  | 
 | ||||||
|  | import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; | ||||||
|  | import com.alibaba.nacos.common.notify.NotifyCenter; | ||||||
|  | import com.alibaba.nacos.common.notify.listener.Subscriber; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.cache.Cache; | ||||||
|  | import org.springframework.cache.CacheManager; | ||||||
|  | import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | import javax.annotation.PostConstruct; | ||||||
|  | import javax.annotation.Resource; | ||||||
|  | 
 | ||||||
|  | @Component | ||||||
|  | @Slf4j | ||||||
|  | public class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> { | ||||||
|  | 
 | ||||||
|  |     @Resource | ||||||
|  |     private CacheManager defaultLoadBalancerCacheManager; | ||||||
|  | 
 | ||||||
|  |     @PostConstruct | ||||||
|  |     public void registerToNotifyCenter(){ | ||||||
|  |         NotifyCenter.registerSubscriber(this); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onEvent(InstancesChangeEvent event) { | ||||||
|  |         log.info("member 接收微服务刷新事件:{}, 开始刷新本地存储的微服务缓存", event.getServiceName()); | ||||||
|  |         Cache cache = defaultLoadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME); | ||||||
|  |         if (cache != null) { | ||||||
|  |             cache.evict(event.getServiceName()); | ||||||
|  |             log.info("{} 微服务刷新", event.getServiceName()); | ||||||
|  |         } | ||||||
|  |         log.info("member 微服务缓存刷新完成"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() { | ||||||
|  |         return InstancesChangeEvent.class; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1,43 @@ | ||||||
|  | package com.hzs.listener; | ||||||
|  | 
 | ||||||
|  | import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; | ||||||
|  | import com.alibaba.nacos.common.notify.NotifyCenter; | ||||||
|  | import com.alibaba.nacos.common.notify.listener.Subscriber; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.cache.Cache; | ||||||
|  | import org.springframework.cache.CacheManager; | ||||||
|  | import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | import javax.annotation.PostConstruct; | ||||||
|  | import javax.annotation.Resource; | ||||||
|  | 
 | ||||||
|  | @Component | ||||||
|  | @Slf4j | ||||||
|  | public class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> { | ||||||
|  | 
 | ||||||
|  |     @Resource | ||||||
|  |     private CacheManager defaultLoadBalancerCacheManager; | ||||||
|  | 
 | ||||||
|  |     @PostConstruct | ||||||
|  |     public void registerToNotifyCenter(){ | ||||||
|  |         NotifyCenter.registerSubscriber(this); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onEvent(InstancesChangeEvent event) { | ||||||
|  |         log.info("report 接收微服务刷新事件:{}, 开始刷新本地存储的微服务缓存", event.getServiceName()); | ||||||
|  |         Cache cache = defaultLoadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME); | ||||||
|  |         if (cache != null) { | ||||||
|  |             cache.evict(event.getServiceName()); | ||||||
|  |             log.info("{} 微服务刷新", event.getServiceName()); | ||||||
|  |         } | ||||||
|  |         log.info("report 微服务缓存刷新完成"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() { | ||||||
|  |         return InstancesChangeEvent.class; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1,43 @@ | ||||||
|  | package com.hzs.listener; | ||||||
|  | 
 | ||||||
|  | import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; | ||||||
|  | import com.alibaba.nacos.common.notify.NotifyCenter; | ||||||
|  | import com.alibaba.nacos.common.notify.listener.Subscriber; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.cache.Cache; | ||||||
|  | import org.springframework.cache.CacheManager; | ||||||
|  | import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | import javax.annotation.PostConstruct; | ||||||
|  | import javax.annotation.Resource; | ||||||
|  | 
 | ||||||
|  | @Component | ||||||
|  | @Slf4j | ||||||
|  | public class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> { | ||||||
|  | 
 | ||||||
|  |     @Resource | ||||||
|  |     private CacheManager defaultLoadBalancerCacheManager; | ||||||
|  | 
 | ||||||
|  |     @PostConstruct | ||||||
|  |     public void registerToNotifyCenter(){ | ||||||
|  |         NotifyCenter.registerSubscriber(this); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onEvent(InstancesChangeEvent event) { | ||||||
|  |         log.info("sale 接收微服务刷新事件:{}, 开始刷新本地存储的微服务缓存", event.getServiceName()); | ||||||
|  |         Cache cache = defaultLoadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME); | ||||||
|  |         if (cache != null) { | ||||||
|  |             cache.evict(event.getServiceName()); | ||||||
|  |             log.info("{} 微服务刷新", event.getServiceName()); | ||||||
|  |         } | ||||||
|  |         log.info("sale 微服务缓存刷新完成"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() { | ||||||
|  |         return InstancesChangeEvent.class; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -16,17 +16,13 @@ import java.util.Date; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * @Description: 配置controller |  * 配置controller | ||||||
|  * @Author: sui q |  | ||||||
|  * @Time: 2023/3/10 15:45 |  | ||||||
|  * @Classname: ConfigBaseController |  | ||||||
|  * @PackageName: com.hzs.config.pub.controller |  | ||||||
|  */ |  */ | ||||||
| @Slf4j | @Slf4j | ||||||
| public abstract class ConfigBaseController extends BaseController { | public abstract class ConfigBaseController extends BaseController { | ||||||
| 
 | 
 | ||||||
|     @DubboReference |     @DubboReference | ||||||
|     private IDelayedMessageApi delayedMessageApi; |     IDelayedMessageApi delayedMessageApi; | ||||||
| 
 | 
 | ||||||
|     protected boolean sendDelayedMessage(Object config, Integer delayBusType, String key, String... otherKey) { |     protected boolean sendDelayedMessage(Object config, Integer delayBusType, String key, String... otherKey) { | ||||||
|         Integer pkId = null; |         Integer pkId = null; | ||||||
|  | @ -63,18 +59,13 @@ public abstract class ConfigBaseController extends BaseController { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * @description: 校验object的属性值是Long类型还是Integer |      * 校验object的属性值是Long类型还是Integer | ||||||
|      * @author: zhang jing |  | ||||||
|      * @date: 2024/5/20 16:01 |  | ||||||
|      * @param: [field, object] |  | ||||||
|      **/ |      **/ | ||||||
|     public static boolean isLong(Object object) { |     public static boolean isLong(Object object) { | ||||||
|         if (object instanceof Long) { |         if (object instanceof Long) { | ||||||
|             return false; |             return false; | ||||||
|         } else if (object instanceof Integer) { |  | ||||||
|             return true; |  | ||||||
|         } else { |         } else { | ||||||
|             return false; |             return object instanceof Integer; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -0,0 +1,43 @@ | ||||||
|  | package com.hzs.listener; | ||||||
|  | 
 | ||||||
|  | import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; | ||||||
|  | import com.alibaba.nacos.common.notify.NotifyCenter; | ||||||
|  | import com.alibaba.nacos.common.notify.listener.Subscriber; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.cache.Cache; | ||||||
|  | import org.springframework.cache.CacheManager; | ||||||
|  | import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | import javax.annotation.PostConstruct; | ||||||
|  | import javax.annotation.Resource; | ||||||
|  | 
 | ||||||
|  | @Component | ||||||
|  | @Slf4j | ||||||
|  | public class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> { | ||||||
|  | 
 | ||||||
|  |     @Resource | ||||||
|  |     private CacheManager defaultLoadBalancerCacheManager; | ||||||
|  | 
 | ||||||
|  |     @PostConstruct | ||||||
|  |     public void registerToNotifyCenter(){ | ||||||
|  |         NotifyCenter.registerSubscriber(this); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onEvent(InstancesChangeEvent event) { | ||||||
|  |         log.info("system 接收微服务刷新事件:{}, 开始刷新本地存储的微服务缓存", event.getServiceName()); | ||||||
|  |         Cache cache = defaultLoadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME); | ||||||
|  |         if (cache != null) { | ||||||
|  |             cache.evict(event.getServiceName()); | ||||||
|  |             log.info("{} 微服务刷新", event.getServiceName()); | ||||||
|  |         } | ||||||
|  |         log.info("system 微服务缓存刷新完成"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() { | ||||||
|  |         return InstancesChangeEvent.class; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -0,0 +1,43 @@ | ||||||
|  | package com.hzs.listener; | ||||||
|  | 
 | ||||||
|  | import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; | ||||||
|  | import com.alibaba.nacos.common.notify.NotifyCenter; | ||||||
|  | import com.alibaba.nacos.common.notify.listener.Subscriber; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.cache.Cache; | ||||||
|  | import org.springframework.cache.CacheManager; | ||||||
|  | import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier; | ||||||
|  | import org.springframework.stereotype.Component; | ||||||
|  | 
 | ||||||
|  | import javax.annotation.PostConstruct; | ||||||
|  | import javax.annotation.Resource; | ||||||
|  | 
 | ||||||
|  | @Component | ||||||
|  | @Slf4j | ||||||
|  | public class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> { | ||||||
|  | 
 | ||||||
|  |     @Resource | ||||||
|  |     private CacheManager defaultLoadBalancerCacheManager; | ||||||
|  | 
 | ||||||
|  |     @PostConstruct | ||||||
|  |     public void registerToNotifyCenter(){ | ||||||
|  |         NotifyCenter.registerSubscriber(this); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void onEvent(InstancesChangeEvent event) { | ||||||
|  |         log.info("third 接收微服务刷新事件:{}, 开始刷新本地存储的微服务缓存", event.getServiceName()); | ||||||
|  |         Cache cache = defaultLoadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME); | ||||||
|  |         if (cache != null) { | ||||||
|  |             cache.evict(event.getServiceName()); | ||||||
|  |             log.info("{} 微服务刷新", event.getServiceName()); | ||||||
|  |         } | ||||||
|  |         log.info("third 微服务缓存刷新完成"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() { | ||||||
|  |         return InstancesChangeEvent.class; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue