Environment Specific Eureka properties is not loading - java

I have created a Eureka Client and Server project using spring Cloud 1.5.17 version. Both the project is working fine but the issue is that based on environment I want certain eureka properties to be loaded at run-time for the client project. For that what I have done is that I have created environment specific property file for eureka like as shown below.
I have tried all the below three properties in application.yml but none of them seems not picking the eureka-client-test.properties properties
spring:
profiles:
active: test
eureka:
instance:
environment: test
eureka:
instance:
environment:
active-profiles:
- test
Can anyone please help me on this
Full source code is committed and is available under the below bitbucket repository
https://bitbucket.org/resh32/eureka

Override this property eureka.client.props with your property file name without .properties suffix.
So lets say you specify your spring active env using spring.profiles.active & then in corresponding application-env.properties file you can set eureka.client.props to eureka-client-env.

Related

Spring Boot: Load common config application properties file using bootstrap.yml

In our config server we have following application properties files:
helloApp-dev.properties //dev env properties
helloApp-commonConfig.properties //common properties across env
The properties are accessible from URI like:
https://myapp.abc.com/1234/helloApp-dev.properties
https://myapp.abc.com/1234/helloApp-commonConfig.properties
Below is our bootstrap.yml of helloApp application:
---
spring:
application:
name: helloApp
---
spring
profiles: dev
cloud:
config:
label: 1234
name: {spring.application.name}
uri: https://myapp.abc.com/
I am using Spring boot version 2.2.4. The helloApp-dev.properties are loading successfully in application but not commonConfig.
The configuration in commonConfig is not being loaded because you are not indicating to Spring that it should load that profile/config, because you are activating only the dev profile - and the configuration for that profile is the one which is being loaded:
---
spring
profiles: dev
In addition to the good solutions proposed by #akozintsov in his/her answer, if you need to include certain common configuration in different environments or profiles, let's say dev, qa or prod, you can use the spring.profiles.include configuration property and include, as a comma separated list of values, if using properties files, or as a list, if using yaml, the different common profiles and corresponding configurations you need to.
In your example, in helloApp-dev.properties you need to include the following information within the rest of your configuration:
spring.profiles.include=commonConfig
These related SO question and this article could be of help as well.
To load properties file, profiles should match.
You have 2 solutions:
Rename helloApp-commonConfig.properties -> helloApp.properties
Use multiple profiles for your application (dev, commonConfig)

Why couldn't I use #Scheduled with an externalized Spring Cloud properties file?

When I was working on a Spring Boot application that used scheduling and acted as a config client, I was unable to pull the crontab expression from the application-cloud.yml file.
This:
#Scheduled(cron = "${cron.expression}")
did not work.
Putting the cron.expression in the jar's internal application.yml was not an option. As a workaround, I changed the source of that property to the appropriate environment variable, CRON_EXPRESSION.
Does anyone know why I had to do that? Why was Spring Scheduled unable to resolve an externalized configuration property?
Edit:
Here is the bootstrap.yml that the project was setup with:
spring:
profiles: cloud
application:
name: application-name
cloud:
config:
uri: https://config-server.com
fail-fast: false

How to resolve placeholder in properties file with values from another properties file in spring boot application

