sharding-jdbc中ShardingTracer的作用是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/main/java/org/apache/shardingsphere/opentracing/ShardingTracer.java
@NoArgsConstructor(access = AccessLevel.PRIVATE) public final class ShardingTracer { private static final String OPENTRACING_TRACER_CLASS_NAME = "org.apache.shardingsphere.opentracing.tracer.class"; /** * Initialize sharding tracer. */ public static void init() { String tracerClassName = System.getProperty(OPENTRACING_TRACER_CLASS_NAME); Preconditions.checkNotNull(tracerClassName, "Can not find opentracing tracer implementation class via system property `%s`", OPENTRACING_TRACER_CLASS_NAME); try { init((Tracer) Class.forName(tracerClassName).newInstance()); } catch (final ReflectiveOperationException ex) { throw new ShardingException("Initialize opentracing tracer class failure.", ex); } } /** * Initialize sharding tracer. * * @param tracer opentracing tracer */ public static void init(final Tracer tracer) { if (!GlobalTracer.isRegistered()) { GlobalTracer.register(tracer); } } /** * Get tracer. * * @return tracer */ public static Tracer get() { return GlobalTracer.get(); } }
ShardingTracer的init方法会重系统properties中读取OPENTRACING_TRACER_CLASS_NAME的值,然后使用Class.forName加载并创建实例,然后在通过opentracing的GlobalTracer.register进行注册
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/test/java/org/apache/shardingsphere/opentracing/ShardingTracerTest.java
public final class ShardingTracerTest { @Before public void setUp() { System.setProperty("org.apache.shardingsphere.opentracing.tracer.class", FooTracer.class.getName()); clearGlobalTracer(); } @After public void tearDown() { System.getProperties().remove("org.apache.shardingsphere.opentracing.tracer.class"); } @Test public void assertDuplicatedLoading() { ShardingTracer.init(mock(Tracer.class)); Tracer t1 = ShardingTracer.get(); ShardingTracer.init(); assertEquals(t1, ShardingTracer.get()); ShardingTracer.init(mock(Tracer.class)); assertEquals(t1, ShardingTracer.get()); } @Test public void assertTracer() { ShardingTracer.init(); assertThat((GlobalTracer) ShardingTracer.get(), isA(GlobalTracer.class)); assertTrue(GlobalTracer.isRegistered()); assertThat(ShardingTracer.get(), is(ShardingTracer.get())); } @Test(expected = ShardingException.class) public void assertTracerClassError() { System.setProperty("org.apache.shardingsphere.opentracing.tracer.class", "com.foo.FooTracer"); ShardingTracer.init(); } @SneakyThrows private static void clearGlobalTracer() { Field tracerField = GlobalTracer.class.getDeclaredField("tracer"); tracerField.setAccessible(true); tracerField.set(GlobalTracer.class, NoopTracerFactory.create()); } }
这里指定了OPENTRACING_TRACER_CLASS_NAME值为FooTracer
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/test/java/org/apache/shardingsphere/opentracing/fixture/FooTracer.java
public final class FooTracer implements Tracer { @Override public SpanBuilder buildSpan(final String operationName) { return null; } @Override public <C> void inject(final SpanContext spanContext, final Format<C> format, final C carrier) { } @Override public <C> SpanContext extract(final Format<C> format, final C carrier) { return null; } @Override public ActiveSpan activeSpan() { return null; } @Override public ActiveSpan makeActive(final Span span) { return null; } }
FooTracer实现了Tracer接口,这里为空实现
ShardingTracer的init方法会重系统properties中读取OPENTRACING_TRACER_CLASS_NAME的值,然后使用Class.forName加载并创建实例,然后在通过opentracing的GlobalTracer.register进行注册
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。