SpringBoot | 第八章:搭建Spring Boot Admin分布式微服务监控中心

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Thinkingcao/article/details/84339398

(一)、什么是Spring Boot Admin

      Spring Boot Admin 是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。他可以返回在列表中浏览所有被监控spring-boot项目的基本信息比如:Spring容器管理的所有的bean、详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,Threads 线程管理,Environment 管理等。

(二)、Spring Boot Admin 是由Client端和Server端组成

  在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的要被监控的应用作为 Client 端, 基于这种的配置如下步骤:

 2.1 搭建Server端

  2.1.1 新建一个Maven项目,目录结构如下

   

 2.1.2 引入Maven依赖

    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<!-- Spring Boot Actuator对外暴露应用的监控信息,Jolokia提供使用HTTP接口获取JSON格式 的数据 -->
		<dependency>
			<groupId>org.jolokia</groupId>
			<artifactId>jolokia-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
			<version>1.1</version>
		</dependency>
	</dependencies>

2.1.3 编辑yml文件

   application.yml

spring:
  application:
    name: spring-boot-admin-server

    
## 自定义info信息
info:
   thinkingcao: 
     name: PHP是最好的语言

2.1.4 Server启动类添加注解,开启监控

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import de.codecentric.boot.admin.server.config.EnableAdminServer;

/**
 * <pre>
 * @author cao_wencao
 * @date 2018年11月22日 上午10:36:11
 * </pre>
 */
@SpringBootApplication
@EnableAdminServer
@EnableAutoConfiguration
public class AdminServerApplication {

	/**
	 * <pre>  
	 * @author cao_wencao
	 * @param args
	 * </pre>  
	 */
	public static void main(String[] args) {
		SpringApplication.run(AdminServerApplication.class, args);
	}

}

2.1.5 启动查看console 控制台

 只有3个接口的监控权限

2.1.6 访问Server端查看,0个实例

2.2  搭建Client端

 2.2.1 新建一个Maven项目,目录结构如下

2.2.2 引入Maven依赖

 <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jolokia</groupId>
			<artifactId>jolokia-core</artifactId>
		</dependency>
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

2.2.3 编辑yml文件

   application.yml

## 将Client作为服务注册到Server,通过Server来监听项目的运行情况
spring:
  boot:
    admin:
      client:
        url: http://localhost:8080
 ## application实例名       
  application:
    name : spring-boot-admin-client
 
server:
  port: 8081

## 启用所有端点的监控权限
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

2.1.4 Client启动类添加注解

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

/**
 * <pre>
 * @author cao_wencao
 * @date 2018年11月22日 上午10:44:36
 * </pre>
 */
@SpringBootApplication
public class AdminClientApplication {

	/**
	 * <pre>  
	 * @author cao_wencao
	 * @param args
	 * </pre>  
	 */
	public static void main(String[] args) {
		SpringApplication.run(AdminClientApplication.class, args);
	}


}

2.2.5 启动查看console 控制台,拥有19个监控权限

2.2.6 先启动完Server端发现没有实例注册时,在启动完Client后,再次访问Server端,查看监控接口情况

访问/actuator/info接口,查看自定义配置文件Info信息

上图清晰的展示了Client端的服务注册到被Server监控的监控中心去了,并且可以查看详细的堆栈、线程等信息,类似Jconsole

项目源码:https://github.com/Thinkingcao/SpringBoot-AdminUI

猜你喜欢

转载自blog.csdn.net/Thinkingcao/article/details/84339398