【问题标题】:how to run two different spring boot instances?如何运行两个不同的 Spring Boot 实例?
【发布时间】:2019-11-11 20:01:05
【问题描述】:

我需要运行两个独立的 spring boot 实例,第一个实例负责一些代码生成,而第二个实例用作 API。

流程如下: 1.运行生成spring boot app 2.生成关闭后,运行API spring boot app

// 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
    }
}

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

我目前遇到的错误是已经有一个 spring boot 实例,因此无法调用Api.run(args)。我希望能够一个接一个地运行这两个。如果有更好的方法,我会全力以赴。归根结底,它需要是一个可运行的 jar。我的主类配置为指向Generation,理论上它应该生成代码然后运行Api。

【问题讨论】:

  • “我目前遇到的错误是已经有一个spring boot实例”显示exact错误。
  • Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
  • 用详细信息编辑您的问题

标签: java spring spring-boot


【解决方案1】:

我使用这样的东西来运行 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();

它们使用不同的端口和上下文路径。

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2019-11-29
    • 2020-12-21
    • 2017-04-27
    • 2018-07-10
    相关资源
    最近更新 更多