四、微服务之监控中心(服务端)

四、微服务之监控中心(服务端)

主要工具

Spring-Boot-Admin 版本号:2.2.1

简单介绍

spring-boot-admin 可以通过注册中心获取到其上服务的运行状态(需要在对应的服务中开放健康信息相关接口的访问权限),用户可以通过网页端查看对应服务的属性配置、堆栈运行、日志等信息

主要依赖
<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>com.fatcat</groupId>
    <artifactId>fc-monitor</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>fc-monitor</name>
    <description>所有服务的监控中心</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.2.1</spring-boot-admin.version>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <!-- 引入eureka的安全检测 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 增加安全配置,账号和密码 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 引入 jetty 是因为tomcat 启动 springBootAdmin 会报错,报错不影响运行 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
配置文件
# 服务端口 
server.port=9900 
# 服务名称 
spring.application.name=fc-monitor 
# 开启安全验证 
spring.security.user.name=admin spring.security.user.password=admin123 
# eureka 注册地址 
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ 
# 自身的主机地址 
eureka.instance.hostname=localhost 
# 心跳周期,单位秒 
eureka.instance.lease-renewal-interval-in-seconds=10 
# 过期时间,单位秒,即60s没有心跳就从注册中心剔除 
eureka.instance.lease-expiration-duration-in-seconds=30 
# 健康监控,暴露自身健康相关全部接口 
management.endpoints.web.exposure.include=* 
# 健康信息显示 
management.endpoint.health.show-details=always 
# 可以直接引入 spring-boot-admin-starter-client 依赖后设置登录账号密码,目前采用配置类的方式设置登录权限 #spring.boot.admin.client.username=${spring.security.user.name} #spring.boot.admin.client.password=${spring.security.user.password}
配置类

如果不需要登录验证可以省略此步,包括配置中心的用户名和密码,以及依赖中的 spring-boot-starter-security 包均可以省略;
如果需要用户权限验证除了以上的必要配置外,还需要添加一个配置类,代码如下:

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.boot.SpringBootConfiguration;
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;

@SpringBootConfiguration(proxyBeanMethods = false)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    public SecurityConfiguration(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        // 控制浏览器是否新开一个客户端
        // successHandler.setTargetUrlParameter("redirectTo"); 
        successHandler.setDefaultTargetUrl(adminContextPath + "/");
        http.authorizeRequests()
                // 配置所有静态资源和登录页可以公开访问 
                .antMatchers(adminContextPath + "/actuator/**")
                .permitAll()
                .antMatchers(adminContextPath + "/assets/**")
                .permitAll()
                .antMatchers(adminContextPath + "/login")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                // 配置登录和登出路径 
                .formLogin().loginPage(adminContextPath + "/login")
                .successHandler(successHandler)
                .and().logout()
                .logoutUrl(adminContextPath + "/logout")
                .and()
                // 开启http basic支持,admin-client注册时需要使用 
                .httpBasic().and().csrf()
                // 开启基于cookie的csrf保护
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                // 忽略这些路径的csrf保护以便admin-client注册 
                .ignoringAntMatchers(adminContextPath + "/instances", adminContextPath + "/actuator/**");
    }
}
启动类修改
package com.fatcat.monitor;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableEurekaClient
// 注明当前服务属于监控服务端
@EnableAdminServer
// 开启登录验证,如果不需要请自行删除 
@EnableWebSecurity
public class FcMonitorApplication {
    public static void main(String[] args) {
        SpringApplication.run(FcMonitorApplication.class, args);
    }
}