https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-tutorials</artifactId> <groupId>com.bolingcavalry</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>jaeger-annonation-demo</artifactId> <dependencies> <dependency> <groupId>com.bolingcavalry</groupId> <artifactId>common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.opentracing.contrib</groupId> <artifactId>opentracing-spring-jaeger-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layers> <enabled>true</enabled> </layers> </configuration> </plugin> </plugins> </build> </project>
spring: application: name: jaeger-annonation-demo opentracing: jaeger: enabled: true udp-sender: host: 127.0.0.1 port: 6831 server: port: 18080
package com.bolingcavalry.annonation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AnnonationDemoApplication { public static void main(String[] args) { SpringApplication.run(AnnonationDemoApplication.class, args); } }
package com.bolingcavalry.annonation.service; public interface ChildBiz { void mockChild(); }
package com.bolingcavalry.annonation.service.impl; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Component @Slf4j public class ChildBizImpl implements ChildBiz { @Override public void mockChild() { log.info("mockChild"); } }
package com.bolingcavalry.annonation.service; public interface Biz { void mock(); }
package com.bolingcavalry.annonation.service.impl; import com.bolingcavalry.annonation.service.Biz; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Component @Slf4j public class BizImpl implements Biz { final ChildBiz childBiz; public BizImpl(ChildBiz childBiz) { this.childBiz = childBiz; } @Override public void mock() { log.info("mock"); childBiz.mockChild(); } }
定义两个注解,一个用于创建span,另一个用于创建子span:
注解MySpan用于希望全新创建一个span的场景:
package com.bolingcavalry.annonation.aop; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented public @interface MySpan { String spanName() default ""; }
package com.bolingcavalry.annonation.aop; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented public @interface MyChildSpan { String spanName() default ""; }
package com.bolingcavalry.annonation.aop; import io.opentracing.Span; import io.opentracing.Tracer; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component @Slf4j public class SpanAspect { private Tracer tracer; public SpanAspect(Tracer tracer) { this.tracer = tracer; } /** * 取得真实方法的相关信息 * @param proceedingJoinPoint * @return */ private static String getOperationDesc(ProceedingJoinPoint proceedingJoinPoint) { Signature signature = proceedingJoinPoint.getSignature(); // 提取类名 return StringUtils.substringAfterLast(signature.getDeclaringTypeName(), ".") + ":" + signature.getName(); } @Around("@annotation(mySpan)") public Object traceSpan(ProceedingJoinPoint proceedingJoinPoint, MySpan mySpan) throws Throwable { // 类名:方法名 String operationDesc = getOperationDesc(proceedingJoinPoint); // 看方法的注解中有没有设置name String name = mySpan.spanName(); // 如果没有设置name,就给span一个默认name if (StringUtils.isEmpty(name)) { name = "span-aspect-" + operationDesc; } // 创建一个span,在创建的时候就添加一个tag Span span = tracer.buildSpan(name).start(); // span日志 span.log("span log of " + operationDesc); // 增加一个tag span.setTag("operation-desc", operationDesc); // span结束 span.finish(); return proceedingJoinPoint.proceed(); } @Around("@annotation(myChildSpan)") public Object traceChildSpan(ProceedingJoinPoint proceedingJoinPoint, MyChildSpan myChildSpan) throws Throwable { // 类名:方法名 String operationDesc = getOperationDesc(proceedingJoinPoint); // 看方法的注解中有没有设置name String name = myChildSpan.spanName(); // 如果没有设置name,就给span一个默认name if (StringUtils.isEmpty(name)) { name = "child-span-aspect-" + operationDesc; } // 从上下文中取得已存在的span Span parentSpan = tracer.activeSpan(); if (null==parentSpan) { log.error("can not get span from context"); } else { Span span = tracer.buildSpan(name).asChildOf(parentSpan).start(); // span日志 span.log("child span log of " + operationDesc); // 增加一个tag span.setTag("child-operation-desc", operationDesc); // span结束 span.finish(); } return proceedingJoinPoint.proceed(); } }
docker run -d \ --name jaeger \ -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \ -p 5775:5775/udp \ -p 6831:6831/udp \ -p 6832:6832/udp \ -p 5778:5778 \ -p 16686:16686 \ -p 14268:14268 \ -p 14269:14269 \ -p 9411:9411 jaegertracing/all-in-one:1.26
启动应用jaeger-annonation-demo
浏览器访问http://localhost:18080/hello,触发web接口类和service类的执行
打开jaeger的web页面,地址是http://localhost:16686/search,如下图,可见trace已经上报了:
我是欣宸,期待与您一同畅游Java世界…