Reputation: 315
How to specify a spring boot application to access only a specific .properties
file among other files in my Spring Cloud Config Server.
My Spring Cloud Config has the following files:
application.properties
,order-dev.properties
,inventory-dev.properties
,
all my db and messaging properties are in order-dev and inventory-dev files.
Now I wish to move those properties to orderdb-dev
, inventorydb-dev
, ordermsg-dev
and inventorymsg-dev
files.
How do I configure my order
and inventory
service to pick the property from orderdb-dev
, inventorydb-dev
, ordermsg-dev
and inventorymsg-dev
files ? I have been going around to find the property for the same. Read through the official documentation and felt lost. Any help would be appreciated.
Upvotes: 4
Views: 3460
Reputation: 2160
Suppose your config with custom file name is myboot.properties
.
According to documentation to externalizable configs there are many ways to start the application, with command line parameter --spring.config.name
:
$ java -jar myproject.jar --spring.config.name=myboot
In command line we specify myboot
but the Spring Boot will search for myboot.properties
. Many property files can be added comma separated like this:
$ java -jar myproject.jar --spring.config.name=myboot,myboot2,myboot3
Now common properties from myboot.properties
will be overridden by myboot2.properties
then overridden by myboot3.properties
.
Another option, full config names in --spring.config.location
:
$ java -jar myproject.jar --spring.config.location=classpath:/myboot.properties
Now filename must contain the file extension. Arbitrary extensions can not be used, because extensions unknown to Spring Boot will generate errors. --spring.config.location
also supports multiple comma separated config names.
Alternatively exactly the same can be achieved in the source code, without changing command line:
@SpringBootApplication
public class MyBoot {
public static void main(String[] args) {
SpringApplication.run(
new Class<?>[] {MyBoot.class, MyController.class},
new String[]{"--spring.config.name=myboot"});
@SpringBootApplication
public class MyBoot {
public static void main(String[] args) {
SpringApplication.run(
new Class<?>[] {MyBoot.class, MyController.class},
new String[]{"--spring.config.location=classpath:/myboot.properties"});
Or you might want to avoid getting rid of existing command line arguments:
@SpringBootApplication
public class MyBoot {
public static void main(String[] args) {
String[] newArgs = Stream.concat (
Stream.of (args),
Stream.of ("--spring.config.location=classpath:/myboot.properties"))
.toArray(String[]::new);
SpringApplication.run(
new Class<?>[] {MyBoot.class, MyController.class},
newArgs);
So, the above code is just an idea, which might not flexible enough, make as flexible as required.
Upvotes: 0
Reputation: 2671
Add a bootstrap.yml file under resources folder. Then add the below properties.
spring:
application:
name: order
cloud:
config:
name: ${spring.application.name}, orderdb, ordermsg
profile: dev
This way, it will first load the properties from order-dev.properties file. Then orderdb-dev.properties and then ordermsg-dev.properties.
Upvotes: 15