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

作者:一张船票 时间:2021-12-23 02:40:22 

基于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实现微服务监控+健康检查+钉钉告警

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

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

来源:https://blog.csdn.net/XinTeng2012/article/details/120988806

标签:SpringBoot,Admin,微服务监控,健康检查,钉钉告警
0
投稿

猜你喜欢

  • Winform让DataGridView左侧显示图片

    2021-09-24 03:50:38
  • ehcache模糊批量移除缓存的方法

    2023-01-11 12:30:37
  • C#实现图形界面的时钟

    2022-10-03 15:10:50
  • Flutter状态管理Bloc使用示例详解

    2023-08-24 09:09:10
  • SpringBoot借助spring.factories文件跨模块实例化Bean

    2021-12-01 18:22:41
  • Android应用程序保持后台唤醒(使用WakeLock实现)

    2022-07-03 07:46:13
  • c#动态类型,及动态对象的创建,合并2个对象,map实例

    2023-04-28 17:40:12
  • SpringBoot使用SchedulingConfigurer实现多个定时任务多机器部署问题(推荐)

    2021-09-17 07:19:20
  • springboot中validator数据校验功能的实现

    2021-07-31 17:43:50
  • C++实现LeetCode(159.最多有两个不同字符的最长子串)

    2023-06-20 22:39:46
  • 详述IntelliJ IDEA插件的安装及使用方法(图解)

    2023-11-26 04:45:06
  • java 注解默认值操作

    2023-08-25 20:31:38
  • 关于Java中Json的各种处理

    2022-06-12 02:37:48
  • Android 使用FragmentTabhost代替Tabhost

    2021-09-10 19:10:26
  • 使用Java实现类似Comet风格的web app

    2023-04-01 10:23:22
  • Android CalendarView,DatePicker,TimePicker,以及NumberPicker的使用

    2022-09-08 18:56:36
  • Java 开发的几个注意点总结

    2021-11-30 20:07:11
  • C#设置开机启动项、取消开机启动项

    2023-02-19 19:40:32
  • JSch教程使用sftp协议实现服务器文件载操作

    2023-10-29 17:43:33
  • 简单了解Java方法的定义和使用实现详解

    2023-10-30 16:12:46
  • asp之家 软件编程 m.aspxhome.com