Spring Boot Admin Page

2020-06-05 01:11发布

问题:

I am trying to understand how to use SBAP in my application because it is a very handy tool for development. I'm reading their reference guide but I'm not understanding a few things. Here is my pom for my application right now:

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

And here is my 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);
    }

}

And my application.yml file:

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

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

The rest I'm very confused about.

  1. So am I registering my application as the server and the client?
  2. 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?
  3. How do I set this up for development but turn it off in production? There reference guide at the bottom says:

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.

I'm taking that to mean you shouldn't have this in production. So if you can't use spring boot admin to monitor your app in production then what is the point? Is the solution to have 2 applications, 1 which is your business application and the other being your monitoring application that monitors the business app?

回答1:

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

In your example, you do. It's not problem that the admin server is a client to itself. In fact the spring-boot-admin-sample is configured this way so you get infos about the admin server itself.

  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?

Yes. If you don't set the spring.boot.admin.context-path the admin is served on root. So if you ship the admin along inside your business-app you should configure spring.boot.admin.context-path so that it's served elsewhere.

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

The point is just to use two separate applications. This is the way we do it from dev-, over qa- to production-stage. We always separate the business- from the admin-server. If you want to enable the admin-server inside your business-application conditionally try this approach: Write an @Configuration-class wich is only active within a certain profile and move the @EnableAdminServer to this configuration.

Hope this helps.



回答2:

I don't think the recommendation is NOT to use Spring boot admin in production for monitoring, ofcourse, after you ensure that there is some level of security that is built in.

The correct usage pattern for SBAP is to think about it as a standalone application that provides an aggregated view of all of your Springboot services via a GUI. Infact, as long as HEALTH/METRICS urls are exposed via ACTUATOR, the MONITORED services do not even need to be aware of the fact there is some application that is monitoring it's status.

SBAP can PULL all METRICS from standard endpoints after it discovers your services from a Service Registry like EUREKA as that totally decouples SBAP from being statically aware of any service other than EUREKA itself. A sample YAML configuration and JAVA code is as follows

@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

With the 2 configurations above alone in a SpringBoot project,it would become a SBAP server that can be accessed via "http://localhost:8080" and monitor your services.