Spring Cloud(Finchley.RCI) (十)

ZipKin简介

ZipKin是一个开放源代码的分布式跟踪系统, 由Twitter公司开源, 它致力于收集服务的定时数据, 以解决微服务架构中的延迟问题, 包括数据的收集, 存储, 查找和展现。

每个服务向Zipkin报告计时数据, ZipKin会根据调用关系通过Zipkin UI生成依赖关系图, 显示了多少跟踪请求通过每个服务, 该系统让开发者可通过一个Web前端收集和分析数据, 例如用户每次请求服务的处理时间等, 可方便的监测系统中存在的瓶颈。

服务追踪说明

微服务架构通过业务来划分服务的, 使用REST调用。对外暴露的一个接口, 可能需要很多个服务协同才能完成这个接口功能, 如果链路上任何一个服务出现问题或者网络超时, 都会导致接口调用失败。随着业务的不断扩张, 服务之间互相调用会越来越复杂。

 

创建Zipkin服务端

创建一个工程名为hello-spring-cloud-zipkin的项目, pom.xml文件如下

<?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>com.funtl</groupId>
        <artifactId>hello-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hello-spring-cloud-zipkin</artifactId>
    <packaging>jar</packaging>

    <name>hello-spring-cloud-zipkin</name>
    <url>http://www.funtl.com</url>
    <inceptionYear>2018-Now</inceptionYear>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.funtl.hello.spring.cloud.zipkin.ZipKinApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Application

通过@EnableZipKinServer注解开启ZipKin Server功能

package com.funtl.hello.spring.cloud.zipkin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import zipkin.server.internal.EnableZipkinServer;

@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class ZipKinApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZipKinApplication.class, args);
    }
}

application.yml

设置端口号为: 9411, 该端口号为Zipkin server的默认端口号:

spring:
  application:
    name: hello-spring-cloud-zipkin
server:
  port: 9411
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
management:
  metrics:
    web:
      server:
        auto-time-requests: false

追踪服务

在所有需要被追踪的项目中增加spring-cloud-starter-zipkin依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

在这些项目的application.yml配置文件中增加Zipkin Server的地址

zipkin:
  base-url: http://localhost:9411

测试追踪

启动全部项目, 打开浏览器防卫: http://localhost:9411/

Spring Boot Admin服务监控

随着开发周期的推移, 项目会不断变大, 切分出的服务也会越来越多, 这时一个个的微服务构成了错综复杂的系统。对于各个微服务系统的健康状态, 会话数量, 并发数, 服务资源, 延迟等度量信息的收集称成为了一个挑战。Spring Boot Admin应运而生, 它正是基于这些需求开发出的一套功能强大的监控管理系统。

Spring Boot Admin有两个角色组成, 一个是Spring Boot Admin Server, 一个是Spring Boot Admin Client.

发布了69 篇原创文章 · 获赞 8 · 访问量 9413

猜你喜欢

转载自blog.csdn.net/u011414629/article/details/101416442