How to run multiple instances of a Spring Boot Application via a script - spring

I need to create 20 instances of a Spring Boot Application. Is there anyway to automate it so that the below command can be run multiple times and increment the port number each time:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8001
Furthermore once the instances are all started up, is there a way to send an identical POST request with a JSON body to each instance at an endpoint on the Spring Boot Application /someEndpoint.

Try writing a script (.sh file ) in which run a loop for desired time for the command. Use --server.port=0 which will randomly select the port.

Related

Fail over mechanism for cron scheduled application deployed in two difference namespaces

I have a spring boot application (which reads data from txt file --> insert into database --> execute few sql queries --> write to csv file). It will run using a cron scheduler and is deployed in namespace prod-1. I want to deploy the same application in one more namespace prod-2 for backup (disaster recovery). I want to implement a fail over mechanism such that whenever the job in prod-1 failed, it should automatically start job in prod-2 and pick up from where it failed. Any ideas regarding the approach would be appreciated.

How to run a Spring Boot microservice multiple instances or times at the same time?

I have one Spring Boot Microservice that will generate data like half an hour for one user. I need to run the same microservice at the same time to generate data for multiple users. So how to create or run the same microservice multiple times or instances?
Am not 100% sure that this is the best way , but you can use gradle bootrun with multiple ports to run the same instance like this
./gradlew bootRun --args='--server.port=8888'
and if you are using maven , you can do it like this
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
and there are profiles that can be used and set when running like this
./gradlew bootRun --args='--spring.profiles.active=dev'
so you will need to create few profile and then run each one alone .
am pretty sure that there is a smarter way though .

Spring task:scheduled or #Scheduler to restrict a Job to run in multiple instance

I have one #Scheduler job which runs on multiple servers in a clustered environment. However I want to restrict the job to run in only in one server and other servers should not run the same job once any other server has started it .
I have explored Spring Batch has lock mechanism using some Database table , but looking for any a solution only in spring task:scheduler.
I had the same problem and the solution what I implemented was a the Lock mechanism with Hazelcast and to made it easy to use I also added a proper annotation and a bit of spring AOP for that. So with this trick I was able to enforce a single schedule over the cluster done with a single annotation.
Spring Batch has this nice functionality that it would not run the job with same job arguments twice.
You can use this feature so that when a spring batch job kicks start in another server it does not run.
Usually people pass a timestamp as argument so it will by pass this logic, which you can change it.

Spring boot run multiple instances of same profile in service

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

How to push and run a spring-batch application on Bluemix?

I have created a spring batch application in spring boot to perform a daily activity for processing data in batch. I am also able to build and create an uber-jar that has all the dependencies in it. How do I push this jar file as a batch application (not as a web application) and how do I invoke the application to start from the command line if possible?
For cloud foundry applications without the web interface, you can run them with:
---
...
no-route: true
This will disable the web interface - https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html#no-route
You may also want to set the health check to process:
---
...
health-check-type: process
This will monitor the exit status of your application. Note that this will monitor the process your java process. If it stops (e.g. batch job application finishes), cloud foundry will try to restart it - https://docs.cloudfoundry.org/devguide/deploy-apps/healthchecks.html. This assumes that you want your application to run continuously.
You will probably want to check the java build pack for methods for running your jar - https://github.com/cloudfoundry/java-buildpack/blob/master/README.md.
I think you will want to deploy using the Java Main method, which runs Java applications with a main() method provided that they are packaged as self-executable JARs. Cloud foundry will automatically run the main() method.

Resources