当前位置:   article > 正文

SpringBootAdmin+actuator进行服务监控_springbootadmin请求/actuator带上了/instances前缀

springbootadmin请求/actuator带上了/instances前缀

SpringBootAdmin可以监控SpringBoot单击或集群项目,提供详细的健康信息、内存信息、JVM系统和环境属性、垃圾回收信息、日志设置和查看、定时任务查看、SpringBoot缓存查看和管理功能。

第一步:监控服务端搭建

        创建一个SpringBoot项目,添加下面依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>de.codecentric</groupId>
  7. <artifactId>spring-boot-admin-starter-server</artifactId>
  8. </dependency>

        在启动类上开启监控服务

  1. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @EnableAdminServer
  5. @SpringBootApplication
  6. public class SysadminApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(SysadminApplication.class, args);
  9. System.out.println("项目启动成功!");
  10. }
  11. }

        在配置文件中只需要配置一个服务端口即可。配置端口的目的是为了不与其它服务冲突。

server.port=8081

         启动服务端,此时访问地址:http://localhost:8081/applications  界面如下

添加security支持,防止任意用户可查看监控。首先添加依赖

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

在配置文件中添加以下配置

  1. # 设置登录用户名、密码和角色
  2. spring.security.user.name=java666
  3. spring.security.user.password=java666
  4. spring.security.user.roles=SBA_ADMIN

最后,进行权限资源设置。在服务端创建类,加入下面代码即可

  1. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  7. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  8. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  9. @EnableWebSecurity
  10. @Configuration(proxyBeanMethods = false)
  11. public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
  12. private final String adminContextPath;
  13. public AdminSecurityConfig(AdminServerProperties adminServerProperties) {
  14. this.adminContextPath = adminServerProperties.getContextPath();
  15. }
  16. @Override
  17. protected void configure(HttpSecurity http) throws Exception {
  18. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  19. successHandler.setTargetUrlParameter("redirectTo");
  20. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  21. http.authorizeRequests()
  22. .antMatchers(adminContextPath + "/assets/**").permitAll()
  23. .antMatchers(adminContextPath + "/login").permitAll()
  24. .antMatchers(adminContextPath + "/instances/**").permitAll()
  25. .anyRequest().authenticated()
  26. .and()
  27. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
  28. .logout().logoutUrl(adminContextPath + "/logout").and()
  29. .httpBasic().and()
  30. .csrf()
  31. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  32. .ignoringAntMatchers(
  33. adminContextPath + "/instances",
  34. adminContextPath + "/actuator/**"
  35. );
  36. }
  37. @Override
  38. public void configure(WebSecurity web) {
  39. web.ignoring().antMatchers("/actuator/**");
  40. }
  41. }

此时访问监控系统就需要输入用户名和密码才能正常使用。如下所示

 

 第二步:客户端搭建配置

        1、所需依赖

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-actuator</artifactId>
  8. </dependency>

        2、在客户端配置文件中添加如下配置

        注意:如果是yml配置文件,在配置开启监控所有项的时候,*需要使用单引号包裹

  1. # 当前项目端口号
  2. server.port=8080
  3. # Spring Boot Admin 监控服务器端地址
  4. spring.boot.admin.client.url=http://localhost:8081
  5. # 开启监控所有项,Actuator功能
  6. management.endpoints.web.exposure.include=*

        3、启动项目,利用服务端主页地址查看即可http://localhost:8081/applications

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

闽ICP备14008679号