From a7f1d5763bd86b4c1939196a5c8ab7f676508291 Mon Sep 17 00:00:00 2001 From: Taeho Kim Date: Wed, 4 Jul 2018 11:54:36 +0900 Subject: [PATCH] Bug fix: DiscoveryClient proxy casting null --- .../spring/cloud/EVCacheCloudAutoConfiguration.java | 12 ++++++++++-- .../main/java/sample/repository/TodoRepository.java | 2 +- .../src/main/resources/config/application.yml | 5 +++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/evcache-client-spring-cloud-autoconfigure/src/main/java/com/github/aafwu00/evcache/client/spring/cloud/EVCacheCloudAutoConfiguration.java b/evcache-client-spring-cloud-autoconfigure/src/main/java/com/github/aafwu00/evcache/client/spring/cloud/EVCacheCloudAutoConfiguration.java index 8ea94b9..73a9831 100644 --- a/evcache-client-spring-cloud-autoconfigure/src/main/java/com/github/aafwu00/evcache/client/spring/cloud/EVCacheCloudAutoConfiguration.java +++ b/evcache-client-spring-cloud-autoconfigure/src/main/java/com/github/aafwu00/evcache/client/spring/cloud/EVCacheCloudAutoConfiguration.java @@ -20,6 +20,8 @@ import javax.inject.Provider; +import org.springframework.aop.TargetSource; +import org.springframework.aop.framework.Advised; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -41,7 +43,6 @@ import com.netflix.evcache.connection.IConnectionFactoryProvider; import com.netflix.evcache.pool.EVCacheClientPoolManager; -import static org.springframework.aop.framework.AopProxyUtils.getSingletonTarget; import static org.springframework.aop.support.AopUtils.isAopProxy; /** @@ -90,7 +91,14 @@ private void appendProperty(final ConfigurableEnvironment environment) { private DiscoveryClient discoveryClient(final EurekaClient eurekaClient) { if (isAopProxy(eurekaClient)) { - return DiscoveryClient.class.cast(getSingletonTarget(eurekaClient)); + final TargetSource targetSource = Advised.class.cast(eurekaClient).getTargetSource(); + try { + return DiscoveryClient.class.cast(targetSource.getTarget()); + // CHECKSTYLE:OFF + } catch (final Exception e) { + // CHECKSTYLE:ON + throw new IllegalStateException(e); + } } return DiscoveryClient.class.cast(eurekaClient); } diff --git a/samples/spring-cloud-sample/src/main/java/sample/repository/TodoRepository.java b/samples/spring-cloud-sample/src/main/java/sample/repository/TodoRepository.java index d14944e..80bc8dd 100644 --- a/samples/spring-cloud-sample/src/main/java/sample/repository/TodoRepository.java +++ b/samples/spring-cloud-sample/src/main/java/sample/repository/TodoRepository.java @@ -31,7 +31,7 @@ public class TodoRepository { private static final Logger LOGGER = LoggerFactory.getLogger(TodoRepository.class); - @Cacheable(cacheNames = "TODO.todos", key = "'findAll'") + @Cacheable(cacheNames = "TODO", key = "'findAll'") public List findAll() { LOGGER.info("CALLED"); return Arrays.asList(new Todo("first"), new Todo("second")); diff --git a/samples/spring-cloud-sample/src/main/resources/config/application.yml b/samples/spring-cloud-sample/src/main/resources/config/application.yml index 77155c4..7257324 100644 --- a/samples/spring-cloud-sample/src/main/resources/config/application.yml +++ b/samples/spring-cloud-sample/src/main/resources/config/application.yml @@ -21,12 +21,13 @@ logging: evcache: clusters: - - app-name: evcache + - name: TODO + app-name: EVCACHE cache-prefix: todos time-to-live: 5 server-group-retry: true enable-exception-throwing: false -EVCACHE.use.inmemory.cache: true +EVCACHE.use.inmemory.cache: false EVCACHE.inmemory.cache.duration.ms: 1000 EVCACHE.log.operation.calls: SET,DELETE,GMISS,TMISS,BMISS_ALL,TOUCH,REPLACE