您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。