简体   繁体   English

如何在微服务中访问一个application.properties

[英]How to access one application.properties in microservices

how is the file application.properties handled in microservices? 微服务中的application.properties文件如何处理? Is there a way to have one global application.properties file that all microservices can access? 是否有一种方法可以使所有微服务都可以访问一个全局application.properties文件?

Application.properties or Application.yaml used for loading the configuration on startup and inject the property value in variable. 用于在启动时加载配置的Application.properties或Application.yaml,并将属性值注入变量中。 Like keyStorePath variable will be injected with value defined in properties file. keyStorePath变量一样,将注入属性文件中定义的值。

Java Code Java代码

@Value("${java.keystore.path}")
    private String keyStorePath; 

application.properties application.properties

java:
 keystore:
  path: /KeyStore.jks

Yes, You can use centralized configurations that can be used by all microservice, Create Config Server annoted with @EnableConfigServer that will hold configurations for all microservice(instead of local application.properties all microservice on start will come to config server for configuration just we need to provide config server url so that microservices can communicate with config server on startup and have required data). 是的,您可以使用所有微服务都可以使用的集中式配置,创建带有@EnableConfigServer注释的配置服务器,该配置服务器将保存所有微服务的配置(而不是本地application.properties,所有启动时的微服务都将进入配置服务器进行配置,只需要我们需要)提供配置服务器网址,以便微服务可以在启动时与配置服务器通信并具有所需的数据)。

Config Server Main Class Config Server主类

@SpringBootApplication
@EnableConfigServer
@ComponentScan(basePackages = {"com.abc.*"})
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

Property file in config Server 配置服务器中的属性文件

 java:
     keystore:
      path: /KeyStore.jks

Property File in Microservice that will hold the url of Config Server 微服务中的属性文件 ,将保存配置服务器的URL

# MicroServices Properties
spring:
  application:
     name: Microservice1
  profiles:
    active: rds
  cloud:
    config:
      uri: http://localhost:8888
      fail-fast: true
      password: test@123
      username: user

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM