Spring Boot Admin 监控告警

Spring Boot Admin 监控告警

要进行监控,需要两个Project,一个是Admin Server端,负责监控Spring boot的项目,另一个是Admin Client端,是被监控的Spring boot服务。当然也可以一个项目同时做服务端和客户端,监控自己的本项目服务;

1.这里提供的是两个服务,首先新建一个server端的springboot项目

在这里插入图片描述

2.server端pom文件配置

<?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 https://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.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>spring-boot-admin-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-admin-server</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>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.2.2</version>
		</dependency>
		<!--监控-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>

         <dependency>
             <groupId>de.codecentric</groupId>
             <artifactId>spring-boot-admin-server-ui</artifactId>
             <version>2.2.2</version>
         </dependency>

	</dependencies>

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

</project>

3.server端application.yml文件配置

 spring:
   application:
     name: admin-server
 server:
   port: 9090

4.server端启动类添加注解 @EnableAdminServer

@EnableAdminServer

5.server端启动项目

在这里插入图片描述

6.新建client端,一个springboot项目即可

pom文件

<?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 https://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.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>spring-boot-admin-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-admin-server</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>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.2.2</version>
		</dependency>

	</dependencies>

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

</project>

7.client端yml文件配置

server:
  port: 8800
spring:
  boot:
    admin:
      client:
        url: http://localhost:9090

8.client端然后启动,查看监听

在这里插入图片描述

配置调用admin的监控接口,实现判断

-达到百分之多少的时候会触发自定义报警消息提示,可配置到配置文件中

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.face.ele.common.utils.IpInfoUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author weijianxing
 * @description: TODO
 * @date 2021/1/21 15:38
 */
@RestController
@RequestMapping("xboot/jvmMemory")
@Slf4j
public class JvmWarnMonitor {

	@Autowired
	private IpInfoUtil ipInfoUtil;

	@Value("${server.port}")
	private String WARN_PORT;
	@Value("${xboot.jvmMemoryLimit}")
	private double jvmMemoryLimit;

	@GetMapping("/getJvmMemory")
	@ApiOperation("获取jvm内存情况")
	public boolean getJvmMemory() throws IOException{
		InetAddress address=InetAddress.getLocalHost();
		//System.out.println(address.toString().split("/")[1]);
		String WARN_IP= address.toString().split("/")[1];//ipInfoUtil.getIpAddr(request);
		//JVM最大内存
		final String MAX_MEMORY = "/xboot/actuator/metrics/jvm.memory.max";
		//JVM已使用的内存
		final String USED_MEMORY = "/xboot/actuator/metrics/jvm.memory.used";
		//可供JVM用的内存
		final String FREE_MEMORY = "/xboot/actuator/metrics/jvm.memory.committed";

		//拼接接口路径地址
		String host1="http://"+WARN_IP+":"+WARN_PORT+"/"+MAX_MEMORY;
		String host2="http://"+WARN_IP+":"+WARN_PORT+"/"+USED_MEMORY;
		String host3="http://"+WARN_IP+":"+WARN_PORT+"/"+FREE_MEMORY;

		//拿到MAX_MEMORY
		Object model1=JvmWarnMonitor.jvmMsg(host1);
		Map<String,List<Map>> map=(Map)model1;
		String maxMemory=map.get("measurements").get(0).get("value").toString();

		//拿到USED_MEMORY
		Object model2=JvmWarnMonitor.jvmMsg(host2);
		Map<String,List<Map>> map2=(Map)model2;
		String usedMemory=map2.get("measurements").get(0).get("value").toString();

		//FREE_MEMORY
		Object model3=JvmWarnMonitor.jvmMsg(host3);
		Map<String,List<Map>> map3=(Map)model3;
		String freeMemory=map3.get("measurements").get(0).get("value").toString();

		//判断当jvm内存达到90% 进行报警
		double used= Double.parseDouble(usedMemory) ;
		double max= Double.parseDouble(maxMemory)*(jvmMemoryLimit/100) ;

		//然后开始判断,如果有超过的
		if(used>=max){
			log.info("内存超出"+jvmMemoryLimit+"%");
			log.info("maxMemory"+maxMemory);
			log.info("usedMemory"+usedMemory);
			log.info("freeMemory"+freeMemory);
			return true;
		}else{
			return false;
		}

	}

