以案例聊聊SpringCloud分布式系统的架构者(九)

上次我们聊了聊SpringCloud中的Zipkin的构建过程。这次我们就来聊聊服务的消费者相互调用时在ZipKin中的表现和变化。既然要观察表现和变化那么就必须来用两个服务的消费者调用,这样才可以跟踪和观察。

第一步:创建服务消费者A。SpringBoot的版本选择1.5.21,然后添加依赖。

第二步:修改配置文件

#指定端口
server.port=7030

#指定当前的名称
spring.application.name=server-consumer-08

#指定Zipkin的地址
spring.zipkin.base-url=http://localhost:9411/

第三步:创建相应的调用对象

package com.alibaba;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
public class ServerConsumer08Application {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Bean
    AlwaysSampler alwaysSampler(){
        return new AlwaysSampler();
    }

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/getUser")
    public String getUser(){
        return "getUser";
    }

    @RequestMapping(value="/getUserMassage")
    public String getUserMassage(){
        return restTemplate.getForObject("http://localhost:7040/getUserMassage",String.class);
    }

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

}

第四步:照着上面的创建步骤再来创建一个同样的消费者B,但是要注意两个消费者的端口号和项目名称不能相同。

创建消费者B时前两步都是一样的,在第三步我还是吧重点的代码粘一下。

package com.alibaba;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
public class ServerConsumer09Application {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    //AlwaysSampler只是为了调试,让zipkin100%采集信息。 可以将AlwaysSampler理解为一个采集器   
    @Bean
    public AlwaysSampler alwaysSampler(){
        return new AlwaysSampler();
    }

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value="/getUserMassage")
    public String getUserMassage(){
        return "SUCCESS";
    }


    @RequestMapping(value="/getUser")
    public String getUser(){
        return restTemplate.getForObject("http://localhost:7030/getUser",String.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(ServerConsumer09Application.class, args);
    }

}

第五步:消费者A调用消费者B,然后反过来消费者B再调用消费者A

第六步:观察Zipkin追踪界面

点击进去上面的以后再看

点击红色方框中的进去出现下面的页面:

这个图是当在浏览器中访问server-consumer-08服务的时候Zipkin追踪到的信息,当刚接接受到从浏览器发送过来的信息的时候Reltive Time的时间为空,然后在server-consumer-08里面调用了server-consumer-09里面的服务,于是zipkin又追踪到了这个调用的过程。

上图是两个服务之间的依赖关系。

Span: 基本的工作单元。Span包括一个64位的唯一ID,一个64位trace码,描述信息,时间戳事件,key-value 注解(tags),span处理者的ID(通常为IP)。

Trace: 包含一系列的工作单元span,它们组成了一个树型结构。

Annotation 
用于及时记录存在的事件。常用的Annotation如下:

cs:客户端发送(client send) 客户端发起一个请求,表示span开始
sr:服务器接收(server received) 服务器接收到客户端的请求并开始处理,sr - cs 的时间为网络延迟
ss:服务器发送(server send)  服务器处理完请求准备返回数据给客户端。ss - sr 的时间表示服务器端处理请求花费的时间
cr:客户端接收(client received) 客户端接收到处理结果,表示span结束。 cr - cs 的时间表示客户端接收服务端数据的时间
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值