SpringBoot-Admin实现微服务监控+健康检查+钉钉告警

 更新时间:2021年10月29日 15:21:03   作者:一张船票  
本文主要介绍了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
    }
}

启动后效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此这篇关于SpringBoot-Admin实现微服务监控+健康检查+钉钉告警的文章就介绍到这了,更多相关SpringBoot-Admin 微服务监控内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JAVA多线程和并发基础面试问答(翻译)

    JAVA多线程和并发基础面试问答(翻译)

    多线程和并发问题是Java技术面试中面试官比较喜欢问的问题之一。在这里,从面试的角度列出了大部分重要的问题,但是你仍然应该牢固的掌握Java多线程基础知识来对应日后碰到的问题
    2014-09-09
  • Java实现两人五子棋游戏(六) 行棋方变换

    Java实现两人五子棋游戏(六) 行棋方变换

    这篇文章主要为大家详细介绍了Java实现一个简单的两人五子棋游戏,行棋方变换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Spring Boot全局统一异常处理器

    Spring Boot全局统一异常处理器

    软件开发springboot项目过程中,不仅有大量的冗余代码,而且还影响代码的可读性.这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常.文中有非常详细的代码示例,需要的朋友可以参考下
    2021-05-05
  • Spring Boot面试题总结

    Spring Boot面试题总结

    这篇文章主要介绍了Spring Boot面试题总结,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • Java实现LeetCode(1.两数之和)

    Java实现LeetCode(1.两数之和)

    这篇文章主要介绍了Java实现LeetCode(两数之和),本文使用java采用多种发放实现了LeetCode的两数之和题目,需要的朋友可以参考下
    2021-06-06
  • Spring Boot小型项目如何使用异步任务管理器实现不同业务间的解耦

    Spring Boot小型项目如何使用异步任务管理器实现不同业务间的解耦

    这篇文章主要介绍了Spring Boot小型项目如何使用异步任务管理器实现不同业务间的解耦,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • springBoot启动输出三行日志控制台自动停止操作

    springBoot启动输出三行日志控制台自动停止操作

    这篇文章主要介绍了springBoot启动输出三行日志控制台自动停止操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java 设置Excel条件格式示例代码(高亮条件值、应用单元格值/公式/数据条等类型)

    Java 设置Excel条件格式示例代码(高亮条件值、应用单元格值/公式/数据条等类型)

    这篇文章主要介绍了Java 设置Excel条件格式示例代码(高亮条件值、应用单元格值/公式/数据条等类型),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • MyBatis获取插入记录的自增长字段值(ID)

    MyBatis获取插入记录的自增长字段值(ID)

    本文分步骤给大家介绍了MyBatis获取插入记录的自增长字段值的方法,在文中给大家提到了mybatis返回插入数据的自增长id,需要的朋友可以参考下
    2017-11-11
  • 详解Java动态字节码技术

    详解Java动态字节码技术

    Java字节码增强指的是在Java字节码生成之后,对其进行修改,增强其功能,可减少冗余代码,提高性能等。本文将详细介绍Java动态字节码技术。
    2021-05-05

最新评论