Run multiple web apps in one spring boot container

10,752

Like this for example:

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);
}

It would start two apps on different ports.

Share:
10,752

Related videos on Youtube

Leon Radley
Author by

Leon Radley

I am the CTO at wec360.com. Scandinavias leading visualization platform.

Updated on September 15, 2022

Comments

  • Leon Radley
    Leon Radley over 1 year

    I would like to be able to have multiple web apps sharing a domain project and running under different contextPaths.

    By setting server.contextPath=/webshop in a spring boot app I dont need to prefix all the RequestMappings.

    I would like the webshop, admin, and main page to share a common domain project which contains all the entities and common services.

    Maybe with something like?

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

    My problem is how do I start a spring boot app with a common domain model, and then a couple of stand alone web apps with unique contextPaths?

    • mad_fox
      mad_fox about 8 years
      Did you ever figure this out? I'm facing the exact same issue.
  • Leon Radley
    Leon Radley over 9 years
    that good to know, but i would like to start them on the same port but different contextPaths.
  • Ilya Dyoshin
    Ilya Dyoshin about 7 years
    That approach contradict with original boot architecturial desing: it is intended first of all for quick development of microservices. So it is inteded single context for single app. I guess you don't want to deal with single context in which as client as admin tasks are performed, and solving complex security issues. So my advise: simply create 2, or 3 apps using spring boot. Deploy them to docker, and create a docker container of apache or nginx, or any other, which will be proxying the requests based on context path to correct web app. And this will be like it was intended by springboot
  • Mo Hassan
    Mo Hassan almost 7 years
    @IlyaDyoshin I like what you suggested above, do you know of any simple hello world example showing how you would break a complex app to 2-3 dockerzed apps
  • Ilya Dyoshin
    Ilya Dyoshin almost 7 years
    @MoHassan there would not be any tutorial: it is up to you first of all to cut your application in subsystems, which could operate separately. Then you start to separate them. also remember that in contrast to single large application where you can keep DRY principle, in microservices world you will be WET (writting everything twice): one part for microservice, another for client part of microservice. After that you can establish an eureka server (have a look at spring's tutorials), and then run all this in docker with compose...