SpringBoot+actuator和admin-UI实现监控中心

淡淡的烟草味﹌ 2024-03-17 20:04 129阅读 0赞

使用SpringBoot很久了,但是很少使用到SpringBoot的查看和监控,将来八成也不会用到,万一有机会用到呢?所以记录一下以前学习SpringBoot+actuator和adminUI实现监控中心的方式

Springboot的版本2.0.x

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.5.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

导入对应的包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. <version>2.0.5.RELEASE</version>
  5. </dependency>
  6. <!-- security 一旦导入就会生效 -->
  7. <!--
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-security</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.security</groupId>
  14. <artifactId>spring-security-test</artifactId>
  15. <scope>test</scope>
  16. </dependency>
  17. -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>

application.properties

  1. server.port=7080
  2. # 配置用户名和密码
  3. #spring.security.user.name=admin
  4. #spring.security.user.password=123456
  5. # 端点信息配置
  6. management.server.port=8081
  7. management.server.servlet.context-path=/sys
  8. # 默认 never always可以显示硬盘使用情况和线程情况
  9. management.endpoint.health.show-details=always
  10. # 端点暴露的内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
  11. management.endpoints.web.exposure.include=*
  12. # actuator 信息
  13. info.actuator.name=test

启动之后

myw
访问

  1. http://localhost:8081/sys/actuator

myw
在这里使用的Actuator是spring boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。可以使用HTTP的各种请求来监管,审计,收集应用的运行情况.特别对于微服务管理十分有意义.缺点:没有可视化界面。
使用场景,针对微服务的服务状态监控,服务器的内存变化(堆内存,线程,日志管理等),检测服务配置连接地址是否可用(模拟访问,懒加载),统计现在有多少个bean(Spring容器中的bean) 统计接口数量, 应用场景:生产环境

  1. /actuator/beans 显示应用程序中所有Spring bean的完整列表
  2. /actuator/configprops 显示所有配置信息
  3. /actuator/env 陈列所有的环境变量
  4. /actuator/mappings 显示所有@RequestMappingurl整理列表
  5. /actuator/health 显示应用程序运行状况信息 up表示成功 down失败,对于懒加载没报错的可以看到控制台报错了
  6. /actuator/info 查看自定义应用信息

懒加载有个缺点,例如mysql的配置,启动的时候不会发现错误,只有运行的时候才报错

当访问

  1. http://localhost:8081/sys/actuator/health

myw
使用adminUI的方式 client客户端导包和配置
pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-actuator</artifactId>
  8. <version>2.0.5.RELEASE</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>de.codecentric</groupId>
  12. <artifactId>spring-boot-admin-starter-client</artifactId>
  13. <version>2.0.5</version>
  14. </dependency>

application.properties

  1. server.port=8071
  2. spring.application.name=boot-example-admin-client
  3. spring.boot.admin.client.url=http://127.0.0.1:8050/myw-admin
  4. spring.boot.admin.client.username=admin
  5. spring.boot.admin.client.password=123456
  6. spring.boot.admin.client.instance.service-url=http://127.0.0.1:8071
  7. #management.server.port=8081
  8. #management.server.servlet.context-path=/sys
  9. # 默认 never always可以显示硬盘使用情况和线程情况
  10. management.endpoint.health.show-details=always
  11. # 端点暴露的内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
  12. management.endpoints.web.exposure.include=*

使用admin-ui的方式 server服务端导包和配置
pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-security</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>de.codecentric</groupId>
  11. <artifactId>spring-boot-admin-starter-server</artifactId>
  12. <version>2.0.5</version>
  13. </dependency>

application.properties

  1. server.port=8050
  2. server.servlet.context-path=/myw-admin
  3. spring.application.name=boot-example-admin-server
  4. spring.security.user.name=admin
  5. spring.security.user.password=123456

WebSecurityConfig.java

  1. package boot.example.admin.server;
  2. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  7. /**
  8. * SpringBootAdmin 登录鉴权使用
  9. *
  10. */
  11. @EnableWebSecurity
  12. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  13. private final String contextPath;
  14. public WebSecurityConfig(AdminServerProperties adminServerProperties) {
  15. this.contextPath = adminServerProperties.getContextPath();
  16. }
  17. @Override
  18. protected void configure(HttpSecurity http) throws Exception {
  19. // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
  20. http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  21. .ignoringAntMatchers(contextPath + "/instances");
  22. http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
  23. http.authorizeRequests().antMatchers(contextPath + "/actuator/**").permitAll(); // 自身监控
  24. http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证
  25. // 整合spring-boot-admin-server-ui
  26. http.formLogin().loginPage("/login").permitAll();
  27. http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
  28. // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
  29. http.httpBasic();
  30. }
  31. }

启动客户端和服务端的监控中心

  1. http://localhost:8050/myw-admin/login#/applications

myw
myw
myw
记录一下在SpringBoot2.6.6版本使用

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.6.6</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

client的pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-actuator</artifactId>
  8. <version>2.6.6</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>de.codecentric</groupId>
  12. <artifactId>spring-boot-admin-starter-client</artifactId>
  13. <version>2.5.6</version>
  14. </dependency>

application.properties 1

  1. server.port=8071
  2. spring.application.name=boot-example-admin-client1
  3. spring.boot.admin.client.url=http://localhost:8050
  4. spring.boot.admin.client.username=admin
  5. spring.boot.admin.client.password=123456
  6. management.endpoint.health.show-details=always
  7. management.endpoints.web.exposure.include=*

application.properties 2

  1. server.port=8072
  2. spring.application.name=boot-example-admin-client2
  3. spring.boot.admin.client.url=http://localhost:8050
  4. spring.boot.admin.client.username=admin
  5. spring.boot.admin.client.password=123456
  6. management.server.port=8082
  7. management.server.base-path = /sys
  8. management.endpoint.health.show-details=always
  9. management.endpoints.web.exposure.include=*

application.properties 3

  1. server.port=8073
  2. spring.application.name=boot-example-admin-client3
  3. spring.boot.admin.client.url=http://localhost:8050
  4. spring.boot.admin.client.username=admin
  5. spring.boot.admin.client.password=123456
  6. #spring.security.user.name=admin
  7. #spring.security.user.password=123456
  8. management.server.port=8083
  9. management.server.base-path = /sys
  10. management.endpoint.health.show-details=always
  11. management.endpoints.web.exposure.include=*

服务端的配置

pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-security</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>de.codecentric</groupId>
  11. <artifactId>spring-boot-admin-starter-server</artifactId>
  12. <version>2.5.6</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-actuator</artifactId>
  17. </dependency>

application.properties

  1. server.port=8050
  2. spring.application.name=boot-example-admin-server
  3. spring.security.user.name=admin
  4. spring.security.user.password=123456

备注记录到这里 将来会不会用到再说了。

发表评论

表情:
评论列表 (有 0 条评论,129人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Dubbo监控中心

    1)、dubbo-admin 图形化的服务管理页面;安装时需要指定注册中心地址,即可从注册中心中获取到所有的提供者/消费者进行配置管理   2)、dubbo-mon

    相关 Dubbo 监控中心

    1.Dubbo 监控中心      dubbo管理控制台开源部分主要包含:   提供者  路由规则  动态配置  访问控制  权重调节  负载均衡  负责人,等管理功能。

    相关 Dubbo后台管理监控中心部署

    通过dubbo监控中心和后台管理可以很好的监控dubbo服务,监控服务端服务和客户端调用情况,调用次数,调用日志,方便问题查找。下面我们看看dubbo的管理后台和监控中心怎么部