mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #865 from echooymxq/master
Check nacos config health use configService
This commit is contained in:
commit
a66c1b4b27
@ -51,7 +51,6 @@ public class NacosConfigEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
|
||||
return new NacosConfigHealthIndicator(nacosConfigProperties,
|
||||
nacosConfigProperties.configServiceInstance());
|
||||
return new NacosConfigHealthIndicator(nacosConfigProperties.configServiceInstance());
|
||||
}
|
||||
}
|
||||
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
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<String> 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) {
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user