# 基础微服务

## 文件方式安装环境

使用docker-compose有的时候会因为版本不同，但是配置文件主要内容就是这些。需要注意版本问题

### mysql配置问题
| **特性** | `**my.cnf**` | `**conf.d**`<br/>** ****目录** |
| --- | --- | --- |
| **<font style="color:rgb(64, 64, 64);">文件类型</font>** | <font style="color:rgb(64, 64, 64);">单个文件</font> | <font style="color:rgb(64, 64, 64);">目录，包含多个</font><font style="color:rgb(64, 64, 64);"> </font>`<font style="color:rgb(64, 64, 64);">.cnf</font>`<br/><font style="color:rgb(64, 64, 64);"> </font><font style="color:rgb(64, 64, 64);">文件</font> |
| **<font style="color:rgb(64, 64, 64);">配置方式</font>** | <font style="color:rgb(64, 64, 64);">集中式配置</font> | <font style="color:rgb(64, 64, 64);">分布式配置</font> |
| **<font style="color:rgb(64, 64, 64);">优先级</font>** | <font style="color:rgb(64, 64, 64);">高（覆盖</font><font style="color:rgb(64, 64, 64);"> </font>`<font style="color:rgb(64, 64, 64);">conf.d</font>`<br/><font style="color:rgb(64, 64, 64);"> </font><font style="color:rgb(64, 64, 64);">中的配置）</font> | <font style="color:rgb(64, 64, 64);">低（被</font><font style="color:rgb(64, 64, 64);"> </font>`<font style="color:rgb(64, 64, 64);">my.cnf</font>`<br/><font style="color:rgb(64, 64, 64);"> </font><font style="color:rgb(64, 64, 64);">覆盖）</font> |
| **<font style="color:rgb(64, 64, 64);">适用场景</font>** | <font style="color:rgb(64, 64, 64);">全局配置，核心配置</font> | <font style="color:rgb(64, 64, 64);">模块化配置，便于扩展和维护</font> |


### MongoDB配置
```bash
sudo mkdir -p ~/docker/docker_data/mongo/conf
sudo mkdir -p ~/docker/docker_data/mongo/logs
sudo chmod 777 ~/docker/docker_data/mongo/logs
sudo chmod 777 ~/docker/docker_data/mongo/conf

cd ~/docker/docker_data/mongo/logs
sudo touch mongod.log
sudo chmod 777 mongod.log

cd ~/docker/docker_data/mongo/conf
sudo vim mongod.conf

cd ~
```

#### 配置文件
```bash
# 数据库文件存储位置
dbpath = /data/db
# log文件存储位置
logpath = /data/log/mongod.log
# 使用追加的方式写日志
logappend = true
# 是否以守护进程方式运行
# fork = true
# 全部ip可以访问
bind_ip = 0.0.0.0
# 端口号
port = 27017
# 是否启用认证
auth = true
# 设置oplog的大小(MB)
oplogSize=2048
```

#### 设置账户密码
```shell
#进入容器
docker exec -it mongodb /bin/bash

#进入mongodb shell
mongosh --port 27017

#切换到admin库
use admin

#创建账号/密码
db.createUser({ user: 'admin', pwd: '02120212', roles: [ { role: "root", db: "admin" } ] });
# db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
```

