How can we set different environment profile in spring boot? - spring

I am working on microservices using spring boot. i have around 5 microservices. so I configured spring cloud config server to centralized configuration. its perfectly working fine.
config server configuretion
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/common-config
server.port=8888
now the problem is I have two environment dev and test and i created application-test.properties and application-dev.properties as per the spring documented i need to set profile like
Spring profile
spring.profiles.active=test
but its already set as a native then how can i load multiple profile.
please help me ..

You can pass in the active profile name as an environment variable at run time of each of the apps. This will take priority over the active profile listed in your properties file. You can do this with any property actually. If you're using docker to launch your apps, you can pass it in from your Dockerfile or from your docker-compose.yml

You can set spring.profiles.active=$ACTIVE_SPRING_PROFILE and set OS environment ACTIVE_SPRING_PROFILE = test

You can use multiple profiles in Spring. Try:
spring.profiles.active=dev,native
See more: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles

Related

How to desgin the spring boot profile active arugment?

As we known that, we have some methods to set the profile, the priortity order is javaCode > commondline argument > jvm variable > os variable > config...
If I have 500 or more spring-boot-micro-services, what is the right way to set the profile variable?
The profile may be has 10 or more values such as prod/prod,sg/prod,us and others.
Thanks for your practical advise very much.
Maybe we can use template + os variable?
We can externalize the properties file in springboot app and make use of Springboot config server.
Here, we need to create one springboot application which will act as config server. On main class give #EnableConfigServer #SpringBootApplication.
Refer to https://spring.io/guides/gs/centralized-configuration/
You can create git repo for this, make branching based on your environments and give data/properties likewise. And deploy that particular branch on particular env, like config_UAT on UAT env.

Change Spring profile from Consul configuration

Migrating an SpringApplication I find that there are profiles used to run in different modes. Anytime there may be different instances with different profiles running in production at the same time.
I want to be able to change those Spring profiles from Consul. But Consul seems to work the other way arround. Profiles change properties.
How can I change the profile from Consul?

Spring Boot application profiles

I understand there's multiple ways of handling application property files and profiles in Spring Boot and I've seen multiple questions and answers on how to handle each but I'm trying to find the "best" way of handling it for a new project, if there is one.
The application is hosted in Weblogic 12c on production/pre-prod (with a jndi database connection) and ran locally in tomcat (with hardcoded database details) for development. I'd like it so that when the project is built via gradle and deployed to production it uses the jndi properties file and when ran locally it defaults to the hardcoded datasource with minimal changes required.
src/main/resources/application.properties
# DEV
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
# DEV
# PROD
# spring.datasource.jndi-name=
# spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
# PROD
From my understanding the recommended way is to externalize the property files and place the required one in a config directory alongside the WAR file for any differing config which is then automatically picked up and used?
You should consider creating multiple profiles. This means: Either multiple properties-Files, or multiple profiles in one file:
See https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
I would recommend to use multiple application-ENV.properties, e.g.
application-prod.properties and application-preprod.properties.
There is always one active profile and settings from the application.properties (without any profile suffix) are used as default values if not overwritten in a specific profile-file.
Depdending on your environment (local, prod etc.) you should set an environment variable (start the java-process/application server with that environment variable), e.g.:
SPRING_PROFILES_ACTIVE=prod
On your local machine you would set:
SPRING_PROFILES_ACTIVE=dev
With this variable you can control, which profile is currently active.
Also consider integrating the active profile into you Continious Integration/Deployment settings.
Please note that putting plain text passwords hardcoded into committed files is not a good idea. Consider using jasypt or a spring cloud config server for your prod database configuraiton or use any mechanism that your cloud provider provides to you if you use any. Microsoft Azure for example provides a KeyVault for sensitive data.
https://cloud.spring.io/spring-cloud-config/multi/multi_spring-cloud-config.html
http://www.jasypt.org/
If you use gradle good solution is to set application.properties and test.properties files and put into them properties for prod and preprod respectively.
Then run application with different vm arguments: -Dspring.profiles.active=test for test.properties and without arguments for application.properties
Use gradle instruments and configure them once for test and prod. For example: gradle bootWar configure without vm arguments, gradle bootWarTest with vm arguments -Dspring.profiles.active=test. Save once you configs and you will create war for different environments only selecting between two buttons in gradle.

