转载

SpringBoot 2.x 开发案例之整合Srping Boot Admin

点击▲关注 “ 爪哇笔记 ”   给公众号标星置顶

更多精彩 第一时间直达

SpringBoot 2.x 开发案例之整合Srping Boot Admin

前言

监控我们当然是要可视化的, Spring Boot Admin  就是将  Spring Boot Actuator 中提供的 endpoint 信息可视化表示,并且可以通过邮件、Telegram、Hipchat等发送告警消息。

集成

注意一定要版本对应,否则会出现意想不到的问题,建议使用  Srping Boot Admin  2.0+以上版本,可以多语言切换。

父项目

pom.xml 引入:

  1. <modules>

  2. <module>admin-server</module>

  3. <module>admin-client</module>

  4. </modules>

  5. <parent>

  6. <groupId>org.springframework.boot</groupId>

  7. <artifactId>spring-boot-starter-parent</artifactId>

  8. <version>2.2.2.RELEASE</version>

  9. <relativePath/>

  10. </parent>

  11. <dependencies>

  12. <dependency>

  13. <groupId>org.springframework.boot</groupId>

  14. <artifactId>spring-boot-starter-web</artifactId>

  15. <exclusions>

  16. <exclusion>

  17. <groupId>org.springframework.boot</groupId>

  18. <artifactId>spring-boot-starter-tomcat</artifactId>

  19. </exclusion>

  20. </exclusions>

  21. </dependency>

  22. <dependency>

  23. <groupId>org.springframework.boot</groupId>

  24. <artifactId>spring-boot-starter-jetty</artifactId>

  25. </dependency>

  26. </dependencies>

监控服务端

pom.xml 引入:

<artifactId>admin-server</artifactId>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.1</version>
</dependency>
<!--登录认证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--掉线发送邮件通知-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>

application.properties 配置文件:

  1. # 爪哇笔记:https://blog.52itstyle.vip

  2. server.port=9000

  3. spring.application.name=SpringBootAdmin

  4. spring.security.user.name=admin

  5. spring.security.user.password=admin

  6. spring.boot.admin.monitor.status-interval = 10000

  7. spring.boot.admin.monitor.info-interval = 10000

  8. spring.mail.host = smtp.163.com

  9. spring.mail.username = 13188888888@163.com

  10. spring.mail.password = 2020

  11. spring.boot.admin.notify.mail.from = 13188888888@163.com

  12. spring.boot.admin.notify.mail.to = 88888888@qq.com

启动类:

  1. /**

  2. * 系统监控

  3. * 爪哇笔记:https://blog.52itstyle.vip

  4. */

  5. @Configuration

  6. @EnableAutoConfiguration

  7. @EnableAdminServer

  8. public class Application {

  9. public static void main(String[] args) {

  10. SpringApplication.run(Application.class, args);

  11. }

  12. @Configuration

  13. public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

  14. private final AdminServerProperties adminServer;

  15. public SecuritySecureConfig(AdminServerProperties adminServer) {

  16. this.adminServer = adminServer;

  17. }

  18. @Override

  19. protected void configure(HttpSecurity http) throws Exception {

  20. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();

  21. successHandler.setTargetUrlParameter("redirectTo");

  22. successHandler.setDefaultTargetUrl(this.adminServer.path("/"));

  23. http.authorizeRequests()

  24. .antMatchers(this.adminServer.path("/assets/**")).permitAll()

  25. .antMatchers(this.adminServer.path("/login")).permitAll()

  26. .anyRequest().authenticated()

  27. .and()

  28. .formLogin().loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()

  29. .logout().logoutUrl(this.adminServer.path("/logout")).and()

  30. .httpBasic().and()

  31. .csrf()

  32. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())

  33. .ignoringRequestMatchers(

  34. new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()),

  35. new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()),

  36. new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))

  37. )

  38. .and()

  39. .rememberMe().key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600);

  40. }

  41. }

  42. }

监控客户端

pom.xml 引入:

<artifactId>admin-client</artifactId>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>

application.properties 配置文件:

# 爪哇笔记:https://blog.52itstyle.vip
spring.boot.admin.client.instance.name = 007
spring.boot.admin.client.url= http://localhost:9000
management.endpoints.web.exposure.include=*
spring.boot.admin.client.username = admin
spring.boot.admin.client.password = admin
spring.boot.admin.client.period = 10000
spring.boot.admin.client.connect-timeout = 5000
spring.boot.admin.client.read-timeout = 5000
spring.boot.admin.client.instance.service-url = http://localhost:8080

监控界面

SpringBoot 2.x 开发案例之整合Srping Boot Admin SpringBoot 2.x 开发案例之整合Srping Boot Admin

SpringBoot 2.x 开发案例之整合Srping Boot Admin

SpringBoot 2.x 开发案例之整合Srping Boot Admin

SpringBoot 2.x 开发案例之整合Srping Boot Admin

小结

不得不说, 2.X  版本还是很美观大气上档次的,并且监控告警功能齐全,小微服务必备神器。

SpringBoot 2.x 开发案例之整合Srping Boot Admin

▲一个有温度的公众号,期待与你一起进步

原文  http://mp.weixin.qq.com/s?__biz=MzA3OTUyNjkwMw==&mid=2656652982&idx=1&sn=da7bb4c43e5588385899001de06d76e1
正文到此结束
Loading...