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,
- Is possible to declare my global variables in application.properties file? If not possible where I can define my global variables?
- If I am using spring config server for global configuration, How I can import those properties conditionally to client project?
- 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.
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.
- Configurations from GIT Link reference
- Using local file system / Environment variables.
Links To refer
- https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html
- https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.3.RELEASE/multi/multi__spring_cloud_config_server.html
- 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