Profile based Spring Cloud Config Server client with optional local developer properties - spring

My Goal:
Having the Spring Cloud Config Server import active and provide a way for developers to have an optional property file on their machine.
My Config Server is up and running using org.springframework.cloud:spring-cloud-config-server:3.1.3 and #EnableConfigServer on the main class. Http requests to the concrete endpoint yield the expected result. This server should provide important environment configurations for a developer for his/her local setup.
$ curl http://localhost:8888/test-application/dev
{"name":"test-application","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/cfg/test-application/application-dev.yml","source":{"server.port":1111}},{"name":"classpath:/cfg/test-application/application.yml","source":{"server.port":8000}}]}
Where localhost:8888 is my Config Server and test-application ist the name of the client application (defined via spring.application.name). The provided dev is the currently active profile on the client (The dev profile indicates a locally running software, there is no dev environment).
Clients configuration:
application.yml
spring:
application:
name: test-application
config:
import:
- configserver:http://localhost:8888 # <- pull the configuration from the configserver
- optional:file:/absolute/path/to/the/project/root/ # <- if there are any additional configuration files, use them
The client uses the following dependencies:
org.springframework.boot:spring-boot-starter-web:2.7.0
org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.3
org.springframework.cloud:spring-cloud-starter:3.1.3
org.springframework.cloud:spring-cloud-starter-config:3.1.3
As shown above, the "base" application.yml configured server.port=8000 where the profile specific is set to server.port=1111. When reading the documentation this behaviour is correct. But my local developer configuration contains server.port=2222. This was ignored.
Here comes the problem:
When starting the client application, i can see the following log statements:
Fetching config from server at : http://localhost:8888
Located environment: name=test-application, profiles=[default], label=null, version=null, state=null
Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-classpath:/cfg/test-application/application-dev.yml'}, BootstrapPropertySource {name='bootstrapProperties-classpath:/cfg/test-application/application.yml'}]
The following 1 profile is active: "dev"
Tomcat initialized with port(s): 1111 (http)
Initializing Spring embedded WebApplicationContext
Tomcat started on port(s): 1111 (http) with context path ''
Started TestApplicationKt in 2.234 seconds (JVM running for 2.762)
The configuration evaluation result from spring was to choose port 1111 instead of the wanted 2222 within the application-dev.yml located in the project root (developer config).
Wrapped up:
Three config files:
config server application.yml (port: 8000)
config server application-dev.yml (port: 1111)
project root developer config application-dev.yml (port: 2222) <- I want this file to have precedence over the other two.
When running the debugger, i see these found property sources within the injected Environment bean. The wanted developer file is within this list and the property source content is correct (server.port=2222).
Does anyone have an idea how to solve this problem?
I have created a project that reproduces this exact behaviour. Link to GitHub.
Thanks in advance!

Is the dev profile active when application is running in dev environment or it's also active when running locally?
Ideally you should have 2 different profiles for dev and local and you can enable config server only when dev profile is active.
You can rename your local property file as application-local.yml and disable spring cloud config when profile local is active.
You can put below code in bootstrap.yml(create new file beside application.yml) and remove config server configurations from application.yml
---
spring:
application:
name: test-application
config:
activate:
on-profile: dev
import:
- configserver:http://localhost:8888 # <- pull the configuration from the configserver
- optional:file:/absolute/path/to/the/project/root/ # <- if there are any additional configuration files, use them
---
spring:
application:
name: test-application
config:
activate:
on-profile: local
cloud:
config:
enabled: false
EDIT: Actual fix
Thanks for sharing reproducible example.
All you need to do is remove local application-dev.yml location reference from client application.yml and add it to configServer application.yml as shown in belolow screenshots.
/client/src/main/resources/application.yml
And put it in configServer application.yml at the end of search-locations. Order matters so please make sure you put overriding location/s at the end.
/configServer/src/main/resources/application.yml
Build and start both the applications and you will see client application server started on port 2222

Related

How to build a Docker image for client service of Spring Cloud Config Server

I have built a Docker image for cloud config service. I have another service employee for which I want to build a Docker image. This client service will read data source configuration properties from the config server
I am able to connect the employee service successfully with config service on localhost without Docker. The problem is only that I am not able to build a Docker image for the employee service.
On running mvn package dockerfile:build command for the employee service I get the following error
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Below is the bootstrap.yml file located at src/main/resources directory
spring:
application:
name: employee
profiles:
active: dev
cloud:
config:
uri: http://configserver:8071
Config service's bootstrap.yml file
spring:
application:
name: config-server
profiles:
active: native,dev
cloud:
config:
server:
#Local configuration: This locations can either of classpath or locations in the filesystem.
native:
#Reads from a specific filesystem folder
search-locations: classpath:/config
server:
port: 8071
employee.properties file in config directory
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
employee-dev.properties file
spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
This is probably because the container of config server with hostname configserver is not up. I am not sure what hostname I should provide. Even if I want to build the image of employee service by starting the container of config server, it won't be on configserver host but on localhost. This is probably why the build is failing?
I want to build the Docker image of employee service so that its container can be fired up using the docker-compose up command
Edit: This question is similar but I already have the spring-cloud-starter-config dependency added in my client service and it still gives me the same error on build
Edit 2
You don't have to separately build a Docker image for the client service at all!
When I ran docker-compose up command I noticed the following line in output
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://configserver:8071
But I was still not sure if it was actually reading the data from the config server. So I purposely added incorrect database credentials in config server properties file for my service and the service startup failed! Now I am sure that my employee service is reading the data from the config server
I am still not sure how can I achieve creating a new Docker image for my employee service.
Did you try changing the URL param db to actual host of vhostname ?
db =change it to IP addess or accessable load balancer URL
spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false

Disabling config server for tests

I am using microservice architecture where the services are done with spring boot, to control them, I am using zuul as a proxy, eureka as register and config server as the configuration provider.
In the microservices I configure my bootstrap.yml file as follows:
spring:
application:
name: portal-gca-server-${MYENV:local}
cloud:
config:
uri: http://localhost:9090
As seen above, the config file name that I look for in the config server is according to my environment variable, otherwise I get the local profile.
This works fine, but when I run the tests on the development machine it will never work, because instead of searching for the test profile it searches for the location, because the dev machine has no environment variable.
I know I could register the environment variable to run the tests but that's not the intention or else I would have to do this all the time to run tests and to run the local application.
Is there any way to solve this? I already tried to use the annotations:
webEnvironment = SpringBootTest.WebEnvironment.MOCK
#ActiveProfiles("test")
#TestPropertySource(locations="classpath:application-test.properties")
None of them any good, because the first thing the application does when executed is to fetch the information in git according to bootstrap.yml.
Has anyone ever experienced this?
Go to your bootstrap.yml and then add :
spring:
cloud:
config:
enabled: false

How to create Spring Cloud Config Client with env specific configuration?

