From 22cf6eabeb4e5ad8dbf5a9001e4ac68817149429 Mon Sep 17 00:00:00 2001
From: ChengJie1053 <18033291053@163.com>
Date: Sat, 27 Jan 2024 10:29:35 +0800
Subject: [PATCH] Eureka refresh is called via reflection instead
---
linkis-commons/linkis-rpc/pom.xml | 9 ++++++++
.../EurekaClientCacheManualRefresher.java | 22 +++++++++++--------
2 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/linkis-commons/linkis-rpc/pom.xml b/linkis-commons/linkis-rpc/pom.xml
index 70ef8e6bc7..9447c6f554 100644
--- a/linkis-commons/linkis-rpc/pom.xml
+++ b/linkis-commons/linkis-rpc/pom.xml
@@ -40,8 +40,17 @@
org.springframework.cloud
spring-cloud-commons
+
+ org.springframework.cloud
+ spring-cloud-loadbalancer
+
+
+ org.springframework.cloud
+ spring-cloud-loadbalancer
+ ${spring-netflix.version}
+
org.springframework.cloud
spring-cloud-starter-openfeign
diff --git a/linkis-commons/linkis-rpc/src/main/java/org/apache/linkis/rpc/conf/EurekaClientCacheManualRefresher.java b/linkis-commons/linkis-rpc/src/main/java/org/apache/linkis/rpc/conf/EurekaClientCacheManualRefresher.java
index 48d0a0dc45..2fad4432ea 100644
--- a/linkis-commons/linkis-rpc/src/main/java/org/apache/linkis/rpc/conf/EurekaClientCacheManualRefresher.java
+++ b/linkis-commons/linkis-rpc/src/main/java/org/apache/linkis/rpc/conf/EurekaClientCacheManualRefresher.java
@@ -25,14 +25,13 @@
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
+import java.lang.reflect.Method;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
-import com.netflix.discovery.DiscoveryClient;
-import com.netflix.discovery.TimedSupervisorTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,7 +42,7 @@ public class EurekaClientCacheManualRefresher {
private final AtomicBoolean isRefreshing = new AtomicBoolean(false);
private final ExecutorService refreshExecutor = Executors.newSingleThreadExecutor();
private final String cacheRefreshTaskField = "cacheRefreshTask";
- private TimedSupervisorTask cacheRefreshTask;
+ private Object cacheRefreshTask;
private long lastRefreshMillis = 0;
private final Duration refreshIntervalDuration = Duration.ofSeconds(3);
@@ -75,14 +74,15 @@ public void refresh() {
return;
}
+ String discoveryClientClassName = "com.netflix.discovery.DiscoveryClient";
if (null == cacheRefreshTask) {
+ Class> discoveryClientClass = Class.forName(discoveryClientClassName);
Field field =
- ReflectionUtils.findField(DiscoveryClient.class, cacheRefreshTaskField);
+ ReflectionUtils.findField(discoveryClientClass, cacheRefreshTaskField);
if (null != field) {
ReflectionUtils.makeAccessible(field);
- DiscoveryClient discoveryClient = beanFactory.getBean(DiscoveryClient.class);
- cacheRefreshTask =
- (TimedSupervisorTask) ReflectionUtils.getField(field, discoveryClient);
+ Object discoveryClient = beanFactory.getBean(discoveryClientClass);
+ cacheRefreshTask = ReflectionUtils.getField(field, discoveryClient);
}
}
@@ -90,12 +90,16 @@ public void refresh() {
logger.error(
"Field ({}) not found in class '{}'",
cacheRefreshTaskField,
- DiscoveryClient.class.getSimpleName());
+ discoveryClientClassName);
return;
}
lastRefreshMillis = System.currentTimeMillis();
- cacheRefreshTask.run();
+ Class> timedSupervisorTaskClass =
+ Class.forName("com.netflix.discovery.TimedSupervisorTask");
+ Method method = timedSupervisorTaskClass.getDeclaredMethod("run");
+ method.setAccessible(true);
+ method.invoke(cacheRefreshTask);
logger.info(
"Manually refresh eureka client cache completed(DiscoveryClient.cacheRefreshTask#run())");
} catch (Exception e) {