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
|
@Bean
|
||||||
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
|
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
|
||||||
return new NacosConfigHealthIndicator(nacosConfigProperties,
|
return new NacosConfigHealthIndicator(nacosConfigProperties.configServiceInstance());
|
||||||
nacosConfigProperties.configServiceInstance());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,16 +16,9 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.nacos.endpoint;
|
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.AbstractHealthIndicator;
|
||||||
import org.springframework.boot.actuate.health.Health;
|
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;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,41 +26,17 @@ import com.alibaba.nacos.api.config.ConfigService;
|
|||||||
*/
|
*/
|
||||||
public class NacosConfigHealthIndicator extends AbstractHealthIndicator {
|
public class NacosConfigHealthIndicator extends AbstractHealthIndicator {
|
||||||
|
|
||||||
private final NacosConfigProperties nacosConfigProperties;
|
|
||||||
|
|
||||||
private final List<String> dataIds;
|
|
||||||
|
|
||||||
private final ConfigService configService;
|
private final ConfigService configService;
|
||||||
|
|
||||||
public NacosConfigHealthIndicator(NacosConfigProperties nacosConfigProperties,
|
public NacosConfigHealthIndicator(ConfigService configService) {
|
||||||
ConfigService configService) {
|
|
||||||
this.nacosConfigProperties = nacosConfigProperties;
|
|
||||||
this.configService = configService;
|
this.configService = configService;
|
||||||
|
|
||||||
this.dataIds = new ArrayList<>();
|
|
||||||
for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository
|
|
||||||
.getAll()) {
|
|
||||||
this.dataIds.add(nacosPropertySource.getDataId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||||
for (String dataId : dataIds) {
|
builder.up();
|
||||||
try {
|
|
||||||
String config = configService.getConfig(dataId,
|
String status = configService.getServerStatus();
|
||||||
nacosConfigProperties.getGroup(),
|
builder.status(status);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,13 +19,9 @@ package com.alibaba.cloud.nacos.endpoint;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.powermock.api.mockito.PowerMockito;
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
@ -68,17 +64,13 @@ public class NacosConfigEndpointTests {
|
|||||||
|
|
||||||
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
Method method = PowerMockito.method(NacosConfigService.class, "getConfig",
|
||||||
String.class, String.class, long.class);
|
String.class, String.class, long.class);
|
||||||
MethodProxy.proxy(method, new InvocationHandler() {
|
MethodProxy.proxy(method, (proxy, method1, args) -> {
|
||||||
@Override
|
|
||||||
public Object invoke(Object proxy, Method method, Object[] args)
|
|
||||||
throws Throwable {
|
|
||||||
|
|
||||||
if ("test-name.properties".equals(args[0])
|
if ("test-name.properties".equals(args[0])
|
||||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||||
return "user.name=hello\nuser.age=12";
|
return "user.name=hello\nuser.age=12";
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -107,18 +99,16 @@ public class NacosConfigEndpointTests {
|
|||||||
Builder builder = new Builder();
|
Builder builder = new Builder();
|
||||||
|
|
||||||
NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator(
|
NacosConfigHealthIndicator healthIndicator = new NacosConfigHealthIndicator(
|
||||||
properties, properties.configServiceInstance());
|
properties.configServiceInstance());
|
||||||
healthIndicator.doHealthCheck(builder);
|
healthIndicator.doHealthCheck(builder);
|
||||||
|
|
||||||
Builder builder1 = new Builder();
|
Builder builder1 = new Builder();
|
||||||
List<String> dataIds = new ArrayList<>();
|
builder1.up();
|
||||||
dataIds.add("test-name.properties");
|
|
||||||
builder1.up().withDetail("dataIds", dataIds);
|
|
||||||
|
|
||||||
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