docker-compose 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
version: '3'
services:
mysql7:
image: mysql:5.7
container_name: mysql7
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${
MYSQL_ROOT_PASSWORD}
TZ: Asia/Shanghai
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M;
ports:
- ${
port}:3306
volumes:
# 映射SQL文件
- ./mysql/init:/docker-entrypoint-initdb.d
- ./mysql/sql:/sql
- ./mysql/data:/var/lib/mysql
nacos:
image: nacos/nacos-server:v2.0.4
container_name: nacos
environment:
# 支持主机名可以使用hostname,否则使用ip,默认ip
- PREFER_HOST_MODE=ip
- TZ=Asia/Shanghai
# 单机模式
- MODE=standalone
# 数据源平台 支持mysql或不保存empty
- SPRING_DATASOURCE_PLATFORM=mysql
# mysql配置,!!!attention必须是mysql所在主机IP
- MYSQL_SERVICE_HOST=${
MYSQL_HOST}
- MYSQL_SERVICE_PORT=${
MYSQL_PORT}
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=${
MYSQL_SERVICE_PASSWORD}
- MYSQL_SERVICE_DB_NAME=${
MYSQL_SERVICE_DB_NAME}
env_file:
- ./.env
volumes:
- ./nacos/logs:/home/nacos/logs
ports:
# web port
- ${
web port}:8848
# client grpc to server,服务请求
- ${
client port}:9848
# server grpc to server,服务同步
- ${
server port}:9849
restart: always
depends_on:
- mysql7
seata-server:
restart: always
image: seataio/seata-server:1.4.0
environment:
- SEATA_IP=${
SEATA_IP}
- SEATA_PORT=${
SEATA_PORT}
- SEATA_CONFIG_NAME=file:/root/seata-config/registry
volumes:
- ./seata/conf:/root/seata-config
- ./seata/logs:/root/logs
ports:
- "${SEATA_PORT}:${SEATA_PORT}"
container_name: "seata-server"
env_file:
- ./.env
depends_on:
- mysql7
- nacos

前提配置

目录结构
在这里插入图片描述

  • 创建seata 文件夹

    1
    mkdir seata
  • 编写config.txt 内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    service.vgroupMapping.${
    spring.application.name}-group=default
    store.mode=db
    store.db.datasource=druid
    store.db.dbType=mysql
    store.db.driverClassName=com.mysql.jdbc.Driver
    store.db.url=jdbc:mysql://${
    host}:${
    port}/ry-seata?useUnicode=true
    store.db.user=root
    store.db.password=${
    pwd}
    store.db.minConn=5
    store.db.maxConn=30
    store.db.globalTable=global_table
    store.db.branchTable=branch_table
    store.db.queryLimit=100
    store.db.lockTable=lock_table
    store.db.maxWait=5000
  • 编写registry.conf 内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    registry {

    type = "nacos"
    nacos {

    application = "seata-server"
    serverAddr = "${
    nacosHost:port}"
    group = "SEATA_GROUP"
    namespace = ""
    cluster = "default"
    username = "${
    nacosLoginName}"
    password = "${
    pwd}"
    }
    }

    config {

    type = "nacos"
    nacos {

    serverAddr = "${
    nacosHost:port}"
    namespace = ""
    group = "SEATA_GROUP"
    username = "${
    nacosLoginName}"
    password = "${
    pwd}"
    }
    }
  • 编写nacos-config.sh 内容如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    #!/usr/bin/env bash
    # Copyright 1999-2019 Seata.io Group.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at、
    #
    # http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.

    while getopts ":h:p:g:t:u:w:" opt
    do
    case $opt in
    h)
    host=$OPTARG
    ;;
    p)
    port=$OPTARG
    ;;
    g)
    group=$OPTARG
    ;;
    t)
    tenant=$OPTARG
    ;;
    u)
    username=$OPTARG
    ;;
    w)
    password=$OPTARG
    ;;
    ?)
    echo " USAGE OPTION: $0 [-h host] [-p port] [-g group] [-t tenant] [-u username] [-w password] "
    exit 1
    ;;
    esac
    done

    urlencode() {

    for ((i=0; i < ${
    #1}; i++))
    do
    char="${
    1:$i:1}"
    case $char in
    [a-zA-Z0-9.~_-]) printf $char ;;
    *) printf '%%%02X' "'$char" ;;
    esac
    done
    }

    if [[ -z ${
    host} ]]; then
    host=localhost
    fi
    if [[ -z ${
    port} ]]; then
    port=8848
    fi
    if [[ -z ${
    group} ]]; then
    group="SEATA_GROUP"
    fi
    if [[ -z ${
    tenant} ]]; then
    tenant=""
    fi
    if [[ -z ${
    username} ]]; then
    username=""
    fi
    if [[ -z ${
    password} ]]; then
    password=""
    fi

    nacosAddr=$host:$port
    contentType="content-type:application/json;charset=UTF-8"

    echo "set nacosAddr=$nacosAddr"
    echo "set group=$group"

    failCount=0
    tempLog=$(mktemp -u)
    function addConfig() {

    curl -X POST -H "${
    contentType}" "http://$nacosAddr/nacos/v1/cs/configs?dataId=$(urlencode $1)&group=$group&content=$(urlencode $2)&tenant=$tenant&username=$username&password=$password" >"${
    tempLog}" 2>/dev/null
    if [[ -z $(cat "${
    tempLog}") ]]; then
    echo " Please check the cluster status. "
    exit 1
    fi
    if [[ $(cat "${
    tempLog}") =~ "true" ]]; then
    echo "Set $1=$2 successfully "
    else
    echo "Set $1=$2 failure "
    (( failCount++ ))
    fi
    }

    count=0
    for line in $(cat $(dirname "$PWD")/config.txt | sed s/[[:space:]]//g); do
    (( count++ ))
    key=${
    line%%=*}
    value=${
    line#*=}
    addConfig "${
    key}" "${
    value}"
    done

    echo "========================================================================="
    echo " Complete initialization parameters, total-count:$count , failure-count:$failCount "
    echo "========================================================================="

    if [[ ${
    failCount} -eq 0 ]]; then
    echo " Init nacos config finished, please start seata-server. "
    else
    echo " init nacos config fail. "
    fi

运行

1
2
3
docker-compose -f fileName.yaml up -d

bash nacos-config.sh -h ${nacosHost} -p ${port} -g SEATA_GROUP -u ${username} -w ${pwd}

作者声明

1
如有问题,欢迎指正!