0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Zipkinを用いてSpringBootアプリケーションをトレーシング

Posted at

概要

Zipkinを触ってみたので導入方法をメモしておきます

トレーシング対象のアプリケーションはSpringBootです。

導入方法

最小構成で、とりあえず動くところを目指します

Zipkinサーバ構築

Zipkinサーバを起動します。

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

http://localhost:9411/ にアクセスして以下のような画面が表示されればOK!

スクリーンショット 2022-03-12 14.15.11.png

アプリケーションの実装

依存にspring-cloud-sleuth-zipkinspring-cloud-starter-sleuthを含めるようにします

guild.gradle
dependencies {
    compileOnly('org.projectlombok:lombok')
    annotationProcessor('org.projectlombok:lombok')

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
    implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
}

HTTP通信でRestTemplateを使用します。

Jipkinへ連携するためにはRestTemplateを必ずBean登録する必要があります

@Configuration
public class ApplicationConfiguration {
    @Bean
    public RestTemplate myRestTemplate() {
        return new RestTemplateBuilder()
                .rootUri("http://localhost:8080")
                .build();
    }
}

アプリケーション本体は以下の通り。

/proxy/helloで受け付けたリクエストを/helloにproxyさせるだけのAPIです。

@RestController
@RequiredArgsConstructor
public class SampleController {
    private final RestTemplate restTemplate;

    @GetMapping("/proxy/hello")
    public String proxyToHello() {
        return restTemplate.getForObject("/hello", String.class);
    }

    @GetMapping("/hello")
    public String hello() {
        return "Hello zipkin.";
    }
}

最後にpropertyです

application.yml
spring:
  zipkin:
    # zipkinサーバのURLを指定
    base-url: "http://localhost:9411/"
    # Zipkinを有効化(デフォルトでも有効)
    enabled: true
    sender:
      # 情報の送信方法を指定
      type: web
  application:
    # アプリケーション名を指定
    name: zipkin-sample
  sleuth:
    sampler:
      # 秒間の送信数
      rate: 10

bootRunでアプリケーションを起動して終わり

動作確認

それではアプリケーションにリクエストを送信してみます

$ curl localhost:8080/proxy/hello
Hello zipkin.

JipkinのUIで見てみると...

スクリーンショット 2022-03-12 14.34.26.png

🎉🎉🎉🎉

あとがき

実運用する場合はJipkinでデータの永続化ができるようにとか意識しなければならないことはまだありますが、
とりあえずどんな感じか触ってみるところまでできました。

時間があったらこんなこともしてみたい

  • 処理時間が遅いものだけ送信
  • 任意の処理をトレーシングしてみる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?