dubbo透传traceId链路追踪

11 篇文章 1 订阅
4 篇文章 0 订阅

文章简介

dubbo的配置,filter的使用,自动透传traceId链路追踪,threadLocal的理解使用,优雅的代码,不乱传参。。。

工程结构

maven搭建环境:
dubbo: 顶级项目,做parent
interface: 提供接口支持,工具支持,filter支持。。。
web:消费者
service:生产者
web和service都依赖interface

核心配置

web模块的dubbo配置

	<dubbo:application name="web" />
	<!--你得装一个zookeeper,填好ip port-->
    <dubbo:registry address="zookeeper://192.168.235.100:2181" />
    <!--注解方式,代表这个包下的controller可以用@Reference获得服务-->
    <dubbo:annotation package="com.lry.controller" />

service模块的dubbo配置

	<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    <dubbo:application name="service"/>
    <dubbo:registry address="zookeeper://192.168.235.100:2181"/>
    <!--注解方式,代表我要把这个包下的加了@Service(dubbo提供的注解)的类代理给dubbo-->
    <dubbo:annotation package="com.lry.service.impl" />

interface模块filter的配置
在interface模块的资源包resources建 /META-INF/dubbo文件夹
在这个文件夹下建个文件名字为com.alibaba.dubbo.rpc.Filter文件内容如下

webFilter=com.lry.util.WebFilter
serviceFilter=com.lry.util.ServiceFilter

核心pom配置

 <!-- dubbo begin -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.jboss.netty</groupId>
          <artifactId>netty</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- dubbo end -->
    <!-- 注册中心zookeeper begin -->

    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.8</version>
    </dependency>

    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>

    <!-- 注册中心zookeeper end -->

核心代码

web模块代码

HelloController

@Controller
public class HelloController {
    @Reference//记得用这个
    public OrderService orderService;// dubbo远程调用
    @Autowired
    public UserService userService;//本地调用
    @RequestMapping("hello")
    public void hello(){
        String traceId = "aewrv26546sca21fdsaf54gdfgs";
        System.out.println("controller生成的项圈:"+traceId);
        ThreadLocalUtil.set(traceId);
        System.out.println(orderService.sayHello());
        System.out.println(userService.hello());
    }
}

UserService

@Service
public class UserService {
    public String hello(){
        System.out.println("本地调用的项圈:"+ ThreadLocalUtil.get());
        return "hello spring";
    }
}

service模块代码

OrderServiceImpl

@Service
public class OrderServiceImpl implements OrderService {
    @Override
        public String sayHello() {
        System.out.println("远程调用的项圈:"+ ThreadLocalUtil.get());
        return "hello dubbo";
    }
}

interface模块代码

ThreadLocalUtil

public class ThreadLocalUtil {
    private static ThreadLocal<String> threadLocal = new ThreadLocal();

    public static void set(String traceId){
        threadLocal.set(traceId);
    }
    public static String get(){
       return threadLocal.get();
    }
}

OrderService接口

public interface OrderService {
     String sayHello();
}

WebFilter

//从当前线程拿出traceId,放入RpcContext
@Activate(group= {Constants.CONSUMER})
public class WebFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String traceId = ThreadLocalUtil.get();
        System.out.println("WebFilter:"+traceId);
        RpcContext.getContext().setAttachment("traceId",traceId);
        return invoker.invoke(invocation);
    }
}

ServiceFilter

//从RpcContext拿出traceId,放入当前线程
@Activate(group= {Constants.PROVIDER})
public class ServiceFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String traceId = RpcContext.getContext().getAttachment("traceId");
        System.out.println("ServiceFilter:"+traceId);
        ThreadLocalUtil.set(traceId);
        return invoker.invoke(invocation);
    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值