Reputation: 1124
I have my Spring cloud config server deployed in PCF servers (My application is binded with config server). I have a requirement to move all the common configurations to a seperate .yml (import.yml) file and import it in main config file. I am able to see all the fields (both main and imported) from config server when I tried to test the config server endpoint from postman. But in Controller I am able to see only the fields from main config file. I am getting null if I try to fetch fields from import.yml file. Why it is working in postman but not on my Controller class?
application.yml
my:
lastname: "Ramanathan"
spring:
config:
import: import.yml
import.yml
my:
firstname: "Thiagarajan"
POSTMAN
my:
lastname: Ramanathan
firstname: Thiagarajan
MyController.java
@Value("${my.fistname:firstname not available}")
private String firstname;
@Value("${my.lastname:lastname not available}")
private String lastname;
public String somemethod()
{
log.error("firstname:" +firstname);
log.error("lastname:" +lastname);
log.error("env firstname:" +environment.getProperty("my.fistname"));
log.error("env lastname:" +environment.getProperty("my.lastname"));
}
Output:
firstname: firstname not available
lastname: Ramanathan
env firstname: null
env lastname: Ramanathan
Upvotes: 0
Views: 25
Reputation: 11
You can do it for example like this.
First create your Config Server configurations in application.yml. For instance:
spring:
application:
name: config-server
profiles:
active: native
server:
port: 8888
---
spring:
config:
activate:
on-cloud-platform: none
cloud:
config:
server:
native:
search-locations: classpath:/configurations-local
---
spring:
config:
activate:
on-cloud-platform: cloudfoundry
cloud:
config:
server:
native:
search-locations: classpath:/configurations-cloudfoundry
This is if you also prefere to test your server locally. Also depending on your requirements you need to specify where configurations are stored, native or some external location.
Next, in this case, create both folders configurations-local and configurations-cloudfoundry. Now in them create again application.yml for all common configurations. For example:
management:
endpoints:
web:
exposure:
include: health,info,metrics
endpoint:
health:
probes:
enabled: true
And then create other yml files based on service names with non common configurations, for example myservice.yml The name of yml depends on application name and spring will automatically bind application.yml and myservice.yml to your service.
In your service you need to add something like this in it's application.yml.
spring:
application:
name: myservice
---
spring:
config:
activate:
on-cloud-platform: none
import: optional:configserver:http://localhost:8888
---
spring:
config:
activate:
on-cloud-platform: cloudfoundry
import: optional:configserver:http://config-server:8888
So name must match non common yml in config server and you need to specify import to your config server.
Hope this helps.
Upvotes: 0