您好,登錄后才能下訂單哦!
sharding-jdbc中ShardingTracer的作用是什么,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
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方法會(huì)重系統(tǒng)properties中讀取OPENTRACING_TRACER_CLASS_NAME的值,然后使用Class.forName加載并創(chuàng)建實(shí)例,然后在通過opentracing的GlobalTracer.register進(jìn)行注冊(cè)
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實(shí)現(xiàn)了Tracer接口,這里為空實(shí)現(xiàn)
ShardingTracer的init方法會(huì)重系統(tǒng)properties中讀取OPENTRACING_TRACER_CLASS_NAME的值,然后使用Class.forName加載并創(chuàng)建實(shí)例,然后在通過opentracing的GlobalTracer.register進(jìn)行注冊(cè)
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。