spring cloud 启动的时候 如果使用配置中心,会先请求配置中心配置文件,如果请求配置中心失败再使用本地配置文件初始化。
在这个过程中,很难分辨到底是哪个配置文件起作用
可以增加一个检查类,在项目启动之后检查配置中心是否起作用
@Configuration
@Conditional(CloudConfigurationCheck.InnerCondition.class)
public class CloudConfigurationCheck implements InitializingBean {
@Autowired(required = false)
ConfigServerInstanceProvider provider;
@Value("${spring.cloud.config.discovery.service-id:configserver}")
String serviceName;
@Override
public void afterPropertiesSet() throws Exception {
if (provider == null) {
throw new BeanCreationException("config server error --spring.native-boot.enabled=true 作为启动参数");
}
try {
provider.getConfigServerInstance(serviceName);
} catch (IllegalStateException e) {
throw new BeanCreationException(" config server error,强制脱离配置中心请使用 --spring.native-boot.enabled=true 作为启动参数",e);
}
}
public static class InnerCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return !context.getEnvironment().containsProperty("spring.native-boot.enabled")||context.getEnvironment().getProperty("spring.native-boot.enabled","false").equals("false");
}
}
}