用docker部署mysql、redis和nacos

開發Spring Cloud Alibaba微服務應用時,mysql、redis和nacos要先搭建好,本文使用docker搭建;html

nacos有多種部署方式,見官方文檔:nacos docker,這裏用的是單機模式mysql模式,因此會與mysql會有關聯;mysql

docker-compose.yaml定義文件以下:nginx

version: "3.6"
services:
  mysql:
    image: mysql:${MYSQL_VERSION}
    container_name: mysql
    ports:
      - "${MYSQL_HOST_PORT}:3306"
    volumes:
      - ${MYSQL_CONF_FILE}:/etc/mysql/conf.d/mysql.cnf:ro
      - ${MYSQL_DATA_DIR}:/var/lib/mysql/:rw
    restart: on-failure
    networks:
      - default
    environment:
      MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
      TZ: Asia/Shanghai

  redis:
    image: redis:${REDIS_VERSION}
    container_name: redis
    environment:
      TZ: Asia/Shanghai
    volumes:
      - ${REDIS_LOG_DIR}:/var/log/redis/:rw
      - ${REDIS_CONF_FILE}:/usr/local/etc/redis/redis.conf:ro
      - ${REDIS_DIR}:/var/lib/redis/6379/:rw
    command:
      - /usr/local/etc/redis/redis.conf
    restart: on-failure
    networks:
      - default
    ports:
      - "${REDIS_HOST_PORT}:6379"

  nacos:
    image: nacos/nacos-server:${NACOS_VERSION}
    container_name: nacos
    environment:
      - "PREFER_HOST_MODE=${PREFER_HOST_MODE}"
      - "MODE=${MODE}"
      - "SPRING_DATASOURCE_PLATFORM=${SPRING_DATASOURCE_PLATFORM}"
      - "MYSQL_SERVICE_HOST=${MYSQL_SERVICE_HOST}"
      - "MYSQL_SERVICE_DB_NAME=${MYSQL_SERVICE_DB_NAME}"
      - "MYSQL_SERVICE_PORT=${MYSQL_SERVICE_PORT}"
      - "MYSQL_SERVICE_USER=${MYSQL_SERVICE_USER}"
      - "MYSQL_SERVICE_PASSWORD=${MYSQL_SERVICE_PASSWORD}"
    volumes:
      - ${NACOS_LOG_DIR}:/home/nacos/logs:rw
      - ${NACOS_PROPERTIES}:/home/nacos/init.d/custom.properties
    depends_on:
      - mysql
    ports:
      - 8848:8848
    restart: on-failure
    
networks:
  default:

環境變量.env:git

################################################
###       environment config file            ###
################################################

#################### MySQL #####################
MYSQL_VERSION=5.7.24
MYSQL_HOST_PORT=3306
MYSQL_ROOT_PASSWORD=123456
MYSQL_DATA_DIR=./mysql
MYSQL_CONF_FILE=./conf/mysql.cnf


#################### Redis #####################
REDIS_VERSION=5.0.9
REDIS_HOST_PORT=6379
REDIS_CONF_FILE=./conf/redis.conf
REDIS_LOG_DIR=./log/redis
REDIS_DIR=./redis

#################### NACOS #####################
NACOS_VERSION=1.2.1
NACOS_LOG_DIR=./log/nacos
PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=mysql #此處要主要的是要寫mysql服務的host,不能寫127.0.0.1;直接寫mysql便可;
MYSQL_SERVICE_DB_NAME=nacos
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=123456
NACOS_PROPERTIES=./conf/custom.properties

文件樹結構以下:github

├── conf
│   ├── conf.d
│   ├── custom.properties
│   ├── mysql.cnf
│   ├── nginx.conf
│   └── redis.conf
├── docker-compose.yml
├── log
│   ├── nacos
│   └── redis
├── mysql
│   ├── auto.cnf
│   ├── ca-key.pem
│   ├── ca.pem
│   ├── client-cert.pem
│   ├── client-key.pem
│   ├── ib_buffer_pool
│   ├── ib_logfile0
│   ├── ib_logfile1
│   ├── ibdata1
│   ├── ibtmp1
│   ├── mysql
│   ├── mysql.error.log
│   ├── mysql.slow.log
│   ├── nacos
│   ├── performance_schema
│   ├── private_key.pem
│   ├── public_key.pem
│   ├── server-cert.pem
│   ├── server-key.pem
│   └── sys
├── redis
│   ├── dump.rdb
│   └── test.txt
└── www

值得注意的是,這裏nacos版本是1.2.1,同時初始化的sql腳本nacos-mysql,能夠從nacos-mysql腳本取得;redis

啓動服務:sql

docker-compose -f docker-compose.yml up -d
Starting redis ... done
Starting mysql ... done
Starting nacos ... done

查看服務和端口狀況:docker

lsof -nP|grep LISTEN
idea        411 bigticket  122u     IPv4 0xf5b8d5ea88576b3b          0t0                 TCP 127.0.0.1:6942 (LISTEN)
idea        411 bigticket  744u     IPv4 0xf5b8d5ea89a9225b          0t0                 TCP 127.0.0.1:63342 (LISTEN)
idea        411 bigticket 1049u     IPv4 0xf5b8d5ea8b092cfb          0t0                 TCP 127.0.0.1:49427 (LISTEN)
privoxy     656 bigticket    3u     IPv4 0xf5b8d5ea896a4503          0t0                 TCP 127.0.0.1:1087 (LISTEN)
ss-local    663 bigticket    8u     IPv4 0xf5b8d5ea86d77ecb          0t0                 TCP 127.0.0.1:1086 (LISTEN)
com.docke  1141 bigticket    7u     IPv4 0xf5b8d5ea808b6de3          0t0                 TCP 127.0.0.1:49948 (LISTEN)
com.docke  1145 bigticket   14u     IPv4 0xf5b8d5ea8976d25b          0t0                 TCP 127.0.0.1:6443 (LISTEN)
com.docke  1145 bigticket   21u     IPv6 0xf5b8d5ea88f29063          0t0                 TCP *:6379 (LISTEN)
com.docke  1145 bigticket   24u     IPv6 0xf5b8d5ea88f26ba3          0t0                 TCP *:3306 (LISTEN)
com.docke  1145 bigticket   25u     IPv6 0xf5b8d5ea88f25943          0t0                 TCP *:8848 (LISTEN)

mysql和redis用客戶端測試鏈接,順利連上;瀏覽器

瀏覽器輸入http://127.0.0.1:8848/nacos/,初始帳號密碼:nacos/nacoside

image.png

參考:
https://github.com/nacos-grou...

相關文章
相關標籤/搜索