sharding-jdbc中ShardingTracer的作用是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联公司长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为木垒哈萨克企业提供专业的成都做网站、成都网站制作、成都外贸网站建设,木垒哈萨克网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
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进行注册
ShardingTracerTest
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
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 publicvoid inject(final SpanContext spanContext, final Format format, final C carrier) { } @Override public SpanContext extract(final Format 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进行注册
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
当前名称:sharding-jdbc中ShardingTracer的作用是什么
地址分享:http://scyingshan.cn/article/gedghg.html