Motan+Zipkin+Brave鏈路跟蹤初探

0.Why Zipkin

隨着業務發展,系統拆分致使系統調用鏈路愈發複雜一個前端請求可能最終須要調用不少次後端服務才能完成,當整個請求變慢或不可用時,咱們是沒法得知該請求是由某個或某些後端服務引發的,這時就須要解決如何快讀定位服務故障點,以對症下藥。因而就有了分佈式系統調用跟蹤的誕生。而zipkin就是開源分佈式系統調用跟蹤的佼佼者前端

zipkin基於google-Dapper的論文有興趣的能夠看下java

1. 部署zipkin

快速上手

  1. 上zipkin官網把可運行jar包下下來
    zipkin latest release
  2. 運行
    java -jar zipkin.jar

改變存儲方式

zipkin默認會將鏈路跟蹤數據保存到內存,同時也支持MySql、ElasticSearch等多種持久化插件mysql

clipboard.png

以MySql爲例,只需在啓時加上如下參數便可:spring

java -jar zipkin.jar --zipkin.storage.StorageComponent=mysql
--zipkin.storage.type=mysql --zipkin.storage.mysql.host=127.0.0.1--zipkin.storage.mysql.port=3306 --zipkin.storage.mysql.username=root --zipkin.storage.mysql.password=root --zipkin.storage.mysql.db=zipkin_test
--zipkin.storage.StorageComponent=mysql #使用mysql組件
--zipkin.storage.type=mysql #使用mysql持久化
--zipkin.storage.mysql.host #mysql數據庫域名
--zipkin.storage.mysql.port=3306 #mysql數據庫端口
--zipkin.storage.mysql.username=root #數據庫帳號
--zipkin.storage.mysql.password=root #數據庫密碼
--zipkin.storage.mysql.db=zipkin_test #庫名

2.服務集成

本文使用Brave做爲Tracer實現,使用spring-cloud的小夥伴能夠選擇spring-cloud-sleuth

引入依賴

引入Bravesql

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave-bom</artifactId>
            <version>${brave.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependency>
    <groupId>io.opentracing.brave</groupId>
    <artifactId>brave-opentracing</artifactId>
    <version>0.31.0</version>
</dependency>
<dependency>
    <groupId>io.zipkin.reporter2</groupId>
    <artifactId>zipkin-sender-okhttp3</artifactId>
    <version>2.6.1</version>
</dependency>

引入filter-opentracing模塊shell

<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>filter-opentracing</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.opentracing</groupId>
            <artifactId>opentracing-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
此處要exclud掉opentracing-api防止filter-opentracing跟brave的依賴衝突

實現TracerFactory

public class BraveTarcerFactory implements TracerFactory {
    private static Tracer braveTracer = BraveTracer.create(Tracing.newBuilder()
            // 設置當前服務的服務名,需作成配置化
            .localServiceName("goods-service")
            // 此處採用OkHttpSender經過HTTP請求的方式發送跟蹤數據,Zipkin的日誌接口地址需作成配置化的
            .spanReporter(AsyncReporter.create(OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans")))
            .build());
    @SuppressWarnings("deprecation")
    @Override
    public Tracer getTracer() {
        return braveTracer;
    }
}

將TracerFactory註冊到openTracingContext

@Bean
BraveTarcerFactory tarcerFactory() {
    return new BraveTarcerFactory();
}

@Bean
OpenTracingContext openTracingContext(TracerFactory tracerFactory) {
    OpenTracingContext context = new OpenTracingContext();
    context.setTracerFactory(tracerFactory);
    return context;
}

記得註冊filter噢~

@Bean(name = "motanClientBasicConfig")
@ConditionalOnMissingBean
public BasicRefererConfigBean baseRefererConfig() {
    BasicRefererConfigBean config = new BasicRefererConfigBean();
    config.setFilter("opentracing");
    return config;
}

@Bean(name = "motanServerBasicConfig")
@ConditionalOnMissingBean
public ServerBasicConfig() {
    BasicRefererConfigBean config = new BasicRefererConfigBean();
    config.setFilter("opentracing");
    return config;
}

3. 走你!

zipkin默認監聽9411端口,經過瀏覽器訪問localhost:9441便可進入他的UI頁面,點擊Find Traces便可看到跟蹤結果:數據庫

圖片描述

4. SpringClould全家桶萬歲!

由於項目沒有用SpringCloud,本身折騰Brave的依賴真真蛋痛,想要方便仍是得上SpringCloud全家桶啊!後端

相關文章
相關標籤/搜索