	/**
	 * 获得所有报警类别
	 * @return
	 * @throws IOException
	 */
	public static Object jvmMsg(String url) throws IOException{

		RequestBuilder builder = RequestBuilder.get().setUri(url);

		//HttpUriRequest httpRequest = builder.setEntity(new StringEntity(JSON.toJSONString(requestObject), "utf-8")).build();
		//请求设置
		HttpUriRequest httpRequest = builder.setEntity(new StringEntity("", "utf-8")).build();
		//建立请求连接
		CloseableHttpClient httpclient = HttpClients.custom().build();
		CloseableHttpResponse response = httpclient.execute(httpRequest);
		HttpEntity entity = response.getEntity();
		//返回结果信息
		byte[] data = EntityUtils.toByteArray(entity);
		//返回到页面结果
		Object obj=JSON.parse(new String(data));
		return obj;
	}



	@GetMapping("/testMemory")
	@ApiOperation("测试jvm_heap溢出")
	public String testMemory() throws IOException {
		int i=0;
		ArrayList<byte[]> a=new ArrayList<byte[]>();
		//调用是否超出规定范围,满足了就不要死循环了
		boolean flag=this.getJvmMemory();
		while (!flag) {
			try {
				boolean flag2=this.getJvmMemory();
				if(flag2){
					break;
				}
				Thread.sleep(500);
				a.add(new byte[1024*1024]);
				i++;
				System.out.println("添加"+i+"M");

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		log.info("已经溢出,结束测试");
		return "已经溢出,结束测试;已添加"+i+"M";
	}


	public static void main(String[] args) throws IOException {
		/*String usedMemory="5.619843071E9";
		String maxMemory="10.123";
		double jvmMemoryLimit=50;
		double used= Double.parseDouble(usedMemory) ;
		double max= Double.parseDouble(maxMemory)* (jvmMemoryLimit/100);

		//然后开始判断,如果有超过的
		if(used>=max){
			System.out.println("超出");
		}*/
		/*InetAddress address=InetAddress.getLocalHost();
		System.out.println(address.toString().split("/")[1]);*/


		/*//JVM最大内存
		final String MAX_MEMORY = "/xboot/actuator/metrics/jvm.memory.max";
		//JVM已使用的内存
		final String USED_MEMORY = "/xboot/actuator/metrics/jvm.memory.used";
		//可供JVM用的内存
		final String FREE_MEMORY = "/xboot/actuator/metrics/jvm.memory.committed";

		//拼接接口路径地址
		String host1="http://127.0.0.1:8877/"+MAX_MEMORY;
		String host2="http://127.0.0.1:8877/"+USED_MEMORY;
		String host3="http://127.0.0.1:8877/"+FREE_MEMORY;
		//拿到MAX_MEMORY
		Object model1=JvmWarnMonitor.jvmMsg(host1);
		Map<String,List<Map>> map=(Map)model1;

		Object maxMemory=map.get("measurements").get(0).get("value");
		System.out.println("maxMemory"+maxMemory);
		//拿到USED_MEMORY
		Object model2=JvmWarnMonitor.jvmMsg(host2);
		Map<String,List<Map>> map2=(Map)model2;

		Object usedMemory=map2.get("measurements").get(0).get("value");
		System.out.println("usedMemory"+usedMemory);
		//FREE_MEMORY
		Object model3=JvmWarnMonitor.jvmMsg(host3);
		Map<String,List<Map>> map3=(Map)model3;

		Object freeMemory=map3.get("measurements").get(0).get("value");
		System.out.println("freeMemory"+freeMemory);*/
	}
}

配置的yml文件

jvmMemoryLimit: 50 #超出最大值的百分比

可以调用测试jvm内存溢出的接口(上面代码)
http://localhost:8877/xboot/jvmMemory/testMemory 即可
然后在页面不断刷新 http://localhost:8877/xboot/getJvmMemory 接口可查出是否报警

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值