Skip to content

Commit

Permalink
Eureka refresh is called via reflection instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengJie1053 committed Jan 27, 2024
1 parent 805ddbf commit 22cf6ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
9 changes: 9 additions & 0 deletions linkis-commons/linkis-rpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,17 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>${spring-netflix.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -75,27 +74,32 @@ 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);
}
}

if (null == cacheRefreshTask) {
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) {
Expand Down

0 comments on commit 22cf6ea

Please sign in to comment.