Docker Compose 搭建基于mysql存储的zipkin环境

服务器

浏览数:98

2020-5-31

目标:通过Docker的方式部署MySql和zipkin,实现zipkin调用mysql

解决方案:让需要链接的容器同属一个外部网络
第一步:定义容器mysql的docker-compose.yml文件,内容如下:

version: '2'
services:
  db:
    image: hub.c.163.com/library/mysql:5.7
    container_name: mysql
    volumes:
      - /Users/xiewanzhi/mysql_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: testDB
    ports:
      # Port used for the Zipkin UI and HTTP Api
      - 3306:3306
    networks: 
      - default 
      - app_net
networks: 
  app_net: 
    external: true

第二步:定义容器zipkin的docker-compose.yml文件,内容如下:

version: '2'

services:
  # The zipkin process services the UI, and also exposes a POST endpoint that
  # instrumentation can send trace data to. Scribe is disabled by default.
  zipkin:
    image: openzipkin/zipkin:2.4.4
    container_name: zipkin
    environment:
      - STORAGE_TYPE=mysql
      # Point the zipkin at the storage backend
      - MYSQL_DB=zipkin
      - MYSQL_USER=root
      - MYSQL_PASS=123456
      - MYSQL_HOST=mysql
      - MYSQL_TCP_PORT=3306
      # Uncomment to enable scribe
      # - SCRIBE_ENABLED=true
      # Uncomment to enable self-tracing
      # - SELF_TRACING_ENABLED=true
      # Uncomment to enable debug logging
      # - JAVA_OPTS=-Dlogging.level.zipkin=DEBUG -Dlogging.level.zipkin2=DEBUG
    ports:
      # Port used for the Zipkin UI and HTTP Api
      - 9411:9411
      # Uncomment if you set SCRIBE_ENABLED=true
      # - 9410:9410
    networks: 
      - default 
      - app_net
networks: 
  app_net: 
    external: true 

这里两个容器的定义里都使用了同一个外部网络 app_net ,因此,我们需要在启动这两个容器之前通过以下命令再创建外部网络:

docker network create app_net 

之后,通过 docker-compose up -d 命令启动这两个容器,然后执行 docker exec -it zipkin ping mysql,你将会看到如下的输出:

MacBook-Pro:docker-zipkin mymac$ docker exec -it zipkin ping mysql
PING mysql (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: seq=0 ttl=64 time=0.100 ms
64 bytes from 172.20.0.2: seq=1 ttl=64 time=0.149 ms
64 bytes from 172.20.0.2: seq=2 ttl=64 time=0.145 ms
64 bytes from 172.20.0.2: seq=3 ttl=64 time=0.143 ms
64 bytes from 172.20.0.2: seq=4 ttl=64 time=0.652 ms
64 bytes from 172.20.0.2: seq=5 ttl=64 time=0.128 ms
64 bytes from 172.20.0.2: seq=6 ttl=64 time=0.210 ms

证明这两个容器是成功链接了。

此时http://127.0.0.1:9411/zipkin/可以提供追踪服务了

作者:xflycloud