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

栏目: Java · 发布时间: 5年前

内容简介:新建一个配置

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 ,浏览器显示界面如图所示:

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

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实战系列(八) - 微服务监控Spring Boot Admin》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Git

Pro Git

Scott Chacon / Apress / 2009-8-27 / USD 34.99

Git is the version control system developed by Linus Torvalds for Linux kernel development. It took the open source world by storm since its inception in 2005, and is used by small development shops a......一起来看看 《Pro Git》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具