My spring boot application has below properties files.
src/main/resources/config/DEV/env.properties
mail.server=dev.mail.domain
src/main/resources/config/QA/env.properties
mail.server=qa.mail.domain
src/main/resources/config/common/env.properties
mail.url=${mail.server}/endpoint
Is it possible to load "common/env.properties" so that it's placeholders will be resolved using the given environment specific properties file. For DEV environment, we want the placeholders in "common/env.properties" to be resolved using values from "DEV/env.properties".
There are answers about how to load multiple properties files and profile based loading but could not find an answer for this particular use case.
Thanks in advance.
2 Options :
Generate the common/application.properties using configuration-maven-plugin and filter files for each environment. It is outdated now.
Use application-<env>.properties for each environment and pass the -Dspring.profiles.active=<env> as VM option in application start up. Spring will automatically take the property from correct file.
In option 2, you will be overwriting whatever is present in application.properties with application-.properties. So you dont have to add only the properties which you need to change per environment.
for eg:
Your application.properties can have
logging.level.root=WARN
logging.level.org.apache=WARN
logging.level.org.springframework=WARN
Your application-dev.properties can have
logging.level.org.springframework=DEBUG
which means, when you are starting application using dev profile, spring takes
logging.level.root=WARN
logging.level.org.apache=WARN
logging.level.org.springframework=DEBUG
edit :
Also, you can try something like below on your class. (Spring will overwrite value in config.properties with values from config-dev.properties). ignoreResourceNotFound will make sure, application will still start with default values even if the corresponding file is not found.
#Configuration
#PropertySource("classpath:config.properties")
#PropertySource(value = "classpath:config-${spring.profiles.active}.properties", ignoreResourceNotFound = true)
You can add resources/application.yml file where you can have multiple profiles in one File.
MultiProfile Yaml
e.g.here are two different profiles 'dev' and 'qa' with different applicationNames 'DEV' and 'QA' and one defaultName 'Default'
spring:
application:
name: Default
profiles:
active: qa
---
spring:
profiles: dev
application:
name: DEV
---
spring:
profiles: qa
application:
name: QA
You can achieve this by declaring a property source on a class configuration and setting up an environment variable in the path :
#PropertySource({ "classpath:config/${env}/env.properties" })
#Configuration
public class config{}
And then you launch the spring boot app with the command line variable -env=dev
UPDATE
You can use #PropertySources annotation to load several properties.
#PropertySources({
#PropertySource("classpath:config/${env}/env.properties"),
#PropertySource("classpath:config/common/env.properties")
})
public class config{}

How do I get multiple default values in properties across different profiles?

I have a few microservices with the same configuration, with each depending on a Spring profile. #ConfigurationProperties takes the values that I have in my YAML application profile file. I want to move this configuration to a common project and out of the microservices YAML configuration.
The problem is that I have 2 different configurations for different profiles. I can use #Value to inject default values, but I can do that only once. Is there a way to have multiple default values contingent on a specific Spring profile?
In my project we use Java configuration, not XML.
Do you mean like below ? I have two profiles A1 & B1 with the same property (app.port) in the same yml file (application.yml). The profiles are separated by ---
---
spring:
profiles: A1
app.port: 8080
---
spring:
profiles: B1
app.port: 9090
---
If you want to read properties from common location then write bootstrap.yml in your microservices and delete application.yml.
bootstrap.yml:
spring:
config:
location: file:/home/external/properties/location/
name: application
profiles:
active: dev
file location: /home/external/properties/location/
suppose you need dev and prod environment.Then keep this 3 properties file in this location.
application.yml
application-dev.yml
application-prod.yml
Spring boot read application.yml properties first. If set active profile dev in bootstrap.yml then application.yml value overwirte by application-dev.yml.
For different value just change active profile in bootstrap.yml of your microservices.
According to my search, I cannot resolve this issue as I want. Probably spring-cloud-config would resolve my problem but I don't want to implement this one. I changed the library and don't need to have these separate configs anymore.

Spring Cloud; CloudFoundry; spring.jpa properties

I am using spring cloud to bind services to a spring boot application deployed to CloudFoundry. When running locally, I can pass Java options to the application as follows:
-Dspring.jpa.hibernate.ddl-auto=create-drop
Now I would like to do the same thing when running the application on CloudFoundry. What's the usual way to do this?
An alternative to setting a system property or environment variable is to set this as a Spring property in src/main/resources/application.properties or src/main/resources/application.yml.
application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
application.yml
spring:
jpa:
hibernate:
ddl-auto: create-drop
With this approach, the configuration will be applied regardless of now the app is deployed - locally, on CF, or on another platform.
You can put an env entry in your manifest.yml file like so:
env:
spring.jpa.hibernate.ddl-auto: create-drop
See more information here:
http://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html#env-block

Categories