首页 > 其他分享 >Springcloud不能识别bootstrap配置文件

Springcloud不能识别bootstrap配置文件

时间:2022-12-09 10:58:17浏览次数:47  
标签:配置文件 Springcloud bootstrap class static spring public cloud

方法一:引用spring-cloud-starter-bootstrap包(推荐)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

方法二:设置spring.cloud.bootstrap.enabled=true

1、在main方法设置

@EnableDiscoveryClient
@SpringBootApplication
public class application {

   public static void main(String[] args) {
       args = Arrays.copyOf(args, args.length + 1);
       args[args.length - 1] = "--spring.cloud.bootstrap.enabled=true";
       SpringApplication.run(application .class, args);
   }
}

2、使用ApplicationListener添加

@Component
public class StartOnApplication implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment=event.getEnvironment();
        Properties properties=new Properties();
        properties.setProperty("spring.cloud.bootstrap.enabled","true");
        environment.getPropertySources().addLast(new PropertiesPropertySource("application1",properties));
    }
}

3、运行时配置,idea如下

原因分析:

  1. BootstrapApplicationListener.class
  2. PropertyUtils
public abstract class PropertyUtils {

	/**
	 * Property name for checking if bootstrap is enabled.
	 */
	public static final String BOOTSTRAP_ENABLED_PROPERTY = "spring.cloud.bootstrap.enabled";

	/**
	 * Property name for spring boot legacy processing.
	 */
	public static final String USE_LEGACY_PROCESSING_PROPERTY = "spring.config.use-legacy-processing";

	/**
	 * Property name for bootstrap marker class name.
	 */
	public static final String MARKER_CLASS = "org.springframework.cloud.bootstrap.marker.Marker";

	/**
	 * Boolean if bootstrap marker class exists.
	 */
	public static final boolean MARKER_CLASS_EXISTS = ClassUtils.isPresent(MARKER_CLASS, null);

	private PropertyUtils() {
		throw new UnsupportedOperationException("unable to instatiate utils class");
	}
        /**
        *判断"spring.cloud.bootstrap.enabled"指定值是否获取到,没有取默认值false;或者判断`Marker`类是否存在。
        *`Marker`类存在`spring-cloud-starter-bootstrap.x.x.jar`里面
        */  
	public static boolean bootstrapEnabled(Environment environment) {
		return environment.getProperty(BOOTSTRAP_ENABLED_PROPERTY, Boolean.class, false) || MARKER_CLASS_EXISTS;
	}
        /**
        *该值默认为false,"spring-boot-X.x,jar"包中的additional-spring-configuration-metadata.json文件中
        */
	public static boolean useLegacyProcessing(Environment environment) {
		return environment.getProperty(USE_LEGACY_PROCESSING_PROPERTY, Boolean.class, false);
	}

}

总结:BootstrapApplicationListener类会判断是否加载名为bootstrap的配置文件,PropertyUtils类会判断spring.cloud.bootstrap.enabled的值是否为true,或者Marker类是否存在。满足条件则会加载bootstrap的配置文件。

标签:配置文件,Springcloud,bootstrap,class,static,spring,public,cloud
From: https://www.cnblogs.com/yl97/p/16968212.html

相关文章

  • 【SpringCloud Alibaba 最新版 全新微服务框架搭建(预告片~~)】
    【SpringCloudAlibaba 最新版 全新微服务框架搭建(预告片~~)】 https://www.bilibili.com/video/BV1hP411M74a/?share_source=copy_web&vd_source=d9abe505a6abb4a85......
  • 前端之jQuery和bootstrap等文件下载
    前端之jQuery和bootstrap等文件下载jQuery版本目前jQuery的版本建议选择3.x的版本,因为在目前主流的浏览器它都兼容(IE已经下葬了,勿念)而jQuery3.x版本是官方现在还在维护......
  • jQuery常见操作及Bootstrap
    昨日内容回顾BOM操作浏览器模型操作,通过window关键字对浏览器窗口进行操作。新建窗口、关闭窗口、查看浏览器版本、查看当前页网址、三种弹出框、设置定时任务、设置......
  • springcloud Config 分布式配置中心
     Server端:提供配置⽂件的存储、以接⼝的形式将配置⽂件的内容提供出去,通过使⽤@EnableConfigServer注解在Springboot应⽤中⾮常简单的嵌⼊Client端:通过接⼝获取配置......
  • springcloud Stream消息驱动
    SpringCloudStream是⼀个构建消息驱动微服务的框架。应⽤程序通过inputs(相当于消息消费者consumer)或者outputs(相当于消息⽣产者producer)来与SpringCloudStream中的bin......
  • 读取配置文件.ini
    1、config.ini文件 [log]name=py30level=INFO[mysql]username=pythonpasswd=1234562、读取ini配置文件数据(conf.py)"""#格式[section]--区域option=value--属性"""......
  • springcloud GateWay网关
    GateWay如何工作客户端向SpringCloudGateWay发出请求,然后在GateWayHandlerMapping中找到与请求相匹配的路由,将其发送到GateWayWebHandler;Handler再通过指定的过滤器......
  • springcloud Eureka服务注册中心
    1.注册中心实现原理分布式微服务架构中,服务注册中心用于存储服务提供者地址信息、服务发布相关的属性信息,消费者通过主动查询和被动通知的方式获取服务提供者的地址信息,......
  • springcloud Ribbon负载均衡
    关于负载均衡负载均衡一般分为服务器端负载均衡和客户端负载均衡所谓服务器端负载均衡,比如Nginx、F5这些,请求到达服务器之后由这些负载均衡器根据一定的算法将请求路由到......
  • springcloud Hystrix熔断器
    服务熔断熔断机制是应对雪崩效应的一种微服务链路保护机制。我们在各种场景下都会接触到熔断这两个字。高压电路中,如果某个地方的电压过高,熔断器就会熔断,对电路进行保护。......