## docker-compose.yml
```yaml
name: cloud-dependence
services:
  mysql:
    container_name: mysql_master
    image: mysql:8.0.33
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - TZ=Asia/Shanghai
    volumes:
      # - ~/docker/docker_data/mysql/mysql_master/etc/my.cnf:/etc/my.cnf # 如果需要创建配置文件
      - ~/docker/docker_data/mysql/mysql_master/etc/mysql:/etc/mysql/conf.d
      - ~/docker/docker_data/mysql/mysql_master/data:/var/lib/mysql
      - ~/docker/docker_data/mysql/mysql_master/backup:/backup
    command:
      - "--log-bin=mysql-bin"
      - "--server-id=1"
      - "--collation-server=utf8mb4_unicode_ci"
      - "--character-set-server=utf8mb4"
      - "--lower-case-table-names=1"
    restart: always
    privileged: true
    networks:
      - cloud  
 
  redis:
    container_name: redis_master
    image: redis:7.0.10
    ports:
      - "6379:6379"
    volumes:
      # - ~/docker/docker_data/redis_master/redis.conf:/etc/redis/redis.conf # 需要创建配置文件
      - ~/docker/docker_data/redis_master:/etc/redis # 之后要配置文件可以直接在这里创建 redis.conf
      - ~/docker/docker_data/redis_master/data:/data
    command:
      - "--appendonly yes"
      - "--daemonize no"
      - "--requirepass 123456"
      - "--tcp-keepalive 300"
    restart: always
    networks:
      - cloud  
 
  minio:
    image: minio/minio
    container_name: minio_master
    ports:
      - "9000:9000"
      - "9090:9090"
    volumes:
      - ~/docker/docker_data/minio/data:/data
    environment:
      - MINIO_ROOT_USER=bunny
      - MINIO_ROOT_PASSWORD=02120212
    command: "server /data --console-address :9090"
    restart: always
    networks:
      - cloud  

  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: always
    privileged: true
    ports:
      - "27017:27017"
    volumes:
      - ~/docker/docker_data/mongo/data:/data/db
      - ~/docker/docker_data/mongo/conf:/data/configdb
      - ~/docker/docker_data/mongo/logs:/data/log
    command: "mongod --config /data/configdb/mongod.conf"
    networks:
      - cloud  
 
  nacos:
    image: nacos/nacos-server:v2.4.3
    container_name: nacos
    ports:
      - "8848:8848"
      - "9848:9848"
    environment:
      - MODE=standalone
    restart: always
    networks:
      - cloud  
 
  sentinel:
    image: bladex/sentinel-dashboard:1.8.8
    container_name: sentinel
    ports:
      - "8858:8858"
    privileged: true
    restart: always
    networks:
      - cloud  

  seata-server:
    image: apache/seata-server:2.3.0.jdk21
    container_name: seata-server
    ports:
      - "8091:8091"
    restart: always
    networks:
      - cloud  

networks: # 定义网络
  cloud: # 定义名为 auth 的网络
    name: cloud  # 网络名称为 auth
    driver: bridge  # 使用 bridge 驱动（默认）
```

## 拉取镜像模板
```yaml
name: cloud-dependence
services:
  mysql:
    container_name: mysql_master
    image: mysql:8.0.33
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - TZ=Asia/Shanghai
    volumes:
      # - ~/docker/docker_data/mysql/mysql_master/etc/my.cnf:/etc/my.cnf # 如果需要创建配置文件
      - ~/docker/docker_data/mysql/mysql_master/etc/mysql:/etc/mysql/conf.d
      - ~/docker/docker_data/mysql/mysql_master/data:/var/lib/mysql
      - ~/docker/docker_data/mysql/mysql_master/backup:/backup
    command:
      - "--log-bin=mysql-bin"
      - "--server-id=1"
      - "--collation-server=utf8mb4_unicode_ci"
      - "--character-set-server=utf8mb4"
      - "--lower-case-table-names=1"
    restart: always
    privileged: true
 
  redis:
    container_name: redis_master
    image: redis:7.0.10
    ports:
      - "6379:6379"
    volumes:
      # - ~/docker/docker_data/redis_master/redis.conf:/etc/redis/redis.conf # 需要创建配置文件
      - ~/docker/docker_data/redis_master:/etc/redis # 之后要配置文件可以直接在这里创建 redis.conf
      - ~/docker/docker_data/redis_master/data:/data
    command:
      - "--appendonly yes"
      - "--daemonize no"
      - "--requirepass 123456"
      - "--tcp-keepalive 300"
    restart: always
 
  minio:
    image: minio/minio
    container_name: minio_master
    ports:
      - "9000:9000"
      - "9090:9090"
    volumes:
      - ~/docker/docker_data/minio/data:/data
    environment:
      - MINIO_ROOT_USER=bunny
      - MINIO_ROOT_PASSWORD=02120212
    command: "server /data --console-address :9090"
    restart: always

  mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: always
    privileged: true
    ports:
      - "27017:27017"
    volumes:
      - ~/docker/docker_data/mongo/data:/data/db
      - ~/docker/docker_data/mongo/conf:/data/configdb
      - ~/docker/docker_data/mongo/logs:/data/log
    command: "mongod --config /data/configdb/mongod.conf"
 
  nacos:
    image: nacos/nacos-server:v2.4.3
    container_name: nacos
    ports:
      - "8848:8848"
      - "9848:9848"
    environment:
      - MODE=standalone
    restart: always
 
  sentinel:
    image: bladex/sentinel-dashboard:1.8.8
    container_name: sentinel
    ports:
      - "8858:8858"
    privileged: true
    restart: always
 
  seata-server:
    image: apache/seata-server:2.3.0.jdk21
    container_name: seata-server
    ports:
      - "8091:8091"
    restart: always
 
  opendjdk:
    image: openjdk:24-ea-17-jdk-oraclelinux9
```

