溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot-Admin如何實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警

發(fā)布時間:2021-10-29 17:12:26 來源:億速云 閱讀:394 作者:小新 欄目:開發(fā)技術

小編給大家分享一下SpringBoot-Admin如何實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

基于SpringCloud微服務平臺,進行服務實例監(jiān)控及健康檢查,注冊中心為eureka,SpringBoot提供了很好的組件SpringBoot Admin,2.X版本直接可以配置釘釘機器人告警。

效果:可以實現(xiàn)eureka注冊的實例上線、下線觸發(fā)釘釘告警。監(jiān)控我們的服務實例健康檢查。

搭建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監(jiān)控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如何實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警

SpringBoot-Admin如何實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警

SpringBoot-Admin如何實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警

SpringBoot-Admin如何實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警

看完了這篇文章,相信你對“SpringBoot-Admin如何實現(xiàn)微服務監(jiān)控+健康檢查+釘釘告警”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI