java—如何将SpringBoot管理服务器和客户机迁移到一个SpringBoot应用程序中

7lrncoxx  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(333)

我用SpringBootAdmin来监视我的springboot应用程序,非常好。但是我需要先启动springboot管理服务器,然后启动包含springboot客户机的应用程序。
有没有办法将spring引导服务器包含到我的spring引导应用程序中,使它们看起来像一个应用程序,这样我就可以启动我的应用程序,然后服务器启动,客户端注册到服务器。最终目的是我的应用程序运行在端口8080上,而spring boot admin运行在端口8081上。
我已经检查了这个线程:如何在同一个应用程序中运行springboot管理客户机和服务器,但它没有包含任何细节,我不能用可能的解决方案实现。
ps:我知道我可以打包SpringBoot管理服务器,然后编写start.sh,先启动服务器,然后启动我的应用程序,但这不是一个很好的解决方案。
有人能帮忙吗?
springboot版本:2.3.4.发布springboot管理版本:2.3.0

qzwqbdag

qzwqbdag1#

从技术上讲,这应该可以将两者集成到一个应用程序中,但我担心您很快就会遇到问题。
但是让我们试试。。。
参考:概念证明github
这种方法的问题:
启动时启动2台服务器 client 左舷 8080 ,以及端口上的管理服务器 8081 客户端和服务器应该有单独的配置

让我们从配置开始

我们可以使用 profile 所以我们可以利用特定于配置文件的文件
应用程序-客户端.properties

server.address=localhost
spring.boot.admin.client.url = http://localhost:8081/
management.endpoints.web.exposure.include = *
management.endpoint.health.show-details= always

spring.application.name=client-app
spring.boot.admin.client.instance.name=client-app

应用程序-管理属性

server.address=localhost
server.port=8081

使用正确的配置文件启动服务器和客户端。

演示应用程序

public class DemoApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication admin = new SpringApplication(DemoAdminServer.class);
        admin.setAdditionalProfiles("admin");
        admin.run(args);

        SpringApplication client = new SpringApplication(DemoClient.class);
        client.setAdditionalProfiles("client");
        client.run(args);
    }
}

@EnableAdminServer
@Configuration
@EnableAutoConfiguration
@Profile("admin")
class DemoAdminServer {

}

@SpringBootApplication
@RestController
@Profile("client")
class DemoClient {

    @GetMapping
    public String hello(){
        return "Hello world!";
    }

}

启动应用程序,我们应该很好。。。
客户
管理服务器
如果您有两个独立的应用程序,那么在 client 你可以启动 admin-server 通过一个过程。

@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) throws Exception{
        startAdminServer();
        SpringApplication.run(DemoApplication.class, args);
    }

    static void startAdminServer() throws Exception {
        new ProcessBuilder()
                .redirectErrorStream(true)
                .command("cmd.exe",
                         "/c",
                         "java -jar path_to_your_admin_server.jar")
                .start();
    }
}
tktrz96b

tktrz96b2#

一个更简单的解决方案是让应用程序在容器中运行,最好将它们隔离,而不是将它们变成一个整体。
您可以使用docker构建应用程序(https://spring.io/guides/gs/spring-boot-docker/)
基本上,每个应用程序都有一个docker文件,如下所示:

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

使用docker compose启动

version: '3.7'
services:
  spring-boot-admin:
    image: spring-boot-admin-image
    container_name: spring-boot-admin
    ports:
      - 8081:8081
    healthcheck:
      test: "curl --fail --silent localhost:8081/actuator/health | grep UP || exit 1"
      interval: 20s
      timeout: 5s
      retries: 5
      start_period: 40s

  spring-boot-client:
    image: spring-boot-client-image
    container_name: spring-boot-client
    ports:
      - 8080:8080
    depends_on:
      - spring-boot-admin

依赖将确保您的spring boot客户端在spring boot admin运行正常后启动

s1ag04yj

s1ag04yj3#

探索spring多模块特性以解决您的用例。
将spring引导服务器作为父项目,并将所有spring引导客户机项目作为单独的模块添加。所有这些单独的模块都只是一个spring引导应用程序。您可以在任何不同的端口上打包和部署这些单独的模块。

相关问题