Introducing problems

After all, writing code, there must be bugs, so we need to log view or need, but micro service view, we need a whole link tracking, otherwise we don’t know where the problem is, so we need to implement link log tracking.

Let’s get started

The first is to introduce the sleUTH dependency of our link tracking.

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
Copy the code

And then go to the API that we wrote before we visited, and let’s see what’s in the console.http://localhost:7000/product-serv/product/1?token=admin We can see that we have the same string here

[service-product,a5670094401b487b,de21187ddc169043,true]

[api-gateway,a5670094401b487b,a5670094401b487b,true]

After comparing several diagrams, you can see that the first one is the service name. There will be a same one in different ones, you can have a look

A5670094401b487b This is equivalent to a link, the following different ids are the unique ids of different services. The third is whether to export to a third party platform, in this case true is the output, in fact I have already configured the output to the Zipkin interface. Let’s look at the configuration:


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>
  zipkin:
    base-url: http:/ / 127.0.0.1:9411
    discovery-client-enabled: falseLet nacos treat it as a URL instead of a service namesender:
      type: web
  sleuth:
    web:
      client:
        enabled: true
    sampler:
      probability: 1.0# Sampling ratio is:0.1(i.e.10%), set the value between0.0to1.0Between,1.0It indicates that all data are collected.Copy the code

So it’s actually in memory, if you reboot it it’s gone, so how do you persist it, there are two main ones, one is mysql, ES and we’re going to show you mysql first, but you’re going to change it when you start it up. For example, in our original Windows Java-jar zipkin-server-2.23.16-exec.jar, if we need to persist the database, we will first create a persistent storage table. You can download it directly from the website:

Github.com/openzipkin/…

Copyright 2015-2019 The OpenZipkin Authors -- -- Licensed under The Apache License, Version 2.0 (The "License"); you may not use this file except -- in compliance with the License. You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under the License -- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express -- or implied. See the License for the specific language governing permissions and limitations under -- the License. -- CREATE TABLE IF NOT EXISTS zipkin_spans ( `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', `trace_id` BIGINT NOT NULL, `id` BIGINT NOT NULL, `name` VARCHAR(255) NOT NULL, `remote_service_name` VARCHAR(255), `parent_id` BIGINT, `debug` BIT(1), `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL', `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query', PRIMARY KEY (`trace_id_high`, `trace_id`, `id`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds'; ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames'; ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames'; ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range'; CREATE TABLE IF NOT EXISTS zipkin_annotations ( `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id', `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id', `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1', `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB', `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation', `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp', `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null', `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address', `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null', `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null' ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci; ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds'; ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames'; ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values'; ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values'; ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job'; CREATE TABLE IF NOT EXISTS zipkin_dependencies ( `day` DATE NOT NULL, `parent` VARCHAR(255) NOT NULL, `child` VARCHAR(255) NOT NULL, `call_count` BIGINT, `error_count` BIGINT, PRIMARY KEY (`day`, `parent`, `child`) ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;Copy the code

Java-jar zipkin-server-2.23.16-exec.jar –STORAGE_TYPE=mysql –MYSQL_HOST=127.0.0.1 –MYSQL_TCP_PORT=3306 –MYSQL_DB=zipkin –MYSQL_USER=root –MYSQL_PASS=123456

You can click on the database to see

The other option is es (full name of ElasticSearch)www.elastic.co/cn/download…Then decompress the download and go to the bin directory to start

java -jar zipkin-server-2.2316.-exec.jar --STORAGE_TYPE=elasticsearch --ES-HOST=localhost:9200
Copy the code

Then restart zipkin using the command above and restart to see the persistent data. So our persistence is done.

At this point, we this chapter of the log link tracking is completed later will continue to add in this project, like please click a start~ project source code reference branch 220301_xgc_Sleuth Gitee: gitee.com/coderxgc/sp… GitHub:github.com/coderxgc/sp…