为了调查微服务,我们将在Rails的示例应用程序中安装Zipkin的跟踪器并进行操作验证!

想做的事情

在之前的帖子中,我使用docker-compose,在单个实例上构建了OpenZipkin服务。

我对于如何在这里注册追踪目标应用程序的信息感到好奇,所以我决定尝试使用Ruby的gem。

    https://github.com/openzipkin/zipkin-tracer

2. 尝试利用存储库

    https://github.com/letusfly85/sample-zipkin-rails

由于已将zipkin tracer的IP写入config/application.rb中,所以我没有提交application.rb文件。我将在后面进行掩码处理。

3. 进行步骤

3-1. 创建Rails应用程序

rails new app
cd app

3-2. 编辑Gemfile文件并执行bundle install命令。

vim Gemfile
bundle install

我在Gemfile中添加了以下两行代码。

gem 'zipkin-tracer'
gem 'faraday', '~> 0.8'

在config/application.rb中添加用于Zipkin的service_name、service_port和json_api_host的注册。

服务名称设为 “hoge app”。

module App
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    zipkin_tracer_config = {service_name: 'hoge app', service_port: 3000, sample_rate: 1, json_api_host: 'http://*.*.*.*:9411'}

    config.middleware.use ZipkinTracer::RackHandler, zipkin_tracer_config


    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
  end
end

这里有点难懂,

json_api_port的值9411是zipkin docker-compose.yml文件中zipkin-query服务所提供的端口。

query:
  image: openzipkin/zipkin-query:1.33.2
  environment:
    # Remove TRANSPORT_TYPE to disable tracing
    - TRANSPORT_TYPE=http
    - STORAGE_TYPE=cassandra
  expose:
    # The http api is mounted at /api/v1
    - 9411
    # Admin interface is under the http path /admin
    # https://twitter.github.io/twitter-server/Features.html#http-admin-interface
    - 9901
  ports:
    - 9411:9411
    - 9901:9901
  links:
    - cassandra:storage

service_port是指跟踪目标服务的端口。
由于要通过rails s启动,所以我指定了3000。

使用rails s命令以调试模式启动rails应用。

rails s

在网页浏览器中访问 localhost:3000

将显示Rails的欢迎页面。

スクリーンショット 2016-03-09 10.58.50.png

试着查看zipkin的8080端口上的WebUI。

スクリーンショット 2016-03-09 11.01.24.png

选择已经注册的 [ hoge app ]。
选择此选项并按下 “查找轨迹” 按钮。

スクリーンショット 2016-03-09 11.02.20.png

似乎有登记记录。我选择了其中一项并试图展示。

スクリーンショット 2016-03-09 11.02.59.png

运行时间也显示了!

4. 感受

    • Rails以外のweb frameworkでも利用できるのか確認してみたい。play, akkaといったscala系はGitHubにサンプル等ありそうだった。Golangはどうだろ。go-kitを使ったサンプルがあるかもしれないから探してみる

 

    • http以外のプロトコルで提供している自前サービスに対してHandler実装できるのかな

 

    • 複数サービスが跨って連携した時のtraceってどうやるんだろうか

 

    • elixirのphoenixとかdynamoでも使えないのかしら

 

    cassandraの内部のデータ構造どうなってるんだろ

这给人留下了还有很多有趣话题的印象^^


今天就到这里吧。

bannerAds