9.SpringCloud-Sleuth

Spring Cloud Sleuth 功能是在分布式系統中提供服務調用追蹤。集成Zipkin提供了可視化界面。

1.創建SleuthZipkinServer工程添加依賴

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

<dependency>
   <groupId>io.zipkin.java</groupId>
   <artifactId>zipkin-server</artifactId>
</dependency>

<dependency>
   <groupId>io.zipkin.java</groupId>
   <artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>

<dependency>
   <groupId>io.zipkin.java</groupId>
   <artifactId>zipkin-ui</artifactId>
   <version>2.4.3</version>
</dependency>

2.修改配置文件

server:
  port: 8020

3.啟動類加入注解

@EnableZipkinServer

4.創建2個相互調用的工程
4.1 創建SleuthZipkina工程并添加依賴

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-zipkin</artifactId>
   <version>1.3.1.RELEASE</version>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-sleuth</artifactId>
   <version>1.3.1.RELEASE</version>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-sleuth-core</artifactId>
   <version>1.3.1.RELEASE</version>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-sleuth-zipkin</artifactId>
   <version>1.3.1.RELEASE</version>
</dependency>

<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.8.13</version>
</dependency>

4.2 修改工程a配置文件

server:
  port: 8021
spring:
  zipkin:
    base-url: http://localhost:8020
  application:
    name: service-hi

4.3 工程a啟動類

@SpringBootApplication
@RestController
public class SleuthzipkinaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SleuthzipkinaApplication.class, args);
    }

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @RequestMapping("/hi")
    public String callHome() {
        return restTemplate.getForObject("http://localhost:8022/miya", String.class);
    }

    @RequestMapping("/info")
    public String info() {
        return "i'm service-hi";
    }

    @Bean
    public AlwaysSampler defaultSampler(){
        return new AlwaysSampler();
    }

}

4.4 創建SleuthZipkinb工程并添加依賴

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-zipkin</artifactId>
   <version>1.3.1.RELEASE</version>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-sleuth-zipkin</artifactId>
   <version>1.3.1.RELEASE</version>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-sleuth</artifactId>
   <version>1.3.1.RELEASE</version>
</dependency>

4.5 修改工程b配置文件

server:
  port: 8022
spring:
  zipkin:
    base-url: http://localhost:8020
  application:
    name: service-miya

4.6 工程b啟動類

@SpringBootApplication
@RestController
public class SleuthzipkinbApplication {

   public static void main(String[] args) {
      SpringApplication.run(SleuthzipkinbApplication.class, args);
   }

   @RequestMapping("/hi")
   public String home(){
      return "hi i'm miya!";
   }

   @RequestMapping("/miya")
   public String info(){
      return restTemplate.getForObject("http://localhost:8021/info",String.class);
   }

   @Autowired
   private RestTemplate restTemplate;

   @Bean
   public RestTemplate getRestTemplate(){
      return new RestTemplate();
   }

}

5.按順序啟動三個工程
訪問http://localhost:8021/hi/ 頁面返回i'm service-hi,表示調用成功
訪問http://localhost:8020/

點列表中的請求查看詳情

點Service列表顯示請求信息

點依賴分析顯示如下圖

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,846評論 25 708
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,925評論 6 342
  • 和往常一樣下班匆匆的就回家了,姑娘正在做飯今天是南瓜粥還有紅燒雞翅,我特意從樓下買了一份小菜作為我們的下飯菜,一吃...
    笙筆記閱讀 333評論 0 0
  • [Array] $addToSet(each循環) 添加 $push(each循環) 追加 $pushAll 批量...
    liudongdong閱讀 680評論 0 2