Global variables And Application Variables Defining in Spring boot project

。_饼干妹妹 提交于 2019-12-18 18:04:29

问题


I am trying to develop micro service by using spring and spring boot. In my project , I am converting monolithic to service oriented architecture. Project contain 20 Micro services.I these I need to set application variables and global variables. I have confusions related to this , And I am adding those confusions here,

  1. Is possible to declare my global variables in application.properties file? If not possible where I can define my global variables?
  2. If I am using spring config server for global configuration, How I can import those properties conditionally to client project?
  3. Can I set different property files for different profiles in config server , and conditionally import into client project for different profiles? Here each profile representing different regions in my case.

回答1:


After My Exploration I find out Solution for this problem for loading global variables and application variables including database configuration. The best way we can use that is - spring cloud config server externalized configuration.

We can create a microservice for spring cloud config server. In config server we can create our variables and configuration in two ways.

  1. Configurations from GIT Link reference
  2. Using local file system / Environment variables.

Links To refer

  1. https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html
  2. https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.3.RELEASE/multi/multi__spring_cloud_config_server.html
  3. https://github.com/spring-cloud/spring-cloud-config

Here I followed using local file system.

Need to create Config folder under src/main/resources. And create different profiles by following naming convention,

db,properties , db-test.properties , db-prod.properties , db-dev.properties. I created for example for different development environment. Like we can create any profiles for variables and configuration.

And add following in application.properties for config server

server.port=8888
spring.profiles.active=native

Add config server dependency in pom.xml file of config server,

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

Add the following into main application run class,

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

And also create client microservice project by adding pom.xml dependency,

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

Add following line in application.properties file for setting client to receive configuration from server,

server.port=8080
spring.application.name=db
spring.cloud.config.uri=localhost:8888

Finally run your client project by specifying profile ,

java -jar -Dsping.profiles.active=<profile> <jar_name>.jar

Thanks In advance



来源:https://stackoverflow.com/questions/49490466/global-variables-and-application-variables-defining-in-spring-boot-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!