java B2B2C springmvc mybatis多租户电子商城系统-(九)服务链路追踪(Spring Cloud Sleuth)

这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件。java

1、简介
Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you will see the correlation data being collected in logs, as long as you are logging requests.web

—— 摘自官网spring

Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,而且兼容支持了 zipkin,你只须要在pom文件中引入相应的依赖便可。bash

2、服务追踪分析
微服务架构上经过业务来划分服务的,经过REST调用,对外暴露的一个接口,可能须要不少个服务协同才能完成这个接口功能,若是链路上任何一个服务出现问题或者网络超时,都会造成致使接口调用失败。随着业务的不断扩张,服务之间互相调用会愈来愈复杂。网络

3、术语
Span:基本工做单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span经过一个64位ID惟一标识,trace以另外一个64位ID表示,span还有其余数据信息,好比摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(一般是IP地址)
span在不断的启动和中止,同时记录了时间信息,当你建立了一个span,你必须在将来的某个时刻中止它。
Trace:一系列spans组成的一个树状结构,例如,若是你正在跑一个分布式大数据工程,你可能须要建立一个trace。
Annotation:用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束
cs - Client Sent -客户端发起一个请求,这个annotion描述了这个span的开始
sr - Server Received -服务端得到请求并准备开始处理它,若是将其sr减去cs时间戳即可获得网络延迟
ss - Server Sent -注解代表请求处理的完成(当请求返回客户端),若是ss减去sr时间戳即可获得服务端须要的处理请求时间
cr - Client Received -代表span的结束,客户端成功接收到服务端的回复,若是cr减去cs时间戳即可获得客户端从服务端获取回复的全部所需时间
将Span和Trace在一个系统中使用Zipkin注解的过程图形化:
将Span和Trace在一个系统中使用Zipkin注解的过程图形化:架构

4、构建工程
基本知识讲解完毕,下面咱们来实战,本文的案例主要有三个工程组成:一个server-zipkin,它的主要做用使用ZipkinServer 的功能,收集调用数据,并展现;一个service-hi,对外暴露hi接口;一个service-miya,对外暴露miya接口;这两个service能够相互调用;而且只有调用了,server-zipkin才会收集数据的,这就是为何叫服务追踪了。app

4.1 构建server-zipkin分布式

建一个spring-boot工程取名为server-zipkin,在其pom引入依赖:spring-boot

复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>复制代码
复制代码

在其程序入口类, 加上注解@EnableZipkinServer,开启ZipkinServer的功能:微服务

复制代码
@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ServerZipkinApplication.class, args);
    }
}复制代码
复制代码

在配置文件application.yml指定服务端口为:

server.port=9411复制代码

4.2 建立service-hi

在其pom引入起步依赖spring-cloud-starter-zipkin,代码以下:

复制代码
<dependencies>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--compile('org.springframework.cloud:spring-cloud-starter-zipkin')-->
 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</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>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>复制代码
复制代码

在其配置文件application.yml指定zipkin server的地址,头经过配置“spring.zipkin.base-url”指定:

server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi复制代码

经过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就能够了。

对外暴露接口:

复制代码
@SpringBootApplication
@RestController
public class ServiceHiApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApplication.class, args);
    }
 
    private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName());
 
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
 
    @RequestMapping("/hi")
    public String callHome(){
        LOG.log(Level.INFO, "calling trace service-hi ");
        return restTemplate.getForObject("http://localhost:8989/miya", String.class);
    }
    @RequestMapping("/info")
    public String info(){
        LOG.log(Level.INFO, "calling trace service-hi ");
 
        return "i'm service-hi";
 
    }
 
    @Bean
    public AlwaysSampler defaultSampler(){
        return new AlwaysSampler();
    }
}复制代码
复制代码

架构代码以下:

"分布式b2b <wbr


Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求:一零三八七七四六二六  

相关文章
相关标签/搜索