From d542b1b080315f3dd7b3516773eabc4dbab429ea Mon Sep 17 00:00:00 2001 From: wxz Date: Wed, 22 Sep 2021 17:50:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=20=E3=80=90?= =?UTF-8?q?=E5=B9=B3=E6=BB=91=E9=83=A8=E7=BD=B2=E3=80=91NacosServiceListLi?= =?UTF-8?q?stenerRegisterer=E6=B3=A8=E5=86=8C=E5=99=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=B9=E6=B3=95=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NacosServiceListListenerRegisterer.java | 36 +++++++++++-------- .../NacosServiceListListenerRegisterer.java | 36 +++++++++++-------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/epmet-module/epmet-point/epmet-point-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java b/epmet-module/epmet-point/epmet-point-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java index dca0edb665..c971d433b3 100644 --- a/epmet-module/epmet-point/epmet-point-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java +++ b/epmet-module/epmet-point/epmet-point-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java @@ -8,9 +8,8 @@ import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.listener.NamingEvent; import com.alibaba.nacos.api.naming.pojo.ListView; import com.epmet.commons.tools.exception.ExceptionUtils; -import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.DynamicServerListLoadBalancer; import com.netflix.loadbalancer.ILoadBalancer; -import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -18,8 +17,6 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; @@ -136,18 +133,27 @@ public class NacosServiceListListenerRegisterer { * @date 2021.09.22 09:29:16 */ private void doRefreshServerList(String serviceName) { + // 刷新方式1:反射调用DynamicServerListLoadBalancer中的restOfInit()方法。该方法原来只执行一次,此处不推荐用 + //ILoadBalancer loadBalancer = springClientFactory.getLoadBalancer(serviceName); + //if (loadBalancer instanceof ZoneAwareLoadBalancer) { + // ZoneAwareLoadBalancer zaLoadBalancer = (ZoneAwareLoadBalancer) loadBalancer; + // IClientConfig clientConfig = springClientFactory.getClientConfig(serviceName); + // try { + // Method restOfInitMethod = zaLoadBalancer.getClass().getSuperclass().getDeclaredMethod(REFRESH_SERVER_LIST_METHOD_NAME, IClientConfig.class); + // restOfInitMethod.setAccessible(true); + // restOfInitMethod.invoke(zaLoadBalancer, clientConfig); + // } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + // String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); + // log.error("【LoadBalancer刷新服务列表】失败:{}", errorStackTrace); + // } + //} + + // 刷新方式2:DynamicServerListLoadBalancer#updateListOfServers()该方法为ribbon定时刷新服务列表的时候真正调用的方法,但是加了@VisibleForTesting + // 暂且 1 try ILoadBalancer loadBalancer = springClientFactory.getLoadBalancer(serviceName); - if (loadBalancer instanceof ZoneAwareLoadBalancer) { - ZoneAwareLoadBalancer zaLoadBalancer = (ZoneAwareLoadBalancer) loadBalancer; - IClientConfig clientConfig = springClientFactory.getClientConfig(serviceName); - try { - Method restOfInitMethod = zaLoadBalancer.getClass().getSuperclass().getDeclaredMethod(REFRESH_SERVER_LIST_METHOD_NAME, IClientConfig.class); - restOfInitMethod.setAccessible(true); - restOfInitMethod.invoke(zaLoadBalancer, clientConfig); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); - log.error("【LoadBalancer刷新服务列表】失败:{}", errorStackTrace); - } + if (loadBalancer instanceof DynamicServerListLoadBalancer) { + DynamicServerListLoadBalancer dslb = (DynamicServerListLoadBalancer) loadBalancer; + dslb.updateListOfServers(); } } } diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java index dca0edb665..c971d433b3 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/config/NacosServiceListListenerRegisterer.java @@ -8,9 +8,8 @@ import com.alibaba.nacos.api.naming.listener.EventListener; import com.alibaba.nacos.api.naming.listener.NamingEvent; import com.alibaba.nacos.api.naming.pojo.ListView; import com.epmet.commons.tools.exception.ExceptionUtils; -import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.DynamicServerListLoadBalancer; import com.netflix.loadbalancer.ILoadBalancer; -import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -18,8 +17,6 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.concurrent.*; @@ -136,18 +133,27 @@ public class NacosServiceListListenerRegisterer { * @date 2021.09.22 09:29:16 */ private void doRefreshServerList(String serviceName) { + // 刷新方式1:反射调用DynamicServerListLoadBalancer中的restOfInit()方法。该方法原来只执行一次,此处不推荐用 + //ILoadBalancer loadBalancer = springClientFactory.getLoadBalancer(serviceName); + //if (loadBalancer instanceof ZoneAwareLoadBalancer) { + // ZoneAwareLoadBalancer zaLoadBalancer = (ZoneAwareLoadBalancer) loadBalancer; + // IClientConfig clientConfig = springClientFactory.getClientConfig(serviceName); + // try { + // Method restOfInitMethod = zaLoadBalancer.getClass().getSuperclass().getDeclaredMethod(REFRESH_SERVER_LIST_METHOD_NAME, IClientConfig.class); + // restOfInitMethod.setAccessible(true); + // restOfInitMethod.invoke(zaLoadBalancer, clientConfig); + // } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + // String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); + // log.error("【LoadBalancer刷新服务列表】失败:{}", errorStackTrace); + // } + //} + + // 刷新方式2:DynamicServerListLoadBalancer#updateListOfServers()该方法为ribbon定时刷新服务列表的时候真正调用的方法,但是加了@VisibleForTesting + // 暂且 1 try ILoadBalancer loadBalancer = springClientFactory.getLoadBalancer(serviceName); - if (loadBalancer instanceof ZoneAwareLoadBalancer) { - ZoneAwareLoadBalancer zaLoadBalancer = (ZoneAwareLoadBalancer) loadBalancer; - IClientConfig clientConfig = springClientFactory.getClientConfig(serviceName); - try { - Method restOfInitMethod = zaLoadBalancer.getClass().getSuperclass().getDeclaredMethod(REFRESH_SERVER_LIST_METHOD_NAME, IClientConfig.class); - restOfInitMethod.setAccessible(true); - restOfInitMethod.invoke(zaLoadBalancer, clientConfig); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - String errorStackTrace = ExceptionUtils.getErrorStackTrace(e); - log.error("【LoadBalancer刷新服务列表】失败:{}", errorStackTrace); - } + if (loadBalancer instanceof DynamicServerListLoadBalancer) { + DynamicServerListLoadBalancer dslb = (DynamicServerListLoadBalancer) loadBalancer; + dslb.updateListOfServers(); } } }