springboot实战笔记(十五)---- 监控 Spring Boot 的健康状况

版权声明:转载请注明出处 https://blog.csdn.net/qq_33223299 https://blog.csdn.net/qq_33223299/article/details/90740053

一 使用 Actuator 检查与监控

   1.1   创建项目,添加Actuator依赖,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>1.5.21.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.chp</groupId>
	<artifactId>springboot-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<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-actuator</artifactId>
		</dependency>
	</dependencies>

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

</project>

1.2  在全局配置文件中设置关闭安全限制(默认是开启状态)

management.security.enabled=false

1.3 启动测试

启动项目,根据上面你所想要的信息,输入地址,比如我想要知道env环境变量的信息 ,那么我就输入如图:

这样看起来挺废劲,可以通过百度下json解析,来格式化这些数据,使看起来更加直观。

二 可视化的监控报表-Spring BootAdmin

   相比上面的方式,这种方式更加直观,因为他是使用了可视化界面。

2.1 创建服务端

   服务端其实也是springboot项目,

pom文件添加下面的依赖:

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>1.5.7</version>
</dependency>

具体的springboot-admin可以访问下面的网址:

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

2.2 在启动类中添加@EnableAdminServer注解

@SpringBootApplication
@EnableAdminServer
public class SpringbootAdminServerApplication {

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

}

2.3 创建客户端项目

   客户端项目也就是我们被监控的项目,创建项目如图:

pom.xml添加依赖

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.7</version>
</dependency>

2.4 配置application.properties文件

management.security.enabled=false
#http://localhost:8888 表示是 Spring Boot Admin 服务端的 IP 地
址以及端口号
spring.boot.admin.url: http://localhost:8888

注意:这里的端口号要去服务端server的保持一致,默认是8080

2.5 启动测试 

     先启动服务端项目,然后再启动客户端项目,都启动成功后,在浏览器中输入:http://localhost:8888

点击 Details

猜你喜欢

转载自blog.csdn.net/qq_33223299/article/details/90740053
今日推荐