From f7f228eacb64408d1cded3ff41ae1ce933c840c8 Mon Sep 17 00:00:00 2001 From: qloog Date: Sat, 30 Oct 2021 09:43:58 +0800 Subject: [PATCH] chore: fix docker start error --- config/config.docker.yaml | 26 ++++-- deploy/docker/mysql/init.sql | 5 ++ docker-compose.yml | 92 +------------------ internal/dao/dao.go | 4 +- test/docker-compose.yaml | 165 +++++++++++++++++++++++++++++++++-- 5 files changed, 187 insertions(+), 105 deletions(-) diff --git a/config/config.docker.yaml b/config/config.docker.yaml index 5e6cb50..66c3f26 100644 --- a/config/config.docker.yaml +++ b/config/config.docker.yaml @@ -13,12 +13,12 @@ app: Http: Addr: :8080 - ReadTimeout: 5 - WriteTimeout: 5 + ReadTimeout: 3s + WriteTimeout: 3s Grpc: Addr: :9090 - ReadTimeout: 5 - WriteTimeout: 5 + ReadTimeout: 3s + WriteTimeout: 3s logger: Development: true @@ -36,7 +36,7 @@ logger: LogRotateSize: 1 LogBackupCount: 7 -mysql: +orm: Name: eagle # 数据库名称 Addr: db:3306 # 如果是 docker,可以替换为 对应的服务名称,eg: db:3306 UserName: root @@ -46,6 +46,22 @@ mysql: MaxOpenConn: 60 # 最大打开的连接数, 需要小于数据库配置中的max_connections数 ConnMaxLifeTime: 60m # 单个连接最大存活时间,建议设置比数据库超时时长(wait_timeout)稍小一些 +mysql: + Dsn: "root:123456@tcp(localhost:3306)/eagle?timeout=2s&readTimeout=5s&writeTimeout=5s&parseTime=true&loc=Local&charset=utf8,utf8mb4" + ShowLog: true # 是否打印SQL日志 + MaxIdleConn: 10 # 最大闲置的连接数,0意味着使用默认的大小2, 小于0表示不使用连接池 + MaxOpenConn: 60 # 最大打开的连接数, 需要小于数据库配置中的max_connections数 + ConnMaxLifeTime: 4000 # 单个连接最大存活时间,建议设置比数据库超时时长(wait_timeout)稍小一些 + QueryTimeout: 200 + ExecTimeout: 200 + TranTimeout: 200 + Braker: # 熔断器配置 + window: 3s + sleep: 100ms + bucket: 100 + ratio: 0.5 + request: 100 + redis: Addr: redis:6379 Password: "" diff --git a/deploy/docker/mysql/init.sql b/deploy/docker/mysql/init.sql index b369218..db71a8c 100644 --- a/deploy/docker/mysql/init.sql +++ b/deploy/docker/mysql/init.sql @@ -1,6 +1,11 @@ use mysql; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root'; GRANT ALL ON *.* to root@'%' IDENTIFIED BY 'root'; + +# create test_user +CREATE USER 'test_user'@'%' IDENTIFIED BY '123456'; +GRANT ALL ON *.* to test_user@'%' IDENTIFIED BY '123456'; + FLUSH PRIVILEGES; -- 创建数据库 diff --git a/docker-compose.yml b/docker-compose.yml index d0a06d6..d41efcf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,8 +34,8 @@ services: environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: eagle - MYSQL_USER: root - MYSQL_PASSWORD: root + MYSQL_USER: test_user + MYSQL_PASSWORD: 123456 TZ: Asia/Shanghai # 解决外部无法访问 for mysql8 command: [ @@ -61,16 +61,6 @@ services: networks: - eagle - # web 数据库管理工具,tips: host使用 db:3306 - adminer: - container_name: adminer_container - image: adminer - restart: always - depends_on: - - db - ports: - - 8036:8036 - redis: container_name: redis_container image: redis:6.0.9-alpine @@ -81,84 +71,6 @@ services: volumes: - redis_data:/var/lib/redis - nginx: - container_name: nginx_container - image: nginx:1.17.10-alpine - ports: - - 80:80 - depends_on: - - app - volumes: - - ./config/nginx_api.conf:/etc/nginx/conf.d/eagle.conf - command: nginx -g 'daemon off'; - - prometheus: - container_name: prometheus_container - image: prom/prometheus - restart: always - volumes: - - ./deploy/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:Z - command: - - '--config.file=/etc/prometheus/prometheus.yml' - - '--storage.tsdb.path=/prometheus' - - '--storage.tsdb.retention=20d' - - '--web.console.libraries=/usr/share/prometheus/console_libraries' - - '--web.console.templates=/usr/share/prometheus/consoles' - ports: - - '9090:9090' - networks: - - eagle - - node_exporter: - container_name: node_exporter_container - restart: always - image: prom/node-exporter - ports: - - '9101:9100' - networks: - - eagle - - grafana: - container_name: grafana_container - restart: always - image: grafana/grafana - ports: - - '3000:3000' - networks: - - eagle - jaeger: - container_name: jaeger_container - image: jaegertracing/all-in-one:1.21 - environment: - - COLLECTOR_ZIPKIN_HTTP_PORT=9411 - ports: - - 5775:5775/udp - - 6831:6831/udp - - 6832:6832/udp - - 5778:5778 - - 16686:16686 - - 14268:14268 - - 14250:14250 - - 9411:9411 - networks: - - eagle - - mongodb: - image: mongo:latest - container_name: mongodb_container - restart: always - environment: - MONGO_INITDB_ROOT_USERNAME: admin - MONGO_INITDB_ROOT_PASSWORD: admin - MONGODB_DATABASE: eagle - ports: - - 27017:27017 - volumes: - - mongodb_data:/data/db - networks: - - eagle - - networks: eagle: driver: "bridge" diff --git a/internal/dao/dao.go b/internal/dao/dao.go index fd4b8f3..5b3a1ba 100644 --- a/internal/dao/dao.go +++ b/internal/dao/dao.go @@ -28,8 +28,8 @@ type Dao struct { // New new a Dao and return func New(cfg *conf.Config, db *gorm.DB) *Dao { return &Dao{ - orm: db, - db: sql.NewMySQL(cfg.MySQL), + orm: db, + //db: sql.NewMySQL(cfg.MySQL), tracer: otel.Tracer("dao"), userCache: cache.NewUserCache(), } diff --git a/test/docker-compose.yaml b/test/docker-compose.yaml index 50da45b..f467a13 100644 --- a/test/docker-compose.yaml +++ b/test/docker-compose.yaml @@ -1,24 +1,173 @@ +# yaml 配置 +# 官方文档:https://docs.docker.com/compose/compose-file/ version: "3.7" services: - db: - image: mysql:5.6 + app: + container_name: app_container + build: . + restart: on-failure + depends_on: + - db + - redis + links: + - db + - redis ports: - - 3306:3306 + - "8080:8080" + networks: + - eagle + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8080/health"] # 用于健康检查的指令 + interval: 1m30s # 间隔时间 + timeout: 10s # 超时时间 + retries: 3 # 重试次数 + start_period: 40s # 启动多久后开始检查 + + db: + container_name: mysql_container + image: mysql:5.7.33 + ports: + - "3306:3306" + expose: + - "3306" environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: eagle MYSQL_USER: root MYSQL_PASSWORD: root TZ: Asia/Shanghai - volumes: - - .:/docker-entrypoint-initdb.d + # 解决外部无法访问 for mysql8 command: [ '--character-set-server=utf8', - '--collation-server=utf8_unicode_ci' + '--collation-server=utf8_unicode_ci', + '--default-authentication-plugin=mysql_native_password' ] + healthcheck: + test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ] # 用于健康检查的指令 + timeout: 20s # 超时时间 + retries: 10 # 重试次数 + start_period: 40s # 启动多久后开始检查 + stdin_open: true + tty: true + # 修复问题 mbind: Operation not permitted + security_opt: + - seccomp:unconfined + volumes: + - mysql_data:/var/lib/mysql + - ./deploy/docker/mysql/my.cnf:/etc/mysql/my.cnf + - ./deploy/docker/mysql/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf + - ./deploy/docker/mysql/:/docker-entrypoint-initdb.d/ + networks: + - eagle + + # web 数据库管理工具,tips: host使用 db:3306 + adminer: + container_name: adminer_container + image: adminer + restart: always + depends_on: + - db + ports: + - 8036:8036 + networks: + - eagle redis: - image: redis + container_name: redis_container + image: redis:6.0.9-alpine ports: - - 6379:6379 \ No newline at end of file + - "6379:6379" + networks: + - eagle + volumes: + - redis_data:/var/lib/redis + + nginx: + container_name: nginx_container + image: nginx:1.17.10-alpine + ports: + - 80:80 + depends_on: + - app + volumes: + - ./config/nginx_api.conf:/etc/nginx/conf.d/eagle.conf + command: nginx -g 'daemon off'; + networks: + - eagle + + prometheus: + container_name: prometheus_container + image: prom/prometheus + restart: always + volumes: + - ./deploy/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:Z + command: + - '--config.file=/etc/prometheus/prometheus.yml' + - '--storage.tsdb.path=/prometheus' + - '--storage.tsdb.retention=20d' + - '--web.console.libraries=/usr/share/prometheus/console_libraries' + - '--web.console.templates=/usr/share/prometheus/consoles' + ports: + - '9090:9090' + networks: + - eagle + + node_exporter: + container_name: node_exporter_container + restart: always + image: prom/node-exporter + ports: + - '9101:9100' + networks: + - eagle + + grafana: + container_name: grafana_container + restart: always + image: grafana/grafana + ports: + - '3000:3000' + networks: + - eagle + jaeger: + container_name: jaeger_container + image: jaegertracing/all-in-one:1.21 + environment: + - COLLECTOR_ZIPKIN_HTTP_PORT=9411 + ports: + - 5775:5775/udp + - 6831:6831/udp + - 6832:6832/udp + - 5778:5778 + - 16686:16686 + - 14268:14268 + - 14250:14250 + - 9411:9411 + networks: + - eagle + + mongodb: + image: mongo:latest + container_name: mongodb_container + restart: always + environment: + MONGO_INITDB_ROOT_USERNAME: admin + MONGO_INITDB_ROOT_PASSWORD: admin + MONGODB_DATABASE: eagle + ports: + - 27017:27017 + volumes: + - mongodb_data:/data/db + networks: + - eagle + + +networks: + eagle: + driver: "bridge" + +volumes: + mysql_data: + redis_data: + mongodb_data: