I have an simple spring boot application.
Client's application.yaml file:
spring:
config:
import: optional:configserver:http://localhost:1717
application.name: config-client
profiles:
active: profile1
---
spring:
config:
activate:
on-profile: profile1
option:
local: profile1-local
And application gets properties from config server.
Config Server provides next config-client.yaml file:
spring:
application.name: config-client
---
spring:
config:
activate:
on-profile: profile1
option:
local: profile1-remote
remote: profile1-remote
But when I trying use:
#Value("${option.local}")
String local;
I gets profile1-local (value from application.yaml) value instead of profile1-remote (value by server config).
But if I use ${option.remote} value gets from config server.
The problem reproduces if I want override profile properties only, properties outside the profile are overridden!
Why Spring Boot not override profile local property by property from config server?
Maybe problem caused by Spring changed config file processing in Spring Boot 2.4, but option spring.config.use-legacy-processing: true not helps me!
I has confused! Thanks you very much!
Related
My Spring Boot version is 2.3.4-RELEASE. I configure DataSource this way in application-dev.yml:
spring:
profiles: dev
datasource:
master:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
slave:
driver-class-name: com.mysql.jdbc.Driver
url: myUrl
When I activate the 'dev' profile, I can start my Spring Boot container successfully, but when I restart it, I get this error:
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
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently
active).
I tried to add the config: spring.datasource.url. It works, but I need to distinguish master and slave. How can I do that?
You must activate your profile using property spring.profiles.active
If you have database settings to be loaded from a particular profile
you may need to activate it (no profiles are currently active).
Read set the active Spring profiles to see how to do it
I’m migrating an application from Spring Boot 2.3 to Spring Boot 2.4, I have some problems with profiles order loading.
In my application, I have some profiles :
default (application-default.yml) : default values for the application. There is a lot of default values, the goal is not to complicate the file application.yml, so it's very important to have this file
quality (application-quality.yml) : quality profile
environment profile (for example application-Dev.yml) : profile of the current environment. It must override application.yml and application-default.yml values
database profile for the environment (for example application-databaseDev.yml) : profile for the database connection for the current environment
In Spring Boot 2.3, configuration is :
application.yml
spring:
profiles:
include:
- default
redis:
jedis:
pool:
max-active: 4
application-default.yml
spring:
profiles:
include:
- quality
myApplication:
vcs:
url: https://default.url
cache:
enabled: true
application-Dev.yml
spring:
profiles:
include:
- databaseDev
myApplication:
cache:
enabled: false
I'm starting application with parameter --spring.profiles.active=Dev
In this case, here is the order of the active profile The following profiles are active: default,quality,Dev,databaseDev
This is order I want. For exemple value for key myApplication.cache.enabled is false
I read the documentation to migrate to Spring Boot 2.4, so my configuration is now :
application.yml
spring:
profiles:
group:
"Dev": "default, quality, databaseDev"
redis:
jedis:
pool:
max-active: 4
application-default.yml
myApplication:
vcs:
url: https://default.url
cache:
enabled: true
application-Dev.yml
myApplication:
cache:
enabled: false
But now I have the active profile The following profiles are active: Dev,default,quality,databaseDev
In this case the value for key myApplication.cache.enabled is true
Now profile Dev doesn't override default profile as 2.3 but inversely default profile override Dev profile.
Is there a way to modify the configuration to work as 2.3 (without using use-legacy-processing: true)? Did I make a mistake somewhere?
I found a solution that works :
Rename the profile Dev to dev and create a group named Dev
Here is the result :
application.yml
spring:
config:
import: classpath:application-groups.yml
redis:
jedis:
pool:
max-active: 4
application-groups.yml
spring:
profiles:
group:
"base": "default, quality"
"Dev": "base, databaseDev"
application-default.yml
myApplication:
vcs:
url: https://default.url
cache:
enabled: true
application-dev.yml
myApplication:
cache:
enabled: false
Starting with parameter --spring.profiles.active=Dev
The following profiles are active: Dev,base,default,quality,databaseDev
What do you think about this solution ?
I have these KVs sources store on Consul:
config/books/<key>
config/common/<key>
And in my spring boot app application.yml, I have config it as following:
spring:
application:
name: sampleapp
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
prefix: config
defaultContext: books
I know that this configuration that I made has pointed the app to read from config/books .
But I can't figure out how can I retrieve the Consul KV config from both config/common and config/books.
It's not really flexible there, but it can be done if you rename your project and use defaultContext as well. Based on the documentation Spring Cloud Consul Config takes next paths:
config/testApp,dev/
config/testApp/
config/application,dev/
config/application/
where testApp - name of your app, application - default context
So with the following configuration it will work
spring:
application:
name: books
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
prefix: config
defaultContext: common
Logs after running this configuration:
Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/books/'}, ConsulPropertySource {name='config/common/'}]}
I just ran jhipster registry and its working fine. It's looking for config files from central-config folder. I want to refactor my config files under folders in the central-config folder itself. That's something that I can achieve running Spring Cloud Config server with a configuration like this:
spring:
cloud:
config:
server:
git:
default-label: develop
uri: file://${user.home}/config-repo
search-paths: employee-service, enterprise-service
How can I achieve such behavior with 'composite thing' in jhipster-registry. For info, this is the bootstrap.yml file from jhipster registry:
# ===================================================================
# Spring Cloud Config bootstrap configuration for the "dev" profile
# In prod profile, properties will be overwriten by the ones defined in bootstrap-prod.yml
# ===================================================================
spring:
application:
name: jhipster-registry
profiles:
active: dev
include: composite
cloud:
config:
server:
bootstrap: true
composite:
- type: native
search-locations: file:./central-config
#search-locations: file://${user.home}/Acensi/isupplier/config-repo
prefix: /config
fail-fast: true
# name of the config server's property source (file.yml) that we want to use
name: jhipster-registry
profile: dev # profile(s) of the property source
label: master # toggle to switch to a different version of the configuration as stored in git
# it can be set to any label, branch or commit of the config source git repository
info:
project:
version: #project.version#
# uncomment to enable encryption features
#encrypt:
# key: my-secret-encryption-key-to-change-in-production
I had the same issue. I added to my application.yml file the following:
spring:
cloud:
config:
server:
bootstrap: true
composite:
- type: native
search-locations: file:./central-config/myFolder
You were very close, hope it helps.
I was upgrading an existing older JHipster Registry to 5.0.1. I was also using the git repo. The problem with the above configurations is that the property is not called search-locations for the git repo it is search-paths. In order to keep everything working as before which was had the search paths specified via an environment variable SPRING_CLOUD_CONFIG_SERVER_GIT_SEARCH-PATHS, I use the following configuration in my prod bootstrap.
bootstrap-prod.yml:
spring:
cloud:
config:
server:
bootstrap: true
composite:
- type: git
uri: git#bitbucket.org:whatever/repo
search-paths: ${spring.cloud.config.server.git.search-paths}
ignore-local-ssh-settings: true
private-key: |
-----BEGIN RSA PRIVATE KEY-----
I am working on a Spring Boot Registration Server(Eureka Server).
Currently it is working with below configuration.
Project Name: registration-service
Inside main method: System.setProperty("spring.config.name", "registration-service");
"yml file":
file name: registration-service
content:
eureka:
instance:
hostname: eureka-server
server:
enableSelfPreservation: false
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
port: 2323 # HTTP (Tomcat) port
spring:
application:
name: eureka-server
With above configuration,application start running on 2323.
But if i change spring.config.name,it does not work,start giving connection refused exception.
why it is happening? even though this spring.config.name is no where used in yml file. Should it be necessarily same as project name? or it is specific to #EnableEurekaServer enabled spring boot application.
And in yml we have to write
spring:
application:
name: eureka-server
though in other spring boot application we give name of current project(here it should be registration-service).why we have to write here eureka-server? I know,I am missing something(or a lot of things).please help me in understanding the missing part.
Spring boot by default looks for file application.yml. If u have different profiles in your application it can also look for application-{profilename}.yml. This is the default convention followed.
spring.config.name property is used to override this default behaviour. When u override this property with register-service then spring boot looks for a file register-service.yml and loads config from that.
So your eureka server url which is given in the register-service.yml file may not be available in the default application.yml file. Hence when u change the value Spring boot may not be avle to find the Eureka server url.
Keep the names unchanged, as much as possible. If u have config file as register-service.yml then keep the spring.config.name=register-service. If you change this value then u need to create the new file with config.name value and then add eureka configuration to that again.