0. 前言
在之前的几章中,我们先搭建了一个项目骨架,又搭建了一个使用nacos的gateway网关项目,网关项目中并没有设置太多的器械。现在我们就接着搭建在Spring Cloud 微服务中另一个主要的项目 - Spring boot admin.
1. Spring Boot Admin 先容
Spring Boot Admin 用来监控基于Spring Boot的应用,在Spring Boot Actuator的基础上提供了简练的可视化Web UI。Spring Boot Admin 提供了以下功效:
- 显示应用的康健状态
- 显示应用的细节内容: JVM和内存信息,micrometer信息, 数据源信息,缓存信息等
- 显示 编译版本
- 查看和下载日志
- 查看jvm参数和环境变量值
- 查看Spring Boot项目设置
- 显示 thread dump
- 显示 http-traces
……
等一系列内容。
2. 确立一个 Spring Boot Admin项目
那么,我们就来确立一个Spring Boot Admin 项目吧。
2.1 确立 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);
}
}
启动后能看到如下界面:
3 与网关服务举行互通
在上一篇中,我们添加了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 进去后可以查看到:
4. 总结
我们搭建了一个Spring Boot Admin 项目作为一个监控系统,后续会在这里添加更多的内容。
更多内容烦请关注我的博客《高先生小屋》
欢迎进入AllbetGmaing客户端下载(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。
www.allbetgaming.net欢迎进入欧博平台网站(www.aLLbetgame.us),www.aLLbetgame.us开放欧博平台网址、欧博注册、欧博APP下载、欧博客户端下载、欧博游戏等业务。最强网文,赞!