简体   繁体   English

如何将 spring 引导管理服务器和客户端迁移到一个 spring 引导应用程序中

[英]How to migrate spring boot admin server and client into one spring boot application

I'm using spring boot admin to monitor my springboot application, it's pretty good.我正在使用 spring boot admin 来监控我的 springboot 应用程序,非常好。 But I need to start spring boot admin server firstly and then start my application which contains spring boot client.但我需要先启动 spring 启动管理服务器,然后启动包含 spring 启动客户端的应用程序。

Is there a way to contain the spring boot server into my spring boot application, make them looks like one application, so that I can just start my application, then the server started and the client registerd to the server.有没有办法将 spring 引导服务器包含到我的 spring 引导应用程序中,使它们看起来像一个应用程序,这样我就可以启动我的应用程序,然后启动服务器并将客户端注册到服务器。 The ultimate purpose is my application is running on port 8080, and the spring boot admin is running on port 8081.最终目的是我的应用程序在端口 8080 上运行,而 spring 引导管理员在端口 8081 上运行。

I have checked this thread: How to run spring boot admin client and server in same application but it doesn't contain any details, I can't implement with the possible solution.我已经检查了这个线程: 如何在同一个应用程序中运行 spring 引导管理客户端和服务器,但它不包含任何细节,我无法使用可能的解决方案来实现。

PS: I know I can package the spring boot admin server, and then write the start.sh, startup the server firstly and then start my application, but it's not a pretty solution. PS:我知道我可以package spring 启动管理服务器,然后编写start.sh,先启动服务器然后启动我的应用程序,但这不是一个很好的解决方案。

Anyone can help?任何人都可以帮忙吗?

SpringBoot version: 2.3.4.RELEASE Spring Boot Admin version: 2.3.0 SpringBoot 版本:2.3.4.RELEASE Spring Boot Admin 版本:2.3.0

Technically this should be possible to integrate both in one single application, but I'm afraid you'll run into problems soon.从技术上讲,这应该可以将两者集成到一个应用程序中,但恐怕您很快就会遇到问题。

But let us give it a try...但是让我们试一试...

Reference: Proof of concept GitHub参考:概念证明GitHub

Problems with this approach:这种方法的问题:

  • launch 2 servers on startup: the client on port 8080 , and the admin-server on port 8081在启动时启动 2 个服务器: client在端口8080上,管理员服务器在端口8081
  • the client and the server should have separate configurations客户端和服务器应该有单独的配置

Let's start with the configuration让我们从配置开始

We could separate the multiple configurations by using a profile so we can take advantage of Profile Specific Files我们可以通过使用profile来分隔多个配置,以便我们可以利用配置文件特定文件

application-client.properties 应用程序-client.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

application-admin.properties 应用程序-admin.properties

server.address=localhost
server.port=8081

Launch the server, and the client with the correct profile.使用正确的配置文件启动服务器和客户端。

DemoApplication 演示应用

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!";
    }

}

Launch the Application, and we should be good...启动应用程序,我们应该很好......


If you have two separate applications, then in your client you could launch the admin-server via a process.如果您有两个单独的应用程序,那么在您的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();
    }
}

Explore spring multi-module feature for solving your use-case.探索 spring 多模块功能以解决您的用例。

Have your spring boot server as a parent project and add all your spring boot client projects as individual modules.将 spring 引导服务器作为父项目,并将所有 spring 引导客户端项目添加为单独的模块。 All these individual modules are nothing but a spring boot application.所有这些单独的模块只不过是一个 spring 引导应用程序。 You can package and deploy those individual modules on any different ports.您可以 package 并将这些单独的模块部署在任何不同的端口上。

An easier solution would be to have the applications run inside containers and will be better to have them isolated instead of turning them into a monolith.一个更简单的解决方案是让应用程序在容器内运行,并且将它们隔离而不是将它们变成一个整体会更好。

You can build your applications using docker ( https://spring.io/guides/gs/spring-boot-docker/ )您可以使用 docker ( https://spring.io/guides/gs/spring-boot-docker/ ) 构建您的应用程序

Basically you will have a docker file for each app, something like this:基本上,每个应用程序都会有一个 docker 文件,如下所示:

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

And start them using docker-compose并使用 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

The depends_on will make sure your spring-boot-client starts after spring-boot-admin is healthy depends_on 将确保您的 spring-boot-client 在 spring-boot-admin 健康后启动

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

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