Spring 引导管理页面

Spring Boot Admin Page

我试图了解如何在我的应用程序中使用 SBAP,因为它是一个非常方便的开发工具。我正在阅读他们的 reference guide 但我不理解一些事情。 这是我现在的应用程序的 pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <packaging>war</packaging>

    <groupId>com.batch.books.test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <version>1.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这是我的 Application.java:

package app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

}

还有我的 application.yml 文件:

info:
  version: @pom.version@
  stage: test

spring:
  application:
    name: @pom.artifactId@
  boot:
    admin:
      url: http://localhost:8080

其余的我很困惑。

  1. 那么我是否将我的应用程序注册为服务器和客户端?
  2. 如何 运行 此应用程序并访问管理页面?他们没有提到任何 URL 去查看管理页面。只是:http://localhost:8080?
  3. 如何为开发设置它,但在生产中关闭它?底部的参考指南说:

Can I include spring-boot-admin into my business application?

tl;dr You can, but you shouldn’t. You can set spring.boot.admin.context-path to alter the path where the UI and REST-api is served, but depending on the complexity of your application you might get in trouble. On the other hand in my opinion it makes no sense for an application to monitor itself. In case your application goes down your monitioring tool also does.

我的意思是你不应该在生产中使用它。因此,如果您不能使用 spring 启动管理员来监控生产中的应用程序,那又有什么意义呢?是否有 2 个应用程序的解决方案,1 个是您的业务应用程序,另一个是监控业务应用程序的监控应用程序?

  1. So am I registering my application as the server and the client?

在你的例子中,你做到了。管理服务器是其自身的客户端不是问题。事实上,spring-boot-admin-sample 就是这样配置的,因此您可以获得有关管理服务器本身的信息。

  1. How do I run this application and access the admin page? They don't mention any URL to go to to see the admin page. Is it just: http://localhost:8080?

是的。如果您不设置 spring.boot.admin.context-path 管理员将在 root 上提供服务。因此,如果您在 business-app 中运送管理员,您应该配置 spring.boot.admin.context-path 以便它在其他地方提供服务。

  1. How do I set this up for development but turn it off in production? There reference guide at the bottom says: ...

关键是要使用两个单独的应用程序。这就是我们从 dev- 到 qa- 到 production-stage 的方式。我们总是将业务与 admin-server 分开。如果你想在你的 business-application 中启用 admin-server 有条件地尝试这种方法: 写入仅在特定配置文件中有效的 @Configuration-class,并将 @EnableAdminServer 移动到此配置。

希望这对您有所帮助。

我不认为建议不要在生产中使用 Spring 引导管理进行监控,当然,在你确保内置了某种级别的安全性之后。

SBAP 的正确使用模式是将其视为一个独立的应用程序,通过 GUI 提供所有 Spring 启动服务的聚合视图。事实上,只要 HEALTH/METRICS url 通过 ACTUATOR 公开,MONITORED 服务甚至不需要知道有一些应用程序正在监视它的状态。

SBAP 在从像 EUREKA 这样的服务注册中心发现您的服务后,可以从标准端点提取所有 METRICS,因为这完全将 SBAP 与静态感知除 EUREKA 本身以外的任何服务分离。示例 YAML 配置和 JAVA 代码如下

@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
public class SpringbootAdminApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringbootAdminApplication.class, args);
   }
}

bootstrap.yml

  eureka:
    instance:
       leaseRenewalIntervalInSeconds: 10
    client:
      registerWithEureka: false
      fetchRegistry: true
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/
  spring:
    boot:
     admin:
       url: http://localhost:8080
    cloud:
      config:
        enabled: false

在Spring启动项目中单独使用上述2个配置,它将成为一个SBAP服务器,可以通过“http://localhost:8080”访问并监控您的服务。