Spring boot run multiple instances of same profile in service - spring-boot

In Spring boot application, how run multiple instances of same profile on same server (OS)?

To run multiple instances of same profile of service
remove property from profile in yml (or properties) file -> server.port (this is in case of using remote configuration accessed through configuration server)
on running the war (or jar) set the property of port and profile in command line as follows
java -jar -Dserver.port=7012 -Dspring.profiles.active=production
demo-0.0.1-SNAPSHOT.jar
java -jar -Dserver.port=7011
-Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

If you want to run multiple instances of your project in sts (spring tool suite) follow below steps
change server.port=0 (this makes application run on random port) in application.properties or yaml
open Boot bashboard, run instance once, it will start running on random port
right click that intance, click duplicate config, it will create new instance in boot dashboard
run new instance that will be started on new random port
you can create as many duplicate instance you want

Related

Best way to start multiple dependent spring boot microservices locally?

Currently my team maintains many spring boot microservices. When running them locally, our workflow is to open a new IntelliJ IDEA window and pressing the "run" button for each microservice. This does the same thing as typing gradle bootRun. At a minimum each service depends on a config server (from which they get their config settings) and a eureka server. Their dependencies are specified in a bootstrap.yml file. I was wondering if there is a way to just launch one microservice (or some script or run configuration), and it would programatically know which dependencies to start along with the service I am testing? It seems cumbersome to start them the way we do now.
If you're using docker then you could use docker compose to launch services in a specific order using the depends_on option. Take a look here and see if that will solve your problem.
https://docs.docker.com/compose/startup-order/

Pass command-line argument for a particular Tomcat web-application startup

For my Spring-Boot project, I have used jasypt library for encrypting my datasource.password in application.properties file. In my localhost setup, I have configured VM arguments in eclipse and it works completely fine.
-Djasypt.encryptor.password
-Djasypt.encryptor.algorithm
Now, I want to deploy my application to Tomcat server where I am not able to figure out where to configure these VM arguments so that Tomcat picks them when I click on "start" button from Tomcat Manager GUI.
Read solutions about setenv.sh but I think that works at complete Tomcat server level. I need to pass these arguments only for my current application without affecting other applications which are already hosted.
Any help is appreciated.

How can we set different environment profile in spring boot?

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

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.

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