java version b2b2c social electricity supplier spring cloud distributed micro-services (nine) link tracking service (Spring Cloud Sleuth)

I. Introduction

b2b2c social e-commerce platform source code, please add penguin beg: 1038774626. Spring Cloud Sleuth main function is to provide a distributed system tracking solutions, and is compatible support zipkin, you only need to introduce appropriate to rely on the pom file.

Second, the service tracking analysis

Divided by business services, by calling REST, an interface of external exposure, may take many months to complete this collaborative service interface functions on micro-service architecture, if any problems with a service or a network timeout on the link will lead to the formation of an interface call failed. With the continuous expansion of business, call each other between services will become increasingly complex.

Paste_Image.png

As more and more services, analysis of the call chain will become increasingly complex. Maybe call the relationship between them as follows:

 

Third, the term

  • Span: the basic unit of work, for example, in a transmission span in a new RPC is equivalent to sending a response to the RPC requests, span, another 64-bit ID to the trace represented by a 64-bit unique identification ID, there are other data span such as abstract, timestamp events, key values comment (tags), span the ID, and schedule ID (usually an IP address) 
    span in constant starts and stops, while recording the time information, when you create a span, you it must stop at some point in the future.
  • Trace: a tree-like structure consisting of a series of spans, for example, if you are running a distributed big data project, you may need to create a trace.
  • Annotation: used to record the presence of a timely event, some of the core annotations to define the start and end of a request 
    • cs - Client Sent - the client initiates a request, this annotion describes the start of the span
    • sr - Server Received - server request and get ready to deal with it, if it sr cs timestamp can be obtained by subtracting the network latency
    • ss - Server Sent - notes that the request to complete the process (when the request back to the client), if ss sr timestamp can be obtained by subtracting the server needs time to process the request
    • cr - Client Received - indicates the end, the client receives the reply span of successful service side, if cr cs minus timestamp can get all the time required to obtain the client from the server's reply 
      will be used in a Span and Trace system Zipkin graphical annotation process:

 

Fourth, build the project

Basic knowledge to explain finished, let's take a practical, case of this paper, there are three projects components: a server-zipkin, its main role using ZipkinServer functions, collect call data and display; a service-hi, the external exposure hi interfaces; a service-miya, miya exposed to the external interfaces; both service can call each other; and only called, server-zipkin will collect the data, which is why the calls traced.

4.1 build server-zipkin

Build a spring-boot project named server-zipkin, introducing rely on its pom:

<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();
    }
}

JAVASpring Cloud needs of large enterprises distributed micro cloud services built B2B2C e-commerce platform source code, please add penguin beg: 1038774626

Guess you like

Origin www.cnblogs.com/itcode-code/p/10966141.html