spring cloud 检查配置中心

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");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值