[] Spring Cloud Spring Cloud's Spring Cloud Sleuth, distributed tracking service (1)

First, the role Spring Cloud Sleuth components

  Increased ability to track service for distributed micro-services architecture, for each request, the full link to track calls, can help us to quickly identify the root causes of errors and performance bottlenecks monitoring and analysis each request on a link and so on.

Second, how the project introduced Spring Cloud Sleuth assembly
1) increasing the spring-cloud-starter-sleuth-dependent

            <!-- sleuth-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-sleuth</artifactId>
                <version>2.0.3.RELEASE</version>
            </dependency>

2) a corresponding increase in project configuration

# Know the service name 
spring.application.name: Service -order 
# leuth specified sampling ratio of 1 for all of the parameters used to control the tracking information is sent to the zipkin on third-party services 
sleuth.sampler.probability: 1

3) modify the print format project log, the purpose is to print tracking information related to the traceid, spanid etc.

console-pattern: the console output log format
file-pattern: log file output format
console-pattern:  '%d{yyyy-MM-dd HH:mm:ss.SSS} [${spring.application.name:-},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] %boldYellow([%thread]) %highlight(%-5level) %boldGreen(%logger{50}.%M\(%F:%L\)) - %msg%n'
file-pattern: '%d{yyyy-MM-dd HH:mm:ss.SSS} [${spring.application.name:-},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}] [%thread] %-5level %logger{50} - %msg%n'

4) controller to increase the log output method and start the project to see the log printing effects, such as

log.info("test sleuth log");

Three, Sleuth related concepts introduced

1) Trace ID, SpanId equivalent description

Run the project and request, you will see this log, as follows:

2019-09-10 00:37:15.585  INFO [service-order,313fe940c4574c66,313fe940c4574c66,true] 5687 --- [io-10850-exec-1] c.zbq.order.controller.OrderController   : test sleuth log

The red marked trace log: [service-order, 313fe940c4574c66,313fe940c4574c66, true] 

It is an important part of these elements to implement distributed tracking service, the meaning of each value is as follows.

  • The first value: service-order, it records the name of the application, which is configured attributes or parameters application.yml bootstrap.yml in spring.application.name.
  • Second value: f410ab57afd5c145, Spring Cloud Sleuth generated by an ID, called Trace ID, which is used to identify a link request. A link request comprising a Trace ID, a plurality of Span ID.
  • Third value: a9f2118fa2019684, Spring Cloud Sleuth further generated an ID, called Span ID, which represents a basic unit of work, such as sending an HTTP request.
  • Fourth values: false, indicating whether you want to output the information to Zipkin and other services to collect and display.

Four values above the Trace ID and Span ID is the core Spring CloudSleuth implementation of distributed service tracking. In the process of calling a service request link, and passes will remain the same Trace ID, which will be distributed to the entire request process in different micro-service tracking information series. If a plurality of services belonging to the same front end service request source, they are of the same Trace ID, the request in the same link.

· X-B3-TraceId: a unique identification request link (the Trace), required value. · X-B3-SpanId: a unit of work (of Span) unique identifier of the required value. · X-B3-ParentSpanId: identifying the current unit of work on a working unit belongs, Root Span (first unit of work request link) the value is null. · X-B3-Sampled: whether the flag is sampled output, 1 needs to be output, 0 means do not need to be output. · X-Span-Name: name of the unit of work.

2) sample log information collected sleuth

Sleuth sample collection strategy is implemented through an interface Sampler
By default, Sleuth will use sampling strategies ProbabilityBasedSampler implemented to request a percentage of ways to configure and collect trace information. We can configure the following parameters in application.properties its set percentage value, it defaults to 0.1, representing 10% of the collected tracking information request. For example, we specified in the configuration:

sleuth.sampler.probability: 1


During development testing, usually gather all trace information and output to a remote repository, we can set its value to 1.

 

Guess you like

Origin www.cnblogs.com/756623607-zhang/p/11518683.html