From 21b141d50090971ca18d17e65db73daa3355ef65 Mon Sep 17 00:00:00 2001 From: echooymxq Date: Wed, 21 Aug 2019 01:34:02 +0800 Subject: [PATCH] Check nacos config health use configService --- .../NacosConfigEndpointAutoConfiguration.java | 3 +- .../endpoint/NacosConfigHealthIndicator.java | 41 +++---------------- .../endpoint/NacosConfigEndpointTests.java | 30 +++++--------- 3 files changed, 16 insertions(+), 58 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java index 2ebeeb5e6..19ded2ac2 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java @@ -51,7 +51,6 @@ public class NacosConfigEndpointAutoConfiguration { @Bean public NacosConfigHealthIndicator nacosConfigHealthIndicator() { - return new NacosConfigHealthIndicator(nacosConfigProperties, - nacosConfigProperties.configServiceInstance()); + return new NacosConfigHealthIndicator(nacosConfigProperties.configServiceInstance()); } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigHealthIndicator.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigHealthIndicator.java index 4954378c1..7fe5a1895 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigHealthIndicator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/endpoint/NacosConfigHealthIndicator.java @@ -16,16 +16,9 @@ package com.alibaba.cloud.nacos.endpoint; -import java.util.ArrayList; -import java.util.List; - import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; -import org.springframework.util.StringUtils; -import com.alibaba.cloud.nacos.NacosConfigProperties; -import com.alibaba.cloud.nacos.NacosPropertySourceRepository; -import com.alibaba.cloud.nacos.client.NacosPropertySource; import com.alibaba.nacos.api.config.ConfigService; /** @@ -33,41 +26,17 @@ import com.alibaba.nacos.api.config.ConfigService; */ public class NacosConfigHealthIndicator extends AbstractHealthIndicator { - private final NacosConfigProperties nacosConfigProperties; - - private final List dataIds; - private final ConfigService configService; - public NacosConfigHealthIndicator(NacosConfigProperties nacosConfigProperties, - ConfigService configService) { - this.nacosConfigProperties = nacosConfigProperties; + public NacosConfigHealthIndicator(ConfigService configService) { this.configService = configService; - - this.dataIds = new ArrayList<>(); - for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository - .getAll()) { - this.dataIds.add(nacosPropertySource.getDataId()); - } } @Override protected void doHealthCheck(Health.Builder builder) throws Exception { - for (String dataId : dataIds) { - try { - String config = configService.getConfig(dataId, - nacosConfigProperties.getGroup(), - nacosConfigProperties.getTimeout()); - if (StringUtils.isEmpty(config)) { - builder.down().withDetail(String.format("dataId: '%s', group: '%s'", - dataId, nacosConfigProperties.getGroup()), "config is empty"); - } - } - catch (Exception e) { - builder.down().withDetail(String.format("dataId: '%s', group: '%s'", - dataId, nacosConfigProperties.getGroup()), e.getMessage()); - } - } - builder.up().withDetail("dataIds", dataIds); + builder.up(); + + String status = configService.getServerStatus(); + builder.status(status); } } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java index 78b5c13a9..bdde9d067 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/com/alibaba/cloud/nacos/endpoint/NacosConfigEndpointTests.java @@ -19,13 +19,9 @@ package com.alibaba.cloud.nacos.endpoint; import static org.junit.Assert.assertEquals; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE; -import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; import java.util.Map; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; @@ -68,17 +64,13 @@ public class NacosConfigEndpointTests { Method method = PowerMockito.method(NacosConfigService.class, "getConfig", String.class, String.class, long.class); - MethodProxy.proxy(method, new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) - throws Throwable { - - if ("test-name.properties".equals(args[0]) - && "DEFAULT_GROUP".equals(args[1])) { - return "user.name=hello\nuser.age=12"; - } - return ""; + MethodProxy.proxy(method, (proxy, method1, args) -> { + + if ("test-name.properties".equals(args[0]) + && "DEFAULT_GROUP".equals(args[1])) { + return "user.name=hello\nuser.age=12"; } + return ""; }); } @@ -107,18 +99,16 @@ public class NacosConfigEndpointTests { Builder builder = new Builder(); NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator( - properties, properties.configServiceInstance()); + properties.configServiceInstance()); healthIndicator.doHealthCheck(builder); Builder builder1 = new Builder(); - List dataIds = new ArrayList<>(); - dataIds.add("test-name.properties"); - builder1.up().withDetail("dataIds", dataIds); + builder1.up(); - Assert.assertTrue(builder.build().equals(builder1.build())); + assertEquals(builder1.build(), builder.build()); } - catch (Exception ignoreE) { + catch (Exception ignore) { }