I am very new to the concept of Spring Cloud and Spring external configurations, in fact started yesterday itself.
I have created one Config Server picking the configurations from a local Git repository, one micro service which is also the config client and one Eureka driven service discovery server.
Below is the code that i have mostly borrowed from various resources across internet -
Config Server - application.yml:
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: file:///${user.home}/config-repo
Config Server - Main class (bootstrap)
@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigServerApplication.class, args);
}
}
config-repo is the local git repo on my machine and has got a .yml file with name of the config client application i.e. authmanager.yml
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
healthcheck:
enabled: true
lease:
duration: 5
spring:
application:
data:
mongodb:
host: localhost
port: 27017
database: edc_mc
logging:
level:
org.exampledriven.eureka.customer.shared.CustomerServiceFeignClient: FULL
Now after running the config server, below is the output of the end point http://localhost:8888/authmanager/default -
{"name":"authmanager","profiles":["default"],"label":"master","version":"0ca6ca7b4502b9bb6ce1bf8efeb25516682cf79a","propertySources":[{"name":"file:///C:\\Users\\username/config-repo/authmanager.yml","source":{"eureka.client.serviceUrl.defaultZone":"http://127.0.0.1:8761/eureka/","eureka.client.healthcheck.enabled":true,"eureka.client.lease.duration":5,"spring.application.data.mongodb.host":"localhost","spring.application.data.mongodb.port":27017,"spring.application.data.mongodb.database":"db_name","logging.level.org.exampledriven.eureka.customer.shared.CustomerServiceFeignClient":"FULL"}}]}
Micro service + Config client code -
bootstrap.yml -
server:
port: 9097
spring:
application:
name: authmanager
cloud:
config:
uri: http://localhost:8888
Client - Main class (bootstrap) -
@SpringBootApplication
@EnableDiscoveryClient
@EnableWebMvc
public class CloudLoginManagerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudLoginManagerApplication.class, args);
}
}
Controller class in the config client, where i want to use the configuration file properties -
@RefreshScope
@RestController
@RequestMapping("/auth")
public class MyController {
@Value("${spring.application.data.mongodb.database}")
String env_var;
Skipping rest of the code for clarity sake.
This is the error i am getting -
Could not resolve placeholder 'spring.application.data.mongodb.database' in string value "${spring.application.data.mongodb.database}"
Other properties like server.port is not giving issues.
I have also tried the Environment interface way, but thats returning null as well.
Any pointers please, i have almost reached dead end now.
Thanks,
AJ
To enable cloud config you have to add the spring-cloud-starter-config
to your dependencies. You can verify by checking the /env (might need to add actuator) to see which properties are available.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With