{"id":1967,"date":"2023-04-01T10:32:06","date_gmt":"2023-04-01T02:32:06","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1967"},"modified":"2023-04-22T08:34:17","modified_gmt":"2023-04-22T00:34:17","slug":"spring-cloud-asynchronous-traceid-empty","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/spring-cloud-asynchronous-traceid-empty\/","title":{"rendered":"Spring Cloud \u5f02\u6b65traceId\u4e3a\u7a7a\u89e3\u51b3"},"content":{"rendered":"<p>Spring Cloud \u5f02\u6b65\u6d41\u7a0btraceId\u548cspanId\u4e3a\u7a7a\uff0c\u5982EventBus\u6216MQ\u7b49\u5f02\u6b65\u64cd\u4f5c\u7684traceId\u548cspanId\u4e3a\u7a7a\uff0c\u9700\u8981\u624b\u52a8\u751f\u6210traceId\u548cspanId<\/p>\n<pre><code class=\"language-java\">@Slf4j\npublic abstract class MyJob implements SimpleJob {\n\n    @Override\n    public final void execute(ShardingContext shardingContext) {\n\n        log.info(String.format(&quot;\u4efb\u52a1\u603b\u7247\u6570: %s, \u5f53\u524d\u5206\u7247\u9879: %s, \u5206\u7247\u53c2\u6570: %s&quot;, shardingContext.getShardingTotalCount(),\n                shardingContext.getShardingItem(), shardingContext.getShardingParameter()));\n\n        TraceHelper.newTrace();\n\n    }\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">@Slf4j\n@Component\npublic class TraceHelper {\n\n    private static Tracer tracer;\n    private static Environment env;\n\n    @Autowired\n    public void setTracer(Tracer tracer) {\n        TraceHelper.tracer = tracer;\n    }\n\n    @Autowired\n    public void setEnv(Environment env) {\n        TraceHelper.env = env;\n    }\n\n    public static void newTrace() {\n        tracer.startScopedSpanWithParent(getApplicationName(), tracer.newTrace().context());\n    }\n\n    private static boolean isNumber(String str) {\n        return str.matches(&quot;-?\\\\d+&quot;);\n    }\n\n    private static String getApplicationName() {\n        return env.getProperty(&quot;spring.application.name&quot;, &quot;&quot;);\n    }\n\n    public static long getTraceId() {\n        return tracer.currentSpan().context().traceId();\n    }\n\n    public static long getSpanId() {\n        return tracer.currentSpan().context().spanId();\n    }\n\n    public static long getParentId() {\n        return tracer.currentSpan().context().parentId();\n    }\n\n    public static boolean getSampled() {\n        return tracer.currentSpan().context().sampled();\n    }\n\n    public static String getTraceIdString() {\n        return tracer.currentSpan().context().traceIdString();\n    }\n\n    public static String getSpanIdString() {\n        return tracer.currentSpan().context().spanIdString();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring Cloud \u5f02\u6b65\u6d41\u7a0btraceId\u548cspanId\u4e3a\u7a7a\uff0c\u5982EventBus\u6216MQ\u7b49\u5f02\u6b65\u64cd\u4f5c\u7684tra [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[494],"class_list":["post-1967","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-494"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1967"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1967\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}