相关
Spring Cloud 实战系列 (一) - 服务注册与发现 Eureka
Spring Cloud 实战系列 (二) - 客户端调用 REST + Ribbon
Spring Cloud 实战系列 (三) - 声明式客户端 Feign
Spring Cloud 实战系列 (四) - 熔断器 Hystrix
Spring Cloud 实战系列 (五) - 服务网关 Zuul
Spring Cloud 实战系列 (六) - 分布式配置中心 Spring Cloud Config
Spring Cloud 实战系列 (七) - 服务链路追踪 Spring Cloud Sleuth
前言
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.YAML, 设置 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.YAML 文件, 设置 日志输出路径, 并关闭 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-server,admin-server 和 admin-client 模块, 访问 admin-server 的主页 http://localhost:5000 , 浏览器显示界面如图所示:
JOURNAL 选项为 服务注册, 下线, 剔除 的 时间线.
4. 添加安全登录界面
Spring Boot Admin 提供了登录界面的组件, 并且和 Spring Boot Security 结合使用, 需要 用户登录 才能访问. 在 Admin Server 的 pom.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.YAML 中完成如下配置, 创建一个 security 的 user 用户, 它的用户名为 admin, 密码为 123456. 通过 eureka.instance.metadate-map 配置属性 带上该 security 的 user 用户信息.
- 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 与微服务构建》
来源: https://juejin.im/post/5c58e4e1518825625f3a2585