Distributed link tracking log Dubbo

Technology Scene

In everyday development, testing, or operation and maintenance, it is often the presence of such a scenario, developers use the logging facility (log4j, slf4j) log in the code, such as the request ID, IP, etc., a convenient online fast, accurate positioning issue, clearly positioning information through complete log link. The projects are generally hierarchical, distributed in a number of log information, how to tell which log information is sent to the same request, detailed implementation follows.

Technical Framework

Project framework: Spring boot
Distributed Coordination: Zookeeper, Dubbo
logging tools: Sf4j
build tools: Maven
development tools: IDEA

Framework of the project

Project structure
mdc-dubbo-api: Interface services
mdc-dubbo-provider: server service
mdc-dubbo-consumer: the consumer side service

Project Configuration

mdc-dubbo-api

It provides an interface

public interface OrderService {
String getOrder(String orderid);
}

mdc-dubbo-consumer

In the server, using the tools in the Controller MDC layer into a TRACE_LOG_ID information in this request the service layer, mdc-dubbo-provider using this information.
Server project structure
Project is divided into layers Controller, Service, Filter and so on:
the Controller layer: storage TRACE_LOG_ID, print

@GetMapping("get/{id}")
    public String get(@PathVariable("id") String id){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        MDC.put(Constants.TRACE_LOG_ID, uuid);
        LOGGER.info("controller->param:{}", id);
        return consumerService.getName(id);
    }

Service Layer: Print TRACE_LOG_ID

@Override
    public String getName(String id) {
        LOGGER.info("consumer->service->param:{}", id);
        return orderService.getOrder(id);
    }

Filter:

public class TraceFilter implements Filter {
@Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        //从MDC中获取
        String logId = MDC.get(Constants.TRACE_LOG_ID);
        Map<String, String> attachments = invocation.getAttachments();
        attachments.put(Constants.TRACE_LOG_ID, logId);
        return invoker.invoke(invocation);
    }

A filter disposed in need resources-> MATE-INF-> dobbo file folder
! [Filter configuration https://img-blog.csdnimg.cn/20181219100406136.png)
TraceFilter = com.bestpay.provider.filter.TraceFilter
this at attention here to use in Dubbo spi mechanism, the file name must be com.alibaba.dubbo.rpc.Filter
Dubbo profile

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--过滤器配置-->
   <!-- <dubbo:provider filter="traceFilter" />-->
    <!-- 应用名-->
    <dubbo:application name="dmc-dubbo-provider"/>
    <!--zookeeper注册中心-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <dubbo:protocol name="dubbo" port="20880"/>
    <!--服务注册-->
    <dubbo:service interface="com.bestpay.service.OrderService" ref="orderService" timeout="10000" **filter="traceFilter"**/>
    <bean id="orderService" class="com.bestpay.provider.service.OrderServiceImpl" />

</beans>

Wherein the filter = "traceFilter is a reference to the key configuration directory dobbo

logback Configuration

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] %logger %line --%mdc{client} [%X{TRACE_LOG_ID}] %msg%n</pattern>
            <!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
            <charset>UTF-8</charset>
        </encoder>
    </appender>

TRACE_LOG_ID corresponding to the key into the MDC

mdc-dubbo-provider

Configuration and the like ## mdc-dubbo-consumer, which is slightly different in some Filter

public class TraceFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String logId = invocation.getAttachment(Constants.TRACE_LOG_ID);
        MDC.put(Constants.TRACE_LOG_ID, logId);
        return invoker.invoke(invocation);
    }
}

Project run

mdc-dubbo-consumer日志:
[2018-12-19 10:16:56] [INFO ] com.bestpay.comsumer.controller.ConsumerController 33 – [d88ecba6581c47b1b3ade78d2821d13a] controller->param:223
[2018-12-19 10:16:56] [INFO ] com.bestpay.comsumer.service.impl.ComsumerServiceImpl 20 – [d88ecba6581c47b1b3ade78d2821d13a] consumer->service->param:223

mdc-dubbo-provider日志:
[2018-12-19 10:16:56] [INFO ] com.bestpay.provider.service.OrderServiceImpl 13 – [d88ecba6581c47b1b3ade78d2821d13a] provider->service->param:223

Above, to complete the full log link between dubbo distributed services.

About the Author : Orange worked Finance IT, responsible for server-side development, focusing on micro-services, distributed, performance tuning, high availability, to welcome you peer communication.

Released four original articles · won praise 17 · views 2575

Guess you like

Origin blog.csdn.net/weixin_39178876/article/details/85088410