Spring Boot sperate @Configurations用于多个应用程序上下文 [英] Spring Boot sperate @Configurations for multiple application contexts

查看:201
本文介绍了Spring Boot sperate @Configurations用于多个应用程序上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个spring boot应用程序,但是让它在多个端口上监听.

I'd like to run one spring boot application but have it listen on multiple ports.

目标是能够让Apache在不同端口上将多个(子)域转发到Spring Boot应用程序(Tomcat).示例:

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

据我从SO上的多个线程了解到,最好从一个主类启动多个@SpringBootApplication带注释的类,对吗? ( https://stackoverflow.com/a/25870132/1510659 )

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

例如,现在,我想为这些应用程序中的每一个提供单独的Spring Security @Configuration类,以及可能具有相同值(例如"/")的@RequestMappings.

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

如何告诉@Configuration@Controller类将它们分配给哪个应用程序?

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?

我希望我不会在这个方向上完全错误.我确实具有Spring MVC的经验,并且已经配置了一些相当简单的Spring应用程序-但是没有多个上下文.如果有人可以带领我朝正确的方向前进,我将感到非常高兴.预先谢谢你.

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.

更新

正如iamiddy的回答和xeon的评论中所提到的,我为此使用了Spring Profiles.在启动时,我为SpringApplicationBuilder提供了每个应用程序上下文的配置文件,并使用了@Components上的@Profile("some_profile"),该属性仅对某些上下文可用.

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.

推荐答案

使用Profiles是一个很棒的spring功能,仅加载与配置文件关联的bean. 完成此操作后,使用不同的portprofile自变量

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

例如:您将按照以下步骤开始第一个,其余的操作执行到您的 N

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

这篇关于Spring Boot sperate @Configurations用于多个应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