当前位置:   article > 正文

06. Springboot admin集成Actuator(二)_actuator配置 邮件

actuator配置 邮件

目录

1、前言

2、快速使用

2.1、服务端集成

2.1.1、添加依赖

2.1.2、配置启动类

2.1.3、配置application.yml

2.1.4、定制security config

2.1.5、启动程序

2.2、客户端集成

2.2.1、添加依赖

2.2.2、配置application.yml

2.2.3、启动程序

2.3、告警通知

2.3.1、邮件通知

2.3.2、Notifier 接口


1、前言

在《Springboot admin集成Actuator(一)》一文中简单演示了Actuator的初步使用,但是可以发现都是json形式返回的结构,那么是否有能够可视化的方式来展示这些指标呢?当然有,就是接下来要说的Springboot Admin。Spring Boot Admin能够将 Actuator 中的信息进行界面化的展示,也可以监控所有 Spring Boot 应用的健康状况,提供实时警报功能。

注:虽然名叫Springboot Admin,但却不是Spring团队研发的。而是由Codecentric公司创建的,代码在Github: spring-boot-admin在新窗口打开上。

2、快速使用

springboot admin分为服务端(spring-boot-admin-server)和客户端(spring-boot-admin-client)。服务端和客户端之间采用 http 通讯方式实现数据交互;单体项目中需要整合 spring-boot-admin-client 才能让应用被监控。在 SpringCloud 项目中,spring-boot-admin-server 是直接从注册中心抓取应用信息,不需要每个微服务应用整合 spring-boot-admin-client 就可以实现应用的管理和监控。

2.1、服务端集成

2.1.1、添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <!-- 添加security模块,可以对spring boot admin设置登录账号密码,添加安全性 -->
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-security</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>de.codecentric</groupId>
  12. <artifactId>spring-boot-admin-starter-server</artifactId>
  13. <version>${spring.boot.admin.version}</version>
  14. </dependency>

2.1.2、配置启动类

启动类需要添加注解@EnableAdminServer。

  1. @EnableAdminServer
  2. @SpringBootApplication
  3. public class SbaServerLauncher {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SbaServerLauncher.class, args);
  6. }
  7. }

2.1.3、配置application.yml

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: sba-server
  6. # 配置登录springboot admin管理端的账号密码
  7. security:
  8. user:
  9. name: admin
  10. password: 123456
  11. # 启动actuator端点
  12. management:
  13. endpoints:
  14. web:
  15. exposure:
  16. include: '*'
  17. endpoint:
  18. health:
  19. show-details: always

2.1.4、定制security config

  1. @Configuration
  2. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  3. private final String adminContextPath;
  4. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  5. this.adminContextPath = adminServerProperties.getContextPath();
  6. }
  7. @Override
  8. protected void configure(HttpSecurity http) throws Exception {
  9. // 登录成功处理类
  10. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  11. successHandler.setTargetUrlParameter("redirectTo");
  12. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  13. http.authorizeRequests()
  14. //静态文件允许访问
  15. .antMatchers(adminContextPath + "/assets/**").permitAll()
  16. //登录页面允许访问
  17. .antMatchers(adminContextPath + "/login", "/css/**", "/js/**", "/image/*").permitAll()
  18. //其他所有请求需要登录
  19. .anyRequest().authenticated()
  20. .and()
  21. //登录页面配置,用于替换security默认页面
  22. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
  23. //登出页面配置,用于替换security默认页面
  24. .logout().logoutUrl(adminContextPath + "/logout").and()
  25. .httpBasic().and()
  26. .csrf()
  27. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  28. .ignoringAntMatchers(
  29. "/instances",
  30. "/actuator/**"
  31. );
  32. }
  33. }

2.1.5、启动程序

接下来就可以启动程序。访问地址http://localhost:9001

输入账号密码admin/123456,进入控制台首页,服务端集成到此就好了。

2.2、客户端集成

服务端集成后,我们发现其应用数为0。这里的应用就是需要我们客户端集成后注册进去。

2.2.1、添加依赖

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-client</artifactId>
  4. <version>${spring.boot.admin.version}</version>
  5. </dependency>

2.2.2、配置application.yml

  1. server:
  2. port: 9001
  3. spring:
  4. boot:
  5. admin:
  6. client:
  7. # 这里就是注册服务端的地址
  8. url: http://localhost:9001
  9. username: ${spring.security.user.name}
  10. password: ${spring.security.user.password}
  11. instance:
  12. service-host-type: ip
  13. metadata:
  14. user.name: ${spring.security.user.name}
  15. user.password: ${spring.security.user.password}

2.2.3、启动程序

启动程序,访问下控制台地址:http://localhost:9001,可以发现我们的服务已经注册上去了。因为我这边服务端和客户端是在同一套工程上,因此可以看到我们自己的应用服务。

点击sba-server应用,可以进去详情查看各个端点信息:

这些信息其实就是前面介绍的actuator各个端点的数据以可视化方式呈现。到此我们的集成就算是完成了。

除此之外,我们还可以自定义springboot admin的导航菜单,通过view-setting配置。以及其他一些可扩展的东西,这里就不赘述,需要感兴趣的小伙伴们慢慢探索。

2.3、告警通知

当检测到状态发生变化时, 我们需要发送一些消息通知。sba-server中内置了如email、消息等通知。

2.3.1、邮件通知

我们以为发送email为例,需要添加如下配置:

添加依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

添加email配置:

  1. spring:
  2. mail:
  3. host: ${MAIL_HOST:邮箱服务器地址}
  4. port:
  5. username: ${MAIL_USERNAME:邮箱服务器用户名}
  6. password: ${MAIL_PWD:邮箱服务器密码}
  7. protocol: ${MAIL_PROTOCOL:smtp}
  8. default-encoding: UTF-8
  9. properties:
  10. mail.smtp.auth: true
  11. mail.smtp.starttls.enable: true
  12. mail.smtp.starttls.required: true
  13. mail.smtp.socketFactory.port: ${MAIL_SMTP_SOCKETFACTORY_PORT:465}
  14. mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
  15. mail.smtp.socketFactory.fallback: false
  16. mail.smtp.ssl.protocols: ${MAIL_SMTP_SSL_PROTOCOLS:TLSv1}

添加spring boot admin邮件告警配置:

  1. spring:
  2. boot:
  3. admin:
  4. notify:
  5. mail:
  6. to: ${NOTIFY_MAIL_TO:邮箱接收人,多个用,隔开}
  7. from: ${NOTIFY_MAIL_FROM:邮箱发送人}

配置完,当客户端出现异常时,就会收到邮件告警。

2.3.2、Notifier 接口

除了内置的一些通知类型外,还可以自定义通知功能。自定义的通知可以实现 AbstractStatusChangeNotifier 抽象类,或者 Notifier 接口。我们以继承AbstractStatusChangeNotifier类为例,使用Notifier接口类似:

  1. public class CustomNotifier extends AbstractStatusChangeNotifier {
  2. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/931550
推荐阅读
相关标签
  

闽ICP备14008679号