SpringCloud zipkin (一、入门实例)

一、简介

Zipkin是 Twitter 的一个 开源项目 ,它基于 Google Dapper实现。我们可以使用它来收集各个 服务器 上请求链路的跟踪数据,并通过它提供的 REST API 接口来辅助我们查询跟踪数据以实现对分布式系统的监控程序,从而及时地发现系统中出现的延迟升高问题并找出系统性能瓶颈的根源。除了面向 开发 的API接口之外,它也提供了方便的 UI 组件来帮助我们直观的搜索跟踪信息和分析请求链路明细,比如:可以查询某段时间内各用户请求的处理时间等。

Zipkin主要包括四个模块 
1- Collector :接收或收集各应用传输的数据 
2- Storage    :存储接受或收集过来的数据,当前支持Memory,MySQL,Cassandra,ElasticSearch等,默认存储在内存中。 
3- API(Query)  :负责查询Storage中存储的数据,提供简单的JSON API获取数据,主要提供给web UI使用 
4- Web         :提供简单的web界面

官方GTIHUB       https://github.com/openzipkin/zipkin/tree/master/zipkin-server

官方介绍     https://zipkin.io/pages/instrumenting.html

二、安装、启动

spring cloud版本在finchley(包含)后,官方不建议自定义zipkin-server,已经提供官方打包好的jar包,直接启动即可,默认端口为9411

下载最新版:https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec

下载2.12.9版:https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=2.12.9&c=exec 

运行:java -jar zipkin-server-2.12.9-exec.jar


如果你Spring cloud版本finchley之前可以自己创建Zipkin服务(不建议)。
创建过程:
pom.xml:

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

ZipkinApplication.java

@SpringBootApplication
@EnableZipkinServer //增加@EnableZipkinServer注解
public class ZipkinApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZipkinApplication.class, args);
    }
}

启动……在浏览器里输入http://localhost:8080,能看到Zipkin的首页。 

使用java -jar zipkin-server-2.12.9-exec.jar 启动zipkin:

三、创建Zipkin Client

将我们的服务配置为Zipkin Client将操作信息发送到Zipkin Server中。

gateway微服务:

pom.xml

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

 application.yml

server:
  port: 8071

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://127.0.0.1:8072
          predicates:
            - After=2017-01-20T17:42:47.789-07:00[America/Denver]

  zipkin:
    base-url: http://127.0.0.1:9411/
    locator:
      discovery:
        enabled: true

Gateway8701.java 

@SpringBootApplication
public class Gateway8071 {

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

}

cms微服务:

pom.xml

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

application.yml

server:
  port: 8072
spring:
  zipkin:
    base-url: http://127.0.0.1:9411/
    locator:
      discovery:
        enabled: true

Cms8072.java

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

    @ResponseBody
    @RequestMapping(value = "news")
    public Object news(){
        String news[]=new String[]{"我和我的祖国", "我是唱作人", "我和我的经纪人", "我爱我家", "我是唱作人第二期", "我的前半生2", "我是歌手", "我们的师父", "我们都要好好的", "我家那闺女"};
        return news;
    }
    
}

启动这两个微服务……

测试,通过网关微服务访问到了cms微服务。

 进入zipkin-server http://127.0.0.1:9411

 

这样我们就很容易排查出调用链中耗时多的做针对性处理了。

猜你喜欢

转载自blog.csdn.net/hong10086/article/details/89472771
今日推荐