Spring Boot 2.0 + zipkin 分布式跟蹤系統(tǒng)快速入門

注意

Spring Boot 2.0之后,使用EnableZipkinServer創(chuàng)建自定義的zipkin服務(wù)器已經(jīng)被廢棄,將無法啟動
具體可看issue
Deprecates EnableZipkinServer to explain custom servers are unsupported

開始

搭建zipkin服務(wù)器

獲取最新發(fā)布的zipkin服務(wù)器紊遵,一個可執(zhí)行的jar

curl -sSL https://zipkin.io/quickstart.sh | bash -s

然后啟動它

java -jar zipkin.jar

也可以通過docker來啟動

docker run -d -p 9411:9411 openzipkin/zipkin

啟動后就可以在瀏覽器打開http://your_host:9411/zipkin/查看了。

創(chuàng)建兩個服務(wù)

pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <sleuth.version>2.0.0.RELEASE</sleuth.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Sleuth automatically adds trace interceptors when in the classpath -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <!-- Sends trace data to zipkin over http (defaults to http://localhost:9411/api/v2/spans) -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-sleuth</artifactId>
                <version>${sleuth.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Backend.java

@EnableAutoConfiguration
@RestController
public class Backend {

  @RequestMapping("/api") public String printDate() {
    return new Date().toString() + " by hongxi";
  }

  public static void main(String[] args) {
    SpringApplication.run(Backend.class,
        "--spring.application.name=backend",
        "--server.port=9000"
    );
  }
}

Frontend.java

@EnableAutoConfiguration
@RestController
@CrossOrigin // So that javascript can be hosted elsewhere
public class Frontend {

  @Autowired RestTemplate restTemplate;

  @RequestMapping("/") public String callBackend() {
    return restTemplate.getForObject("http://localhost:9000/api", String.class);
  }

  @Bean RestTemplate restTemplate() {
    return new RestTemplate();
  }

  public static void main(String[] args) {
    SpringApplication.run(Frontend.class,
        "--spring.application.name=frontend",
        "--server.port=8081"
    );
  }
}

application.properties

spring.sleuth.traceId128=true
spring.sleuth.sampler.probability=1.0

然后分別啟動Backen和Frontend侥蒙,在瀏覽器訪問http://localhost:8080/暗膜,可以看到頁面顯示

Wed Jun 20 16:47:00 CST 2018 by hongxi

說明調(diào)用成功,然后回到http://your_host:9411/zipkin/點擊大大的那個按鈕“Find Traces”即可看到調(diào)用跟蹤信息鞭衩。

WX20180620-164932@2x.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末学搜,一起剝皮案震驚了整個濱河市娃善,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌瑞佩,老刑警劉巖聚磺,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異炬丸,居然都是意外死亡瘫寝,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門御雕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來矢沿,“玉大人滥搭,你說我怎么就攤上這事酸纲。” “怎么了瑟匆?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵闽坡,是天一觀的道長。 經(jīng)常有香客問我愁溜,道長疾嗅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任冕象,我火速辦了婚禮代承,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘渐扮。我一直安慰自己论悴,他們只是感情好,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布墓律。 她就那樣靜靜地躺著膀估,像睡著了一般。 火紅的嫁衣襯著肌膚如雪耻讽。 梳的紋絲不亂的頭發(fā)上察纯,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機與錄音针肥,去河邊找鬼饼记。 笑死,一個胖子當著我的面吹牛慰枕,可吹牛的內(nèi)容都是我干的具则。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼捺僻,長吁一口氣:“原來是場噩夢啊……” “哼乡洼!你這毒婦竟也來了崇裁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤束昵,失蹤者是張志新(化名)和其女友劉穎拔稳,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锹雏,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡巴比,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了礁遵。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片轻绞。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖佣耐,靈堂內(nèi)的尸體忽然破棺而出政勃,到底是詐尸還是另有隱情,我是刑警寧澤兼砖,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布奸远,位于F島的核電站,受9級特大地震影響讽挟,放射性物質(zhì)發(fā)生泄漏懒叛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一耽梅、第九天 我趴在偏房一處隱蔽的房頂上張望薛窥。 院中可真熱鬧,春花似錦眼姐、人聲如沸诅迷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽竟贯。三九已至,卻和暖如春逝钥,著一層夾襖步出監(jiān)牢的瞬間屑那,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工艘款, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留持际,地道東北人。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓哗咆,卻偏偏與公主長得像蜘欲,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子晌柬,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容