I'd like to run one spring boot application but have it listen on multiple ports.
The aim is to be able to let an Apache forward multiple (sub-) domains to the spring boot application (Tomcat) on different ports. Example:
example.com/** -> PORT 8080
client.example.com/** -> PORT 8090
employee.example.com/** -> PORT 8100
As far as I understood from several threads on SO, I'm best off launching multiple @SpringBootApplication
Annotated classes from one main class, right? (https://stackoverflow.com/a/25870132/1510659)
What I didn't grasp yet, is how to configure each one of those applications separately.
Let's say I have launched these three Applications as shown in the linked post above:
MainExampleApplication
ClientExampleApplication
EmployeeExampleApplication
Now, for example, I want to have separate Spring Security @Configuration
classes for each of these Applications, as well as @RequestMappings
which might have the same value (e.g. "/").
How do I tell the @Configuration
or @Controller
classes which Application they are assigned to?
Or are there properties that can to be passed to the applications on startup to specify which resources are responsible for the context?
I hope I'm not going in a totally wrong direction here. I do have experience with Spring MVC and have configured some rather simplistic Spring applications - but not with multiple contexts. I'd be really glad if someone could lead me in the right direction. Thank you in advance.
UPDATE
As mentioned in iamiddy's answer and xeon's comment, I used Spring Profiles for that. I provided the SpringApplicationBuilder with a profile for each of my application contexts on startup and used the @Profile("some_profile")
on the @Components
that should only be available to some of the contexts.
We can have multiple application contexts that share a parent-child relationship. A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.
Spring Boot supports different properties based on the Spring active profile. For example, we can keep two separate files for development and production to run the Spring Boot application.
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.
Use Profiles
it's a great spring feature, loads only beans associted with the profile.
Once that's done start your applications N times with different port
and profile
arguments
Ex: Here is how you would start the first one, do it for the rest to your N
java -jar -Dspring.profiles.active=production1 -Dserver.port=9000 app.jar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With