diff --git a/docker/docker-compose.yml b/docker/sharding-jdbc/sharding/docker-compose.yml similarity index 83% rename from docker/docker-compose.yml rename to docker/sharding-jdbc/sharding/docker-compose.yml index 614b7e368b239c5c67bafca7f02c6dd5efa46903..431257d3a416edd30f0aaf8359fcc8bafa008e9d 100644 --- a/docker/docker-compose.yml +++ b/docker/sharding-jdbc/sharding/docker-compose.yml @@ -3,19 +3,19 @@ services: mysql: ## mysql version, you could get more tags at here : https://hub.docker.com/_/mysql?tab=tags image: "mysql:5.7" - ## host port is 3306, you could change to 33060 or any other port doesn't conflict MySQL on your OS + ## default port is 3306, you could change to 33060 or any other port doesn't conflict MySQL on your OS ports: - - "3306:3306" + - "3306:3306" container_name: sharding-sphere-mysql ## launch mysql without password ## you could access the mysql in container by following command : ## docker exec -it sharding-sphere-mysql mysql -uroot environment: - - MYSQL_ALLOW_EMPTY_PASSWORD=yes + - MYSQL_ALLOW_EMPTY_PASSWORD=yes ## if you insist to use password in mysql, remove MYSQL_ALLOW_EMPTY_PASSWORD=yes and uncomment following args # - MYSQL_ROOT_PASSWORD=root volumes: - - ../src/resources/manual_schema.sql:/docker-entrypoint-initdb.d/manual_schema.sql + - ../../../src/resources/manual_schema.sql:/docker-entrypoint-initdb.d/manual_schema.sql zookeeper: ## get more versions of zookeeper here : https://hub.docker.com/_/zookeeper?tab=tags diff --git a/docker/sharding-proxy/sharding/conf/config-sharding.yaml b/docker/sharding-proxy/sharding/conf/config-sharding.yaml new file mode 100644 index 0000000000000000000000000000000000000000..7aeea7d99872e6e32938e4da10dea1be862a95aa --- /dev/null +++ b/docker/sharding-proxy/sharding/conf/config-sharding.yaml @@ -0,0 +1,55 @@ +###################################################################################################### +# +# Here you can configure the rules for the proxy. +# This example is configuration of sharding rule. +# +# If you want to use sharding, please refer to this file; +# if you want to use master-slave, please refer to the config-master_slave.yaml. +# +###################################################################################################### + +schemaName: sharding_db + +dataSources: + ds_0: + url: jdbc:mysql://sharding-sphere-mysql:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 50 + ds_1: + url: jdbc:mysql://sharding-sphere-mysql:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 50 + +shardingRule: + tables: + t_order: + actualDataNodes: ds_${0..1}.t_order_${0..1} + tableStrategy: + inline: + shardingColumn: order_id + algorithmExpression: t_order_${order_id % 2} + keyGeneratorColumnName: order_id + t_order_item: + actualDataNodes: ds_${0..1}.t_order_item_${0..1} + tableStrategy: + inline: + shardingColumn: order_id + algorithmExpression: t_order_item_${order_id % 2} + keyGeneratorColumnName: order_item_id + bindingTables: + - t_order,t_order_item + defaultDatabaseStrategy: + inline: + shardingColumn: user_id + algorithmExpression: ds_${user_id % 2} + defaultTableStrategy: + none: + defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator diff --git a/docker/sharding-proxy/sharding/conf/server.yaml b/docker/sharding-proxy/sharding/conf/server.yaml new file mode 100644 index 0000000000000000000000000000000000000000..bf5f46c1c80c7edd17ec3831e6734b62e1d24d85 --- /dev/null +++ b/docker/sharding-proxy/sharding/conf/server.yaml @@ -0,0 +1,28 @@ +###################################################################################################### +# +# If you want to configure orchestration, authorization and proxy properties, please refer to this file. +# +###################################################################################################### + +#orchestration: +# name: orchestration_ds +# overwrite: true +# registry: +# serverLists: localhost:2181 +# namespace: orchestration +# +authentication: + username: root + password: root + +props: + max.connections.size.per.query: 1 + acceptor.size: 16 # The default value is available processors count * 2. + executor.size: 16 # Infinite by default. + proxy.frontend.flush.threshold: 128 # The default value is 128. + # LOCAL: Proxy will run with LOCAL transaction. + # XA: Proxy will run with XA transaction. + # BASE: Proxy will run with B.A.S.E transaction. + proxy.transaction.type: LOCAL + proxy.opentracing.enabled: false + sql.show: false diff --git a/docker/sharding-proxy/sharding/docker-compose.yml b/docker/sharding-proxy/sharding/docker-compose.yml new file mode 100644 index 0000000000000000000000000000000000000000..a1e2a9f8abfc8fa79872598fb8dfd0dbbc9a36d3 --- /dev/null +++ b/docker/sharding-proxy/sharding/docker-compose.yml @@ -0,0 +1,41 @@ +version: '3' +services: + mysql: + ## mysql version, you could get more tags at here : https://hub.docker.com/_/mysql?tab=tags + image: "mysql:5.7" + ## default port is 3306, you could change to 33060 or any other port doesn't conflict MySQL on your OS + ports: + - "33060:3306" + container_name: sharding-sphere-mysql + ## launch mysql without password + ## you could access the mysql in container by following command : + ## docker exec -it sharding-sphere-mysql mysql -uroot + environment: + - MYSQL_ALLOW_EMPTY_PASSWORD=yes + ## if you insist to use password in mysql, remove MYSQL_ALLOW_EMPTY_PASSWORD=yes and uncomment following args + # - MYSQL_ROOT_PASSWORD=root + volumes: + - ../../../src/resources/manual_schema.sql:/docker-entrypoint-initdb.d/manual_schema.sql + + proxy: + ## get more versions of proxy here : https://hub.docker.com/r/shardingsphere/sharding-proxy/tags + image: "shardingsphere/sharding-proxy:3.1.0" + container_name: sharding-sphere-proxy + depends_on: + - mysql + ## wait-for-it.sh will make proxy entrypoint wait until mysql container 3306 port open + entrypoint: > + /bin/sh -c "/opt/wait-for-it.sh sharding-sphere-mysql:3306 --timeout=20 --strict -- + && /opt/sharding-proxy/bin/start.sh 3308 + && tail -f /opt/sharding-proxy/logs/stdout.log" + ports: + - "13308:3308" + links: + - "mysql:mysql" + volumes: + - ./conf/:/opt/sharding-proxy/conf + - ../../tools/wait-for-it.sh:/opt/wait-for-it.sh + environment: + - JVM_OPTS="-Djava.awt.headless=true" + + diff --git a/docker/tools/wait-for-it.sh b/docker/tools/wait-for-it.sh new file mode 100755 index 0000000000000000000000000000000000000000..071c2bee3e1c42f373d4fd19415f11fefbcef567 --- /dev/null +++ b/docker/tools/wait-for-it.sh @@ -0,0 +1,178 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + WAITFORIT_BUSYTIMEFLAG="-t" + +else + WAITFORIT_ISBUSY=0 + WAITFORIT_BUSYTIMEFLAG="" +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi