在以前的几章中,咱们先搭建了一个项目骨架,又搭建了一个使用nacos的gateway网关项目,网关项目中并无配置太多的东西。如今咱们就接着搭建在Spring Cloud 微服务中另外一个重要的项目 - Spring boot admin.java
Spring Boot Admin 用来监控基于Spring Boot的应用,在Spring Boot Actuator的基础上提供了简洁的可视化Web UI。Spring Boot Admin 提供了如下功能:web
显示应用的健康状态spring
显示应用的细节内容:JVM和内存信息,micrometer信息, 数据源信息,缓存信息等apache
显示 编译版本bootstrap
查看和下载日志缓存
查看jvm参数和环境变量值app
查看Spring Boot项目配置jvm
显示 thread dumpmaven
显示 http-traceside
……
等一系列内容。
那么,咱们就来建立一个Spring Boot Admin 项目吧。
在manager 目录下,建立一个 monitor目录,并在monitor目录下建立一个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>
<artifactId>monitor</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>
<parent>
<artifactId>manager</artifactId>
<groupId>club.attachie</groupId>
<version>${revision}</version>
</parent>
</project>
在 manager/pom.xml 注册咱们新建的项目模块:
<modules>
<module>gateway</module>
<module>monitor</module>
</modules>
在 monitor 建立以下目录:
.
├── pom.xml
└── src
└── main
├── java
└── resources
在根目录的pom.xml 添加 Spring Boot Admin 依赖:
先添加spring-boot-admin版本号变量:
<spring-boot-admin.version>2.2.3</spring-boot-admin.version>
并在dependencyManagement > dependencies 下添加:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
在monitor/pom.xml文件中添加:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
</dependencies>
运行
mvn clean install
检查并刷mvn引用缓存。
建立MonitorApplication类:
package club.attachie.nature.monitor;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
启动后能看到以下界面:
在上一篇中,咱们添加了Spring Cloud Gateway项目,到目前为止两个项目之间彻底割裂没有关联。在这一节,咱们在二者之间创建关联。也就是说,将gateway 项目引入Spring Admin Boot监听。
在 manager/gateway 的pom.xml 文件中加入以下引用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
而后修改 gateway项目的启动端口,在resources/bootstrap.yml 添加:
server:
port: 8070
在 monitor中加入nacos引用:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改MonitorApplication 为:
package club.attachie.nature.monitor;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@SpringBootApplication
@EnableAdminServer
@RefreshScope
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
建立monitor项目的bootsrap.yml:
spring:
application:
name: monitor
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
关于这里的配置 在上一篇 中有个错误,应该是 discovery > server-addr,不是 config > server-addr。二者有区别,discovery表示设置nacos为服务发现中心,config表示nacos为配置中心。
启动 gateway 项目和 monitor项目查看效果, 访问 8080端口:
能够看到两个应用能够被发现,若是没有设置monitor项目把nacos当作服务发现中心,将没法获取到具体在线的应用。点击 gateway 进去后能够查看到:
咱们搭建了一个Spring Boot Admin 项目做为一个监控系统,后续会在这里添加更多的内容。