I have facing an issue with Spring Cloud Config Server and Eureka Server Profiling.
Let's say I have 3 services with their name ("spring.application.name") as :
myapp-svc
myapp-spring-cloud-config-svc
myapp-spring-eureka-svc
I want to deploy each service in 2 regions ( dev and prod ). In Dev region, each service will run on localhost and in prod it will have some different url. 'myapp-spring-cloud-config-svc' in dev region will point to local git repo, while in prod region it will point to remote git repo.I can have 2 configurations:
1) When I start 'myapp-svc' service in local, it should connect to 'myapp-spring-cloud-config-svc' in dev.
I can do this by setting spring.cloud.config.uri = .
But the issue with this set up is that the property needs to be defined in bootstrap.properties.
So, If deploy 'myapp-svc' to prod, I will have to change config uri there to point it to prod config service which in turn would need another build creation.
This doesn't seem like a good solution, what if I have 50 app related services, I can't change this property in each one of them before prod deployment.
I tried setting spring.cloud.config.uri in application-dev.properties of 'myapp-svc' but it doesn't work. As per docs, it must be changed in bootstrap.
So, how do I implement this without having to create new build for prod ?
2) I can first call eureka and then using eureka I can call config service here.
The problem here is also same.
If I use eureka to look up config then "eureka.client.serviceUrl.defaultZone" must be defined in "bootstrap.yml".
See this:https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html
So, in this case too, I need to change eureka url before deploying this service to prod.
Please help me on this...!!
Here is how, the properties, yml looks like for each of the above mentioned services:
1) myapp-svc:
1.1)bootstrap.yml
spring:
application:
name: myapp-svc
cloud:
config:
discovery:
enabled: true
serviceId: myapp-spring-cloud-config-svc
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
server:
port: 8082
2) myapp-spring-cloud-config-svc:
2.1)application-dev.properties:
spring.cloud.config.server.git.uri=file:///C:/config-repo
eureka.client.serviceUrl.defaultZone=http://localhost:8762/eureka
2.2)application-prod.properties:
spring.cloud.config.server.git.uri=https://github.com/<mygit Repo>
2.3)bootstrap.proerties:
spring.application.name=myapp-spring-cloud-config-svc
server.port=8888
3) myapp-spring-eureka-svc
3.1)bootstrap.proerties
spring.application.name=myapp-spring-eureka-svc
server.port=8762
1) You can have profile specific bootstrap-<profile>.properties (like for application-<profile>.properties) for each supported profile to avoid rebuilding your application for each env. Then just pass application profile using to your application during start-up. Spring will load correct bootstrap-<profile>.properties and will connect to proper configuration server (or eureka, etc). Example:
java -jar your-app.jar --spring.profiles.active=dev
2) You can pass your URLs externally as custom properties (same as with profile above) and have smth like this in bootstrap.properties. Example:
spring.cloud.config.uri=${config.server.url}
then pass --config.server.url= ... during start-up.
3) You can pass Spring properties in the same way during start-up. Example:
java -jar your-app.jar --spring.cloud.config.uri= ...
4) You can use system env variables. Example:
spring.cloud.config.uri=${SYSTEM_ENV_CLOUD_CONFIG_URI}

Shared Config(at git) between SPring Boot Services

I am developing spring boot services.
So lets say I have 3 Services A, B, C with dev and prod profiles each. Now I am keeping the configuration for these services at GIT and using config server to fetch the configuration.
So when I run A service in prod mode, the config file A-prod.properties is getting used.
Now I want to keep some common configuration which will be used by all 3 Services say common-prod.properties. How should I do that?
I have tried this:
Config Server :
spring:
cloud:
config:
server:
git:
uri: http://gitPaath/Configs.git
username: <username>
password: <pass>
cloneOnStart: true
searchPaths: "{common}"
My properties are file are in Git Repo in this order :
- A-prod.properties
- A-dev.properties
- B-prod.properties
- A-dev.properties
- C-prod.properties
- C-dev.properties
- common
- common-prod.propeties
- common-dev.properties
You can put all shared properties between services in application-<profile>.properties file.
In your case, make application-prod.properties and application-dev.properties files and put these file on root folder or any folder that is searchable by config server.
You can find more details in "Sharing Configuration With All Applications" section in here.

Spring cloud config in local dev mode

Is it possible to control if the application requires spring-config-server based on profile.
I want to pick properties from resource/... in say local-dev profile and use cloud-config only with a profile where there would be a config-server running.
You can disable using config-server for a specific profile. Please try to define the properties in bootstrap.yml like below. Please note, we should configure these settings only in bootstrap.yml. Setting these properties in application.yml will not work. In this case, local profile will be running only with local profiles in /resources folder. In dev profile, properties from config-server will override properties from resources/ folder.
spring:
profiles: local
cloud:
config:
enabled: false
---
spring:
profiles: dev
cloud:
config:
uri: http://<your config server>

Resources