spring admin监控整合eureka,邮箱报警

文章介绍了如何创建SpringBootAdminServer和Client,以及它们之间的交互。接着讲解了如何结合Eureka搭建服务注册中心,确保服务发现和管理。最后提到了在SpringBootAdmin中集成邮箱报警功能,以便在服务状态变化时发送通知。
摘要由CSDN通过智能技术生成

1. 创建Spring Boot Admin Server

1.1. pom依赖

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

1.2. 开启 AdminServer

@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run( AdminServerApplication.class, args );
    }

}

在工程的配置文件application.yml中配置程序名和程序的端口,代码如下:

spring:
  application:
    name: admin-server
server:
  port: 8769

这样Admin Server就创建好了。

2. 创建Spring Boot Admin Client

2.1. client pom依赖

<dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.0</version>
        </dependency>
       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

在工程的配置文件application.yml中配置应用名和端口信息,

  • 以及向admin-server注册的地址为http://localhost:8769,最后暴露自己的actuator的所有端口信息,

具体配置如下:

2.2. client yaml配置文件

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8769
server:
  port: 8768

management:
  endpoints:
    web:
      exposure:
        include: '*' #所有端口信息
  endpoint:
    health:
      show-details: ALWAYS

在工程的启动文件如下:

@SpringBootApplication
public class AdminClientApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run( AdminClientApplication.class, args );
    }

一次启动两个工程,在浏览器上输入localhost:8769 ,浏览器显示的界面如下:

img

3. Spring boot Admin结合eureka注册中心使用

3.1. 搭建eureka server

3.1.1. pom依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

3.1.2. 配置文件

spring:
    application:
        name: eureka-server
    #安全验证配置
    security:
        user:
            name: root
            password: 123456
#端口配置
server:
    port: 7001
eureka:
    name: root
    password: 123456
    client:
        region: zsy
        availability-zones:
            zsy: zsy-zone1,zsy-zone2
        #表示是否将自己注册进EurekaServer默认为true
        #如果建高可用的系统,需要设置为true
        fetch-registry: true
        #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
        register-with-eureka: true
        service-url:
            defaultZone: http://${eureka.name}:${eureka.password}@127.0.0.1:7002/eureka/,http://${eureka.name}:${eureka.password}@127.0.0.1:7001/eureka/
    instance:
        hostname: zone1
        #服务续约
        # Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
        lease-renewal-interval-in-seconds: 10
        # Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
        lease-expiration-duration-in-seconds: 20
        # 访问路径可以显示IP地址
        prefer-ip-address: true
        # 主机名:应用名:ip:端口
        instance-id: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
    server:
        # 关闭自我保护机制,保证不可用服务被及时剔除,默认打开
        enable-self-preservation: false
        #        # 失效剔除,单位是毫秒,默认为90秒
        #        eviction-interval-timer-in-ms: 20000
management:
    endpoints:
        web:
            exposure:
                include: '*' #所有端口信息
    endpoint:
        health:
            show-details: ALWAYS

3.1.3. 启动类

在工程的启动文件EurekaServerApplication加上@EnableEurekaServer注解开启Eureka Server.

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run( EurekaServerApplication.class, args );
    }
}

3.1.4. 配置类放开actuator端口

@EnableWebSecurity
public class SecurityConf extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();     //关闭跨域
        http.authorizeRequests()   //认证请求
                .antMatchers("/actuator/**").permitAll();
        super.configure(http);
    }
}

3.2. 搭建admin-server

3.2.1. pom依赖

<artifactId>spring-boot-admin-starter-server</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
   
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2.2. admin server的 yml

spring:
  application:
    name: admin
  security:
    user:
      name: root
      password: 123456
server:
  port: 8000
eureka:
  name: root
  password: 123456
  client:
    region: zsy
    availability-zones:
      zsy: zsy-zone2
    #表示是否将自己注册进EurekaServer默认为true
    #如果建高可用的系统,需要设置为true
    fetch-registry: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    register-with-eureka: true
    service-url:
      defaultZone: http://${eureka.name}:${eureka.password}@127.0.0.1:7002/eureka/,http://${eureka.name}:${eureka.password}@127.0.0.1:7001/eureka/
    instance:
      metadata-map:
        user.name: ${spring.security.user.name}
        user.password: ${spring.security.user.password}
management:
  endpoints:
    web:
      exposure:
        include: "*" #配置这个将监控所有的信息
  endpoint:
    health:
      show-details: ALWAYS

3.2.3. 启动类

@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class AdminApplication {

	public static void main(String[] args) {
		SpringApplication.run(AdminApplication.class, args);
	}

}

3.2.4. security 配置类

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    //注入 adminServerProperties
    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");

        http.authorizeRequests() //assets 和 login 同意
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .antMatchers(adminContextPath + "/actuator/**").permitAll()
                .anyRequest().authenticated() //其他都需要登录
                .and() //登录成功
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf().disable(); //csrf禁用
        // @formatter:on
    }
}

4. 集成邮箱报警功能

在spring boot admin中,也可以集成邮箱报警功能,比如服务不健康了、下线了,都可以给指定邮箱发送邮件。集成非常简单,只需要改造下admin-server即可:

在admin-server工程Pom文件,加上mail的起步依赖,代码如下:

4.1. 引入pom

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

在配置文件application.yml文件中,需要配置邮件相关的配置,如下:

4.2. yaml配置

spring.mail.host: smtp.163.com
spring.mail.username: miles02
spring.mail.password:
spring.boot.admin.notify.mail.to: 124746406@qq.com

做完以上配置后,当我们已注册的客户端的状态从 UP 变为 OFFLINE 或其他状态,服务端就会自动将电子邮件发送到上面配置的地址。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值