Spring Cloud实战系列(八) - 微服务监控Spring Boot Admin

5,319 阅读4分钟

相关

  1. Spring Cloud实战系列(一) - 服务注册与发现Eureka

  2. Spring Cloud实战系列(二) - 客户端调用Rest + Ribbon

  3. Spring Cloud实战系列(三) - 声明式客户端Feign

  4. Spring Cloud实战系列(四) - 熔断器Hystrix

  5. Spring Cloud实战系列(五) - 服务网关Zuul

  6. Spring Cloud实战系列(六) - 分布式配置中心Spring Cloud Config

  7. Spring Cloud实战系列(七) - 服务链路追踪Spring Cloud Sleuth

  8. Spring Cloud实战系列(八) - 微服务监控Spring Boot Admin

  9. Spring Cloud实战系列(九) - 服务认证授权Spring Cloud OAuth 2.0

  10. Spring Cloud实战系列(十) - 单点登录JWT与Spring Security OAuth

前言

Spring Boot Admin 是一个 管理监控 Spring Boot 应用程序 的一款开源软件。Spring Boot Admin 分为 Server 端和 Client 端,Spring Boot Admin UI 部分使用 AngularJS 将数据展示在前端。

正文

1. 项目结构

  • Eureka Server服务注册中心,端口号为 8761

  • Admin Server:用于对 微服务系统 进行统一的 监控管理

  • Admin Client客户端 集成 Admin

2. 构建Admin Server

新建一个 Spring Boot 的项目模块,取名为 admin-server,其完整依赖如下:

<?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 http://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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>io.github.ostenant.springcloud</groupId>
    <artifactId>admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>admin-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- 管理界面与JMX-Beans交互 -->
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置 application.yml,设置 management.security.enabled=false,保证 安全验证 关闭,设置 Spring Boot Admin 默认开启的 服务端点

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 5000
spring:
  application:
    name: admin-server
  boot:
    admin:
      routes:
        endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
management:
  security:
    enabled: false
logging:
  file: "logs/boot-admin-sample.log"

src/main/resources 目录下新建一个 logback-spring.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <jmxConfigurator/>
</configuration>

在应用的 启动类 上通过注解 @EnableAdminServer 开启 Admin Server 的功能。

@EnableEurekaClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }
}

Admin Server 创建完成,运行 AdminServerApplication 启动应用程序!

2. 构建Admin Client

新建一个 Spring Boot 的项目模块,取名为 admin-client,其完整依赖如下:

<?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 http://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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>io.github.ostenant.springcloud</groupId>
    <artifactId>admin-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>admin-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置 application.yml 文件,设置 日志输出路径,并关闭 Actuator 模块的 安全验证

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: admin-client
management:
  security:
    enabled: false
logging:
  file: "logs/boot-admin-client.log"

在应用的 启动类 上加上 @EnableEurekaClient 注解,开启 EurekaClient 的功能。

@SpringBootApplication
@EnableEurekaClient
public class AdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminClientApplication.class, args);
    }
}

3. 启动应用程序

依次启动 eureka-serveradmin-serveradmin-client 模块,访问 admin-server 的主页 http://localhost:5000,浏览器显示界面如图所示:

JOURNAL 选项为 服务注册下线剔除时间线

4. 添加安全登录界面

Spring Boot Admin 提供了登录界面的组件,并且和 Spring Boot Security 结合使用,需要 用户登录 才能访问。在 Admin Serverpom.xml 文件中引入以下依赖:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui-login</artifactId>
    <version>1.5.0</version>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
</dependency>

admin-server 模块的 application.yml 中完成如下配置,创建一个 securityuser 用户,它的用户名为 admin,密码为 123456。通过 eureka.instance.metadate-map 配置属性 带上该 securityuser 用户信息。

security:
  user:
    name: admin
    password: 123456
eureka:
  instance:
    metadata-map:
      user.name: admin
      user.password: 123456

然后在 应用程序 中配置 Spring Boot Security,创建一个 SecurityConfig配置类,给 静态资源 加上 permitAll() 权限,其他的 资源访问 则需要 权限认证,另外这些资源不支持 CSFR跨站请求伪造),所以禁用掉 CSFR,最后需要开启 HTTP 的基本认证,即 httpBasic() 方法。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
        http.logout().logoutUrl("/logout");
        http.csrf().disable();

        http.authorizeRequests()
                .antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
                .permitAll();
        http.authorizeRequests().antMatchers("/**").authenticated();

        http.httpBasic();
    }
}

重新启动 admin-server 项目模块,在浏览器中访问 admin-client 的地址 http://localhost:5000,在登录界面输入用户名 admin,密码为 123456,登录即可。

参考

  • 方志朋《深入理解Spring Cloud与微服务构建》

欢迎关注技术公众号: 零壹技术栈

零壹技术栈

本帐号将持续分享后端技术干货,包括虚拟机基础,多线程编程,高性能框架,异步、缓存和消息中间件,分布式和微服务,架构学习和进阶等学习资料和文章。