搭建Springboot Admin监控服务器

1 认识springboot actuator

1.1 Actuator Endpoints (端点)

♦ Endpoints是Actuator的核心部分,它用来监视应用程序及交互; SpringBoot Actuator 内置了很多 Endpoints ,并支持扩展

♦ SpringBoot Actuator提供的原生端点有三类:
应用配置类:自动配置信息、Spring Bean信息、yml文件信息、环境信息等等
度量指标类:主要是运行期间的动态信息,例如堆栈、健康指标、metrics信息等等
操性制类:主要是指shutdown ,用户可以发送一个请求将应用的监控功能关闭

2 搭建服务器步骤:

2.1 添加SpringBoot Admin Start依赖


<dependency>
  <groupId>de. codecentricgroupId>
  <artifactId>spring-boot-admin-starter-serverartifactId>
  <version>2.2.0version>
dependency>
2.2 添加@EnableAdminServer注解
2.3 在bootstrap.yml添加如下配置:
server:
  port: 7001
  servlet:
    context-path: /e-commerce-admin
spring:
  application:
    name: e-commerce-admin
  cloud:
    nacos:
      discovery:
        enabled: true
        server-addr: 127.0.0.1:8848
        namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
        metadata:
          management:
            context-path: ${server.servlet.context-path}/actuator
     thymeleaf:
    check-template: false
    check-template-location: false
# 暴露端点
management:
  endpoints:
    web:
      exposure:
        include: '*'  # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 *, 可以开放所有端点
  endpoint:
    health:
      show-details: always

3 应用注册到 SpringBoot Admin Server

3.1 被监控和管理的应用(微服务),注册到 Admin Server的两种方式
◆ 方式一:被监控和管理的应用程序,使用SpringBoot Admin Client库,通过 HTTP
调用注册到SpringBoot Admin Server上
◆ 方式二:首先,被监控和管理的应用程序,注册到SpringCloud集成的注册中心;
然后SpringBoot Admin Server通过注册中心获取到被监控和管理的应用程序
3.2 应用注册到AdminServer步骤:
3.2.1 在模块的pom文件中添加SpringBoot Actuotor配置:
   <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
3.2.2 在模块的bootstrap.yml添加以下配置:
spring:
  application:
    name: e-commerce-nacos-client # 应用名称也是构成 Nacos 配置管理 dataId 字段的一部分 (当 config.prefix 为空时)
  cloud:
    nacos:
      namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
      discovery:
        metadata:
          management:
            context-path: ${server.servlet.context-path}/actuator
# 暴露端点
management:
  endpoints:
    web:
      exposure:
        include: '*'  # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 *, 可以开放所有端点
  endpoint:
    health:
      show-details: always

3 SpringBoot Admin Server开启认证

3.1 在SpringBoot Admin Server模块的pom文件中添加SpringBoot Security依赖:


    org.springframework.boot
    spring-boot-starter-security

3.2 在bootstrap.yml文件中添加配置:

spring:
  security: #
    user: root #
    password: root #
spring:
  cloud:
    nacos:
      namespace: 1bc13fd5-843b-4ac0-aa55-695c25bc0ac6
      discovery:
        metadata:
          management:
            context-path: ${server.servlet.context-path}/actuator
          user.name: root 
          user.password: root   

3.3 创建Spring Security的配置类:


import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

/**
 * 

配置安全认证, 以便其他的微服务可以注册

* 参考 Spring Security 官方 * */
@Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { /** 应用上下文路径 */ private final String adminContextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties) { this.adminContextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(adminContextPath + "/"); http.authorizeRequests() // 1. 配置所有的静态资源和登录页可以公开访问 .antMatchers(adminContextPath + "/assets/**").permitAll() .antMatchers(adminContextPath + "/login").permitAll() // 2. 其他请求, 必须要经过认证 .anyRequest().authenticated() .and() // 3. 配置登录和登出路径 .formLogin().loginPage(adminContextPath + "/login") .successHandler(successHandler) .and() .logout().logoutUrl(adminContextPath + "/logout") .and() // 4. 开启 http basic 支持, 其他的服务模块注册时需要使用 .httpBasic() .and() // 5. 开启基于 cookie 的 csrf 保护 .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // 6. 忽略这些路径的 csrf 保护以便其他的模块可以实现注册 .ignoringAntMatchers( adminContextPath + "/instances", adminContextPath + "/actuator/**" ); } }

你可能感兴趣的:(spring,boot,服务器,java,spring,cloud)