How to configure different data sources for local testing and deployment in Spring Boot Application

I am trying to find the best way to configure my Spring Boot Web application to easily switching between the following data sources for both local testing and deployment.
H2 in memory db. Local testing only.
Dev oracle. Local testing and deployment.
Prod oracle. Deployment only.
By local testing, I mean to test in IDE environment (Eclipse). Dev and prod oracle databases are set up on two remote servers.
After some research, there are different ways to switch from one data source to another.
Use Spring profile. Using H2 and Oracle with Spring Boot. Set up the following files in classpath, application.properties, application-h2. properties and application-dev.properties. While connections for h2 and dev are defined in corresponding properties files, spring.profiles.active is set in application.properties. My understanding is this property can be overridden during build process by specifying spring.profiles.active. However, it seems to be a JVM variable, how do I set it running maven?
Maven profile. Create multiple profiles in pom and a filter pointing to application properties files. The profile specified by -P option during maven build will determine which application properties file to look. However, according to maven application with multi environment configuration can't deploy on tomcat, this will generate multiple wars for different deployment. So method 1 is preferred. Plus, it does not apply to switching datasources while testing locally.
Persistence units. Define different persistence units for different data sources in persistence.xml. Use EntityManager by choosing a specific unit. Variation of this method include having a variable in unit names which is determined in application.properties.
JNDI lookup. Set up a jndi name in application.properties with spring.datasource.jndi-name. The actual database information including url and credentials will be specified in context.xml in the tomcat folder where the war will be deployed.
My mind is set on local testing environment. Gonna go with method 1. Switching between H2 in memory and oracle is so easy just by changing the property in application.properties. Since the testing is usually done in IDE, war does not need to be generated, although answers are welcome for run maven install with spring.profiles.active.
As far as deployment, JNDI is definitely the way to go. However, I am concerned that the two properties in application.properties: spring.profiles.active and spring.datasource.jndi-name may be conflicting with each other. If I have spring.profiles.active=h2 and then tried to deploy the war to prod server, does it try to connect to h2 based on the spring profile or to prod db based on jdni-name? What is the best practice to accommodate all scenarios with enough flexibility?
Also is a explicit configuration class for DataSource required such as Configure Mutiple DataSource in Spring Boot with JNDI? My understanding is application.properties and spring profile should be enough to handle it, right?
Definitely use Spring profiles.
You don't want to use Maven profiles as it creates different artifacts. Ask your QA/Release engineers how they feel about having different artifacts for different environments :). They wouldn't be happy.
H2 is what you want to use in CI server integration testing as well. Such integration testing is fast and easy.
Instead of changing profile in application.properties, consider defining profile via command line parameter. So that configuration file changes are not required to run your application in different profiles.

spring boot: create multiple context for the same application-context.xml

I need to create multiple context for the same application context.xml file and each context use its own application.properties.
How to do it using spring boot ?
I have 3 clients who have the same behaviour but each one with specific details declared into client-application.properties.
So i use also spring integration and the flow will be reused for each client .
I need to launch 3 clients in the same time and each one with its own application.properties. And i use xml for that.
Take a look at this... I have a single project, and inside of it are three application.properties (or the number you need)
in application.properties, i specify general parameters
and in each application-.properties i specify specific environment properties, e.g., the port in production:
And the port for my dev profile:
In order to use them check the documentation that Ivaylo recommended
...A small example:
In this case, the application will boot on the port showed in the different .properties files.
You can specify the profile like: mvn spring:boot run -Dspring.profiles.active=dev

Resources