简体   繁体   English

如何运行两个不同的 spring 启动实例?

[英]how to run two different spring boot instances?

I have a need to run two separate spring boot instances, the first instance is responsible for some code-generation, while the second is serves as an API.我需要运行两个单独的 spring 启动实例,第一个实例负责一些代码生成,而第二个实例用作 API。

Here is the process: 1. Run the generation spring boot app 2. After the generation is shutdown, run the API spring boot app流程如下: 1.运行生成spring引导应用程序 2.生成关闭后,运行API spring引导应用程序

// Api.java

@Log4j2
@EnableEurekaClient
@SpringBootApplication
public class Api {
    public static void main(String[] args) {
        SpringApplication.run(Api.class, args);
    }
}

// Generator.java
@Log4j2
@EnableEurekaClient
@SpringBootApplication
public class Generator implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Generator.class, args);
        Api.run(args); // run the API instance
    }

    @Override
    public void run(String... args) throws Exception {
        // do generation here
    }
}

Exact error: Error creating bean with name 'nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication确切的错误: Error creating bean with name 'nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication

The error I'm currently getting is that there is already a spring boot instance, and hence can not call Api.run(args) .我目前遇到的错误是已经有一个 spring 启动实例,因此无法调用Api.run(args) I'd like to be able to run both of these one after another.我希望能够一个接一个地运行这两个。 If there is a better way I'm all ears.如果有更好的方法,我会全力以赴。 At the end of the day it needs to be a single runnable jar.归根结底,它需要是一个可运行的 jar。 My main class is configured to point to the Generation, and theoretically it should generate the code then run the Api.我的主要 class 配置为指向 Generation,理论上它应该生成代码然后运行 Api。

I use something like this to run 2 services - User Service and Project Service:我使用这样的东西来运行 2 个服务 - 用户服务和项目服务:

    SpringApplicationBuilder uws = new SpringApplicationBuilder(UserWebApplication.class)
            .properties("server.port=8081",
                    "server.contextPath=/UserService");
    uws.run();
    SpringApplicationBuilder pws = new SpringApplicationBuilder(ProjectWebApplication.class)
            .properties("server.port=8082",
                    "server.contextPath=/ProjectService");
    pws.run();

They use different ports and context paths.它们使用不同的端口和上下文路径。

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

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