SpringBoot-Admin实现微服务监控+健康检查+钉钉告警
作者:一张船票 发布时间:2021-12-23 02:40:22
标签:SpringBoot,Admin,微服务监控,健康检查,钉钉告警
基于SpringCloud微服务平台,进行服务实例监控及健康检查,注册中心为eureka,SpringBoot提供了很好的组件SpringBoot Admin,2.X版本直接可以配置钉钉机器人告警。
效果:可以实现eureka注册的实例上线、下线触发钉钉告警。监控我们的服务实例健康检查。
搭建admin-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.4.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>admin-server</artifactId>
<version>1.0.0</version>
<name>etc-admin-server</name>
<description>Spring Boot Admin监控eureka服务实例和健康检查,钉钉告警</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.4.3</spring-boot-admin.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置
spring:
application:
name: admin-server
security:
user:
name: "admin"
password: "pwd"
boot:
admin:
notify:
dingtalk:
enabled: true
webhookUrl: 'https://oapi.dingtalk.com/robot/send?access_token=钉钉机器人access_token'
secret: '钉钉机器人secret'
message: '服务告警: #{instance.registration.name} #{instance.id} is #{event.statusInfo.status}'
server:
port: 9002
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: 'http://127.0.0.1:8020/eureka/'
instance:
hostname: ${spring.cloud.client.ip-address}
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
ip-address: ${spring.cloud.client.ip-address}
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
启动类
package com.example;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author xxx
*/
@EnableAdminServer
@EnableDiscoveryClient
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
config类
package com.example;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
/**
* WebSecurity配置
* @author xxxx
*/
@Configuration
public class WebSecurityConfigure extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public WebSecurityConfigure(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
// @formatter:on
}
}
启动后效果
来源:https://blog.csdn.net/XinTeng2012/article/details/120988806


猜你喜欢
- 引言:上一专题介绍了下编译器是如何来翻译委托的,从中间语言的角度去看委托,希望可以帮助大家进一步的理解委托,然而之前的介绍都是委托只是封装一
- Spring Cloud Feign简介 Spring Cloud Feign也是一个基础工具类,它整合了Spring Cloud Ribb
- 本文实例为大家分享了Android自动播放Banner图片轮播的具体代码,供大家参考,具体内容如下先看一下效果图支持本地图片以及网络图片or
- 第一个Lambda表达式在Lambda出现之前,如果我们需要写一个多线程可能需要下面这种方式:Runnable runnable = new
- 工作笔记(在不知道json的key时如何获取当前json的keys)String json="{'name':
- 1,如今NestedScrolling运用到很多地方了,要想好看一点的滑动变换,基本上就是使用这个来完成的,让我们来简单的了解一下。2,Ne
- 以前面试的时候经常会碰到这样的问题.,叫你写一下ArrayList.LinkedList.Vector三者之间的区别与联系:原先一直搞不明白
- 前面一篇文章实现了使用ViewPager实现 * launcher拖动效果 ,后来很多朋友问能不能实现左右循环滑动效果和引导页面。今天实现了左
- 什么是Handler Handler是Android消息机制的上层接口,它为我们封装了许多底层的细节,让
- 本文实例讲述了Java基于swing实现的弹球游戏代码。分享给大家供大家参考。主要功能代码如下:package Game;import ja
- 前言:根据ThreadPoolExecutor的构造方法,JDK提供了很多工厂方法来创建各种用途的线程池.1 newFixedThreadP
- package com.yao;import java.util.concurrent.ExecutorService;import jav
- 一.关于数组的特点1.在Java中,无论使用数组或集合,都有边界检查。如果越界操作就会得到一个RuntimeException异常。2.数组
- 本文实例讲述了Android实现打开各种文件的intent方法。分享给大家供大家参考,具体如下:import android.app.Act
- 我们经常需要对我们的开发的软件做各种测试, 软件对系统资源的使用情况更是不可少, 目前有多个监控工具, 相比JProfiler对系统资源尤其
- 总结:对应某个类的实例化的对象tc, 遍历获取所有属性(子成员)的方法(采用反射):Type t = tc.GetType();//获得该类
- WPF 窗体设置亚克力效果框架使用大于等于.NET40。Visual Studio 2022。项目使用 MIT 开源许可
- 实现效果:Form1.cs代码:using System;using System.Collections.Generic;using Sy
- 很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换。下面Android123给大家两种比较简单高效的方法。
- 本文实例讲述了C++双向循环列表用法。分享给大家供大家参考。具体如下:/* 双向循环链表 */#include <iostream&