Spring boot admin - 微服务监测demo

因为服务器的不稳定,而且小的服务越来越多,添加一个微服务的监测会更放心一些。

参考:https://github.com/codecentric/spring-boot-admin

  http://codecentric.github.io/spring-boot-admin/2.1.2/#_what_is_spring_boot_admin

首先是用于监测其他服务的Spring boot admin

  1. 按文档说明,最好创建一个新的Project用于监测。创建到选择Dependencies的时候,发现Ops下可以选Spring Boot Admin(Server)

2. 按文档说明,需要几处代码的添加

  • 启动类
package com.cnipr.pt;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAdminServer
@SpringBootApplication //已经包含Configuration和AutoConfiguration
public class MonitorApplication {

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

}
  • application.properties(以下配置为了在微服务状态变化时发送邮件)
spring.mail.host=XXX.123.net
spring.mail.port=25
spring.mail.username=username
spring.mail.password=123456
spring.boot.admin.notify.mail.from=[email protected]
spring.boot.admin.notify.mail.to[email protected]
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cnipr.pt</groupId>
    <artifactId>monitor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>monitor</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.1</spring-boot-admin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

为了在SBA(Spring Boot Admin)中注册服务,要在相应的spring boot应用中添加Spring boot admin client的相关配置。

  • pom.xml(注意!client的version最好和spring-boot-starter-parent的version保持一致,否则可能会出现运行错误)
<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.2</version>
        </dependency>

为了在SBA上查看到版本号,可以在pom.xml中的spring-boot-maven-plugin的配置中添加build-info。

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

application.properties(url指定需要注册的admin的地址; logging.pattern.file的配置可将日志显示为彩色,可不加;name显示服务名称,可不加;prefer-ip为true会显示微服务的ip地址,可不加)

spring.boot.admin.client.instance.name=mail-service
spring.boot.admin.client.instance.prefer-ip = true
spring.boot.admin.client.url=http://localhost:8080 management.endpoints.web.exposure.include=*
logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx

admin显示界面(默认端口为8080)

猜你喜欢

转载自www.cnblogs.com/jane850113/p/10385091.html