Reputation: 357
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
Upvotes: 5
Views: 9329
Reputation: 1625
1-if you using maven, add this dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2-if you using gradle, add this dependency:
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-config'
}
3- refrech
Upvotes: 0
Reputation: 5495
In my case, I add the spring-cloud version in the properties section.
1 : Add spring cloud version:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
2 : dependencyManagement
below of the <dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Upvotes: 0
Reputation: 1945
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>
Upvotes: 7