Because each service in a microservice architecture may be distributed on different servers, a solution for distributed logging is required. Spring-cloud provides a component called Sleuth for tracing services. It can obtain service dependencies through logging. Based on SLEUTH, existing logging tools can be used to collect distributed logs.

ELK is elasticSearch, Logstash, kibana.

A sleuth,

Step 1: SLEUTH management end

Sleuth is generally placed in a separate project. You need to add the following dependencies

Configure the address of the service registryCopy the code

The startup class adds service discovery annotations and Zipkin annotations, as shown below

At this point, launch and access the address of the microservice, and you’ll see the Zipkin administration page

Step 2: Managed microservers

Our other microservers require a simple configuration that is managed by Zipkin

Introduction of depend on

Add the following configuration

Spring. The sleuth. Sampler. Percentage with: the meaning of this parameter is to grab 100% of the logs, only by grasping log can learn dependencies. However, if the log is always captured, performance will be affected, so you can configure it yourself. Typically, this value is set to 1 in a development environment and depends on the production environment.

Spring.zipkin.base-url: address of the zipkin management microservice configured for the first step

Now launch the service registry, gateway, required microservices, and SLEUTH respectively.

Call a random microservice

Then we can see the associated trace log

We can also see the dependencies between microservices. Here the myService-consumer-Feign microservice is called through the gateway, and then the myService-Provider microservice is called through the myService-consumer-Feign microservice

2. Build ELK

Install and configure elasticSearch as a single point, cluster, and head plugin. I will not summarize here.

2, Kibana installation, nothing to say, decompression, run it

3, logstash installation, decompress

Create a configuration file under config

In the command, port indicates the port number, codec indicates that elasticSearch is in JSON format, and hosts indicates the address of elasticSearch. Index Indicates the elasticSearch index of log storage.

You need to invoke the logstash command under bin to specify the configuration file through -f

Use Kibana

Enable ElasticSearch, Head, Kibana, logStash

Create index applog

To configure applog into Kibana, enter our applog index in the index Pattern

Finally, click Create

Click Discover in the menu to view the log

3. Configure logBack

Spring-cloud and Logstash support Logback, so you need to configure the corresponding logback-spring.xml for microservices

It is worth noting here that in spring-boot, logback-spring. XML is loaded before application.yml. We need to use spring.application.name in logback-spring.xml. Therefore, we need to add the spring.application.name configuration to bootstrap.yml.

The loading sequence is bootstrap.yml, logback-spring. XML, and application.yml

Instead of normal logback-spring.xml, we configure the spring.application. Name, logstash appender

Here is an example of logback-spring.xml

192.168.160.66:4560 UTC {” Severity “:”%level”, “service”: “springAppName:−”,”trace”:”{springAppName:-}”, “trace”: “%X{X-B3-TraceId:-}”, “span”: “%X{X-B3-SpanId:-}”, “exportable”: “%X{X-Span-Export:-}”, “pid”: “SpringAppName: -“, “trace” : “{PID: -}”, “thread” : “% thread”, “class” : “% logger {40}”, “rest” : “% message”}

We configure the Message message into the REST field.

4. Query logs

Start the service registry, gateway, required microservices, and SLEUTH.

Start ElasticSearch, Head, Kibana, Logstash and run any service you like

The myService-Provider userController is called through the gateway

The Eclipse console outputs logs

Search for logs in Kibana

We see the log information in the REST field. In addition, trace and SPAN allow you to trace the entire microservice invocation process. At this point, the entire log collection is completed. After the system is online, you can search the log content of each micro-service on each server by clicking ElasticSearch.