SpringBoot邮件监控报警

Lou.Chen
大约 3 分钟

SpringBoot邮件监控报警

但我们要用SBA(Spring-Boot-Admin)实现应用的监控并实时了解应用的上下线时,我们可以使用邮件报警的方式

一、服务端配置

1、pom.xml

spring-boot-admin-starter-server

spring-boot-starter-mail

<?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.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.lc</groupId>
    <artifactId>admin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>admin</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、properties配置
#指定邮件服务器的地址 这里我们使用qq的smtp服务器发送邮件
spring.mail.host=smtp.qq.com
# 服务器的端口号修改成465或587;
spring.mail.port=587
# 邮件发送者的邮件
spring.mail.username=421192425@qq.com
# 这里使用的为授权码(不能使用邮箱密码)
spring.mail.password=eqsdqizwdnenbgif
#设置默认编码
spring.mail.default-encoding=UTF-8
#设置ssl协议
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
#打印发送邮件的日志
spring.mail.properties.mail.debug=true


#邮件发送给谁
spring.boot.admin.notify.mail.to=421192425@qq.com
#谁发的
spring.boot.admin.notify.mail.from=421192425@qq.com
#忽略的事件(这里没有配置,则应用的上线和下线都要发送邮件)
spring.boot.admin.notify.mail.ignore-changes=

3、开启admin服务
@SpringBootApplication
//开启admin服务
@EnableAdminServer
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}

二、客户端配置(监控的应用)

1、pom.xml

spring-boot-starter-actuator

spring-boot-admin-starter-client

<?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.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.lc</groupId>
    <artifactId>client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <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>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、客户端信息配置
#暴露所有端口信息
management.endpoints.web.exposure.include=*
server.port=8081
#声明该客户端的url地址
spring.boot.admin.client.url=http://localhost:8080
3、启动应用

三、应用上下线邮件提示

1、上线邮件模板

spring-boot-application (65685b0ef8f8) is UP Instance 65685b0ef8f8 changed status from UNKNOWN to UP

Status Details Registration Service Url http://LAPTOP-LJJM9R29:8081/open in new window Health Url http://LAPTOP-LJJM9R29:8081/actuator/healthopen in new window Management Url http://LAPTOP-LJJM9R29:8081/actuatoropen in new window

2、下线邮件模板

spring-boot-application (65685b0ef8f8) is OFFLINE Instance 65685b0ef8f8 changed status from UP to OFFLINE

Status Details exception io.netty.channel.AbstractChannel$AnnotatedConnectException message Connection refused: no further information: LAPTOP-LJJM9R29/169.254.1.137:8081 Registration Service Url http://LAPTOP-LJJM9R29:8081/open in new window Health Url http://LAPTOP-LJJM9R29:8081/actuator/healthopen in new window Management Url http://LAPTOP-LJJM9R29:8081/actuatoropen in new window