Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run multiple spring boot jars in one jvm

Tags:

java

spring

My project contains several services, each one is annotated with @SpringBootApplication and can be run on a random port via "gradle bootRun".

Is it possible to build the services into jars and run them together in one JVM? Not matter by programmatic method or just put them in a container.

Please show me some instructions if possible. Thanks!

like image 857
Sean Liang Avatar asked Jul 25 '15 08:07

Sean Liang


People also ask

Can two spring boot applications run on same port?

No you can have only one application listening on a specific port at a time.

Can we have more than one @springbootapplication?

In this example we are having two SpringBoot applications that we want to merge into one and for that we will create a new SpringBoot application for package them both and add the two old applications as maven dependencies. Then we will ask Spring to boot-up the application.


2 Answers

It's a little hacky, but can be done. I wrote a blog post about it some time ago: Running Multiple Spring Boot Apps in the Same JVM. The basic idea is to run every Spring Boot application in a different classloader (because otherwise there would be resource conflicts).

I, personally, only used it for testing. I would prefer to run the different applications in different docker containers in production. But for testing it's pretty cool: You can quickly boot up your application and debug everything...

like image 184
David Tanzer Avatar answered Sep 20 '22 22:09

David Tanzer


If you want to launch multiple spring boot microservices in single JVM then you can achieve this by launching multiple threads. Please refer sample code here https://github.com/rameez4ever/springboot-demo.git

like image 24
Rameez Shikalgar Avatar answered Sep 19 '22 22:09

Rameez Shikalgar