Two or more spring boot application sharing same port - spring-boot

I have a use case where I need to run atleast two or more spring boot applications on the same port.
I am able to run spring boot applications via mvn spring-boot:run -Pdev... port and other information. I have provided following configuration in spring boot projects
server.port= 8597
server.contextPath=<as per the project>
spring.config.name=<project name>
With this configuration, I successfully run two spring boot application on same port i.e. 8587.
Now, here the problem comes, when I package the project as jar for dist purposes and run the application as java -jar <profile>..... second boot project run fails with Address already in use.
I am not able to understand where I am going wrong. I even tried passing context, port and config name in vm arguments but no success.
Any help?

Related

Run one Spring application in two modes - as CommandLineRunner and SpringBootServletInitializer type

I need to run my Spring Boot app in two modes:
As command line application (without Tomcat) as CommandLineRunner
As SpringBootServletInitializer REST API app for standalone Tomcat.
Question: Is it possible? How can I achieve this setup?
I think there is a contradiction in the question:
If you use SpringBootServletInitializer then the artifact is supposed to be packaged as WAR and deployed on some webserver like tomcat. Tomcat is not embedded in this case.
If you want an Embedded Tomcat, then you don't need WAR, you should use a "usual" jar type of spring boot application artifact.
A couple of facts that might help:
There is nothing special about CommandLineRunner beans except the fact spring boot automatically will run a run method on all beans that implement this interface when the application context becomes available.
The fact that you have a web starter in dependencies usually means that you intend to run the web server (tomcat), however its not always the case, you can specify the web env. type to 'NONE' and an embedded tomcat won't be started (see this thread for more technical information)
Now given these two facts, and assuming you don't really build a WAR (in this case you'll have to host it on embedded tomcat, java doesn't recognize WARs) - you can define a "flavour" - profile for example and define the configuration for command line runner to be active only for certain profile and the property of not-running the tomcat to be relevant for another type of profile.
Update
Based on OP's comment:
So, the goal is being able to run:
java -jar myapp.war
in a way that only CommandLineRunner will run and no real tomcat will be loaded
On the other hand you want to put myapp.war to webapps folder of some tomcat and in this way, CommandLineRunner won't be invoked, right?
In this case, consider implementing the following suggestion:
create the application-cmd.properties and specify that the tomcat should not run (web env. type = NONE) - see the link that I've provided above
Add #Profile("cmd") on CommandLineRunner implementation so that it would run only when profile 'cmd' is specified.
Run java -jar myapp.war with --spring.profiles.active=cmd flag so that cmd profile will be loaded.
Don't change anything for tomcat deployment - profile cmd won't be activated and CommandLineRunner won't run

how to add reference of one spring boot application to another spring boot app

I want to make a spring boot product base application.
In that I want to make a different spring application depends on each other by adding reference of one application to another application.
And make seperate jars of each and put it into tomcat
I would like to suggest you to start with simple hello-world application and later you can scale your application up.
Simple way is go to 'start.spring.io', add dependencies like spring boot starter web.
You can download the project after adding dependencies. Now, just import the project in your eclipse as 'import existing maven project'.
Spring boot has inbuilt tomcat, simply right click on the import project and select
run as --> java application
Your application will start on port 8080.
Now, you can create one more similar application and can run the application on port 8081 by adding a property server.port=8081 in your 2nd application.
You can call the api of 1st application from 2nd application.
If you are planning to create microservices, then please read about spring cloud.

Defining Database dynamically in spring boot maven based war application

My Spring boot application won't know the database to be connected in prior, once before application deployment, the user will select the database to be connected, and places the jar in the server webinf(or probably some other repository path), and changes the externalized properties file, so that application connects to the database, I was trying giving the database dependency scope as provided, but getting class not found. What is the preferred approach for solving the issue?
You can run your Spring Boot bootJar like this:
java -cp your-jdbc-driver.jar -jar your-boot-jar.jar --spring.datasource.url=your:jdbc:url

End to end test across multi Spring Boot applications

Currently in our project, we are using Spring Integration to integrate many service and some protocol related endpoints.
The project is a multi Spring Boot applications, more than one executable jars will be deployed in production.
The question is:
How to run an end to end test which needs to run cross some of these applications, I have to run the one by one manually? In before none-Spring-Boot applications, I can use Maven tomcat7 plugin to complete this work(deploy the wars into an embedded tomcat and run it in pre-integration-test phase), now how to start up all related applications before I run my test. Assume I do not use Docker/Vagrant now.
Similar question found on stackoverflow, End to end integration test for multiple spring boot applications under Maven
How to run the end2end test automatically?
In an Spring Integration test, sometime I have to mock a http endpoint, so I wrote a simple Controller in test package to archive this purpose, but I want to run it at a different port, which make it more like an outside resource. How to run different #SpringBootApplicaiton classes at varied ports at the same time in the test for this purpose?
I am using the latest Maven, Java 8, Spring Boot 1.3.1.RELEASE.
Actually, Spring Boot comes with the embedded Servlet Container support. One of them is exactly Tomcat. The default on for the org.springframework.boot:spring-boot-starter-web.
With the org.springframework.boot:spring-boot-starter-test and its #SpringApplicationConfiguration and #WebIntegrationTest you can achieve your requirements, even with the random port.
Please, refer to the Spring Boot Reference Manual for more information:
To change the port you can add environment properties to #WebIntegrationTest as colon- or equals-separated name-value pairs, e.g. #WebIntegrationTest("server.port:9000"). Additionally you can set the server.port and management.port properties to 0 in order to run your integration tests using random ports.
With that your #SpringBootApplicaiton will be deployed to that embedded Tomcat and your test can get access to the ran services/controllers.
Note: it doesn't matter if your Spring Boot application has Spring Integration facilities. The behavior is the same: embedded Servlet Container and integration tests against #Value("${local.server.port}").

How to configure wily with spring boot application

I have created an application as spring boot application,Now i want to integrate this application with wily,where i ll passed wily parameter in application to configure application.
The fact that it's a Spring Boot application shouldn't make any difference. If you're using an executable jar file, you'll need to configure the agent when you launch the jar, for example:
java -jar my-app.jar -javaagent:<Agent_Home>/Agent.jar -Dcom.wily.introscope.agentProfile=<Path_To_Agent_Profile>
If you're deploying your Spring Boot application as a war file to a servlet container or application server, you'll need to be make the equivalent configuration changes. The documentation describes how to configure Tomcat.

Resources