springboot整合dubbo设置全局唯一ID进行日志追踪的示例代码
1.新建项目
利用idea创建一个父项目,三个子项目,其中一个项目为生产者,一个项目为消费者,一个为接口等公共服务项目,生产者和消费者需要有web依赖,可以作为tomcat容器启动。
2.项目依赖
<dependencies> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-spring-boot-starter</artifactid> <version>2.7.6</version> </dependency> <!-- zk的依赖 --> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-dependencies-zookeeper</artifactid> <version>2.7.6</version> <type>pom</type> <exclusions> <exclusion> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-aop</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> </dependency>
3.在facade项目中新建接口
4.编写生产者
4.1 增加dubbo配置
server.port=8081 dubbo.registry.address=zookeeper://localhost:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20880 dubbo.registry.timeout=30000 dubbo.application.name=dubbo-provider-ll
4.2 编写生产者dubbo filter
public class providerfilter implements filter { @override public result invoke(invoker<?> invoker, invocation invocation) throws rpcexception { object threadname= invocation.getattachment("threadname"); if(null!=threadname){ thread thread = thread.currentthread(); thread.setname(threadname.tostring()); } return invoker.invoke(invocation); } }
注意:此处filter 是dubbo的filter,不是servlet的filter
这里代码的目的是将从消费端传来的线程名称设置为线程名称
在resources目录下新建meta-inf/dubbo/com.alibaba.dubbo.rpc.filter 文件
即新增目录meta-inf/dubbo和文件 com.alibaba.dubbo.rpc.filter
在文件中增加,等号后面为实现dubbo filter的实现类路径
providerfilter=com.dubbo.spring.provider.filter.providerfilter
4.3编写dubbo生产者实现类
@service(filter = {"providerfilter"}) public class demoserviceimpl implements idemoservice { public logger logger= loggerfactory.getlogger(demoserviceimpl.class); @override public string getname() { logger.info("provider threadname : "+thread.currentthread().getname()); return "dubbo-test"; } }
5.编写消费者
5.1编写消费者filter
public class dubbofilter implements filter { @override public result invoke(invoker<?> invoker, invocation invocation) throws rpcexception { string name = thread.currentthread().getname(); invocation.setattachment("threadname",name); return invoker.invoke(invocation); } }
此处是将线程名称放入到attachment中,attachment底层是hashmap,后续使用dubbo请求生产者时,会把attachment给到生产者,故在生产中中可以通过key threadname来获取消费者端的线程名称
在resources目录下新建meta-inf/dubbo/com.alibaba.dubbo.rpc.filter 文件
即新增目录meta-inf/dubbo和文件 com.alibaba.dubbo.rpc.filter
在文件中增加,等号后面为实现dubbo filter的实现类路径
consumerfilter=com.dubbo.spring.consumer.filter.dubbofilter
5.2 编写response对象
public class response implements serializable { private static final long serialversionuid = -3186818832535757509l; private string code; private string message; private object result; private string index; public string getcode() { return code; } public void setcode(string code) { this.code = code; } public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } public object getresult() { return result; } public void setresult(object result) { this.result = result; } public string getindex() { return index; } public void setindex(string index) { this.index = index; } }
此response为web端返回到页面统一对象
5.3 编写aop切面
@aspect @component public class aopcontext { @before("execution(* com.dubbo.spring..*.*(..))") public void before(){ thread thread = thread.currentthread(); thread.setname(uuidutil.getuuid()); } @around("execution(* com.dubbo.spring..*.*(..))") public object around(proceedingjoinpoint pjp){ response response=new response(); try { object proceed = pjp.proceed(); if(proceed instanceof response){ response=(response) proceed; response.setindex(thread.currentthread().getname()); } } catch (throwable throwable) { throwable.printstacktrace(); } return response; } }
1.before是在请求进入时给线程设置名称,为随机生成的uuid
2.around是环绕通知,在执行完之后,在返回的结果中将线程名称设置进去,便于以后异常追踪
5.4 编写web
@restcontroller public class webcontroller { private logger logger= loggerfactory.getlogger(webcontroller.class); @reference(filter = {"consumerfilter"}) private idemoservice idemoservice; @getmapping("/getname") public response getname(){ logger.info("consumer threadname : "+thread.currentthread().getname()); string name = idemoservice.getname(); response response=new response(); response.setresult(name); response.setcode("1001"); response.setmessage("success"); return response; } }
请求结果
此处为postman响应的,index 为1ca55cb7a17148879923265b89102ccf
生产者线程名称:
消费者线程名称:
可以看到从web页面到生产者,消费者,都有一个全局唯一id进行贯穿,如果在web页面提示有异常时,可以通过这个uuid进行日志追踪
到此这篇关于springboot整合dubbo设置全局唯一id进行日志追踪的文章就介绍到这了,更多相关springboot整合dubbo内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!