在一个弹簧启动容器中运行多个Web应用程序

Leo*_*ley 18 java spring spring-mvc spring-boot

我希望能够有多个Web应用程序共享域项目并在不同的contextPaths下运行.

通过在spring启动应用程序中设置server.contextPath =/webshop,我不需要为所有RequestMappings添加前缀.

我希望网店,管理员和主页共享一个包含所有实体和公共服务的公共域项目.

也许有类似的东西?

public static void main(String[] args) {
    new SpringApplicationBuilder(Domain.class)
        .showBanner(false)
        .child(Admin.class, Webshop.class)
        .run(args);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何使用通用域模型启动Spring启动应用程序,然后使用独特的contextPaths启动一些独立的Web应用程序?

Dav*_*yer 9

像这样例如:

public static void main(String[] args) {
    start(Admin.class, Webshop.class).run(args);
    start(Another.class).properties("server.port=${other.port:9000}").run(args);
}

private static SpringApplicationBuilder start(Class<?>... sources) {
    return new SpringApplicationBuilder(Domain.class)
        .showBanner(false)
        .child(sources);
}
Run Code Online (Sandbox Code Playgroud)

它将在不同的端口上启动两个应用程序.

  • 这种方法与原始引导架构设计相矛盾:它首先用于快速开发微服务.所以它是单个应用程序的单一上下文.我想你不想处理单个上下文,其中作为客户端执行管理任务,并解决复杂的安全问题.所以我建议:只需使用spring boot创建2个或3个应用程序.将它们部署到docker,并创建一个apache或nginx或任何其他的docker容器,它将根据上下文路径代理请求以更正Web应用程序.这就像springboot打算的那样 (8认同)
  • 很高兴知道,但我想在相同的端口但不同的contextPaths启动它们. (5认同)
  • 也许2个servlet就够了呢? (2认同)