2025年springboot指定上下文,Spring Boot sperate @Configurations用于多个应用程序上下文

springboot指定上下文,Spring Boot sperate @Configurations用于多个应用程序上下文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

大家好,我是讯享网,很高兴认识大家。


讯享网

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//)

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.

解决方案

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

小讯
上一篇 2025-04-06 07:37
下一篇 2025-01-07 16:44

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/47713.html