简体   繁体   English

Spring Boot为多个应用程序上下文编写@Configurations

[英]Spring Boot sperate @Configurations for multiple application contexts

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. 目的是让Apache能够将多个(子)域转发到不同端口上的spring boot应用程序(Tomcat)。 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? 据我从SO上的几个线程理解,我最好从一个主类中启动多个@SpringBootApplication Annotated类,对吧? ( https://stackoverflow.com/a/25870132/1510659 ) 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 (eg "/"). 现在,例如,我想为每个应用程序分别提供Spring Security @Configuration类,以及可能具有相同值的@RequestMappings (例如“/”)。

How do I tell the @Configuration or @Controller classes which Application they are assigned to? 如何告诉@Configuration@Controller类分配给哪个应用程序?

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. 我确实有使用Spring MVC的经验,并且已经配置了一些相当简单的Spring应用程序 - 但是没有多个上下文。 I'd be really glad if someone could lead me in the right direction. 如果有人能带领我朝着正确的方向前进,我真的很高兴。 Thank you in advance. 先感谢您。

UPDATE UPDATE

As mentioned in iamiddy's answer and xeon's comment, I used Spring Profiles for that. 正如iamiddy的回答和xeon的评论中提到的那样,我使用了Spring Profiles。 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. 我为SpringApplicationBuilder在启动时为每个应用程序上下文提供了一个配置文件,并在@Components上使用了@Profile("some_profile") ,它只能用于某些上下文。

Use Profiles it's a great spring feature, loads only beans associted with the profile. 使用Profiles它是一个很好的弹簧功能,只加载与配置文件关联的bean。 Once that's done start your applications N times with different port and profile arguments 完成后,使用不同的portprofile参数启动应用程序N

Ex: Here is how you would start the first one, do it for the rest to your N 例如:这是你如何开始第一个,剩下的就是你的N.

java -jar -Dspring.profiles.active=production1 -Dserver.port=9000 app.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM