瀏覽代碼

!94 通过shardingproxy实现分库分表
Merge pull request !94 from rice666/2.X

疯狂的狮子Li 3 年之前
父節點
當前提交
3269abbcad

+ 13 - 0
docker/docker-compose.yml

@@ -492,3 +492,16 @@ services:
       - /docker/grafana/grafana.ini:/etc/grafana/grafana.ini
       - /docker/grafana:/var/lib/grafana
     network_mode: "host"
+  shardingproxy:
+    image: apache/shardingsphere-proxy
+    container_name: shardingsphere-proxy
+    restart: always
+    command: server /data
+    ports:
+      - 13307:3307
+    volumes:
+      - /docker/shardingproxy/conf:/opt/shardingsphere-proxy/conf
+      - /docker/shardingproxy/ext-lib:/opt/shardingsphere-proxy/ext-lib
+    environment:
+      - JVM_OPTS="-Djava.awt.headless=true"
+    network_mode: "host"

+ 125 - 0
docker/shardingproxy/conf/config-database-discovery.yaml

@@ -0,0 +1,125 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+######################################################################################################
+#
+# Here you can configure the rules for the proxy.
+# This example is configuration of HA rule.
+#
+######################################################################################################
+#
+#schemaName: database_discovery_db
+#
+#dataSources:
+#  primary_ds:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_primary_ds
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 3000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  replica_ds_0:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_replica_ds_0
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 3000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  replica_ds_1:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_replica_ds_1
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 3000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !DB_DISCOVERY
+#  dataSources:
+#    pr_ds:
+#      dataSourceNames:
+#        - ds_0
+#        - ds_1
+#        - ds_2
+#      discoveryTypeName: mgr
+#  discoveryTypes:
+#    mgr:
+#      type: MGR
+#      props:
+#        groupName: 92504d5b-6dec-11e8-91ea-246e9612aaf1
+#        zkServerLists: 'localhost:2181'
+#        keepAliveCron: '0/5 * * * * ?'
+
+######################################################################################################
+#
+# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
+#
+######################################################################################################
+
+#schemaName: database_discovery_db
+#
+#dataSources:
+#  ds_0:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_primary_ds?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 3000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  ds_1:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_replica_ds_0?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 3000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  ds_2:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_replica_ds_1?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 3000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !DB_DISCOVERY
+#  dataSources:
+#    pr_ds:
+#      dataSourceNames:
+#        - ds_0
+#        - ds_1
+#        - ds_2
+#      discoveryTypeName: mgr
+#  discoveryTypes:
+#    mgr:
+#      type: MGR
+#      props:
+#        groupName: 92504d5b-6dec-11e8-91ea-246e9612aaf1
+#        zkServerLists: 'localhost:2181'
+#        keepAliveCron: '0/5 * * * * ?'

+ 113 - 0
docker/shardingproxy/conf/config-encrypt.yaml

@@ -0,0 +1,113 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+######################################################################################################
+# 
+# Here you can configure the rules for the proxy.
+# This example is configuration of encrypt rule.
+# 
+######################################################################################################
+#
+#schemaName: encrypt_db
+#
+#dataSources:
+#  ds_0:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_ds_0
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  ds_1:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_ds_1
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !ENCRYPT
+#  encryptors:
+#    aes_encryptor:
+#      type: AES
+#      props:
+#        aes-key-value: 123456abc
+#    md5_encryptor:
+#      type: MD5
+#  tables:
+#    t_encrypt:
+#      columns:
+#        user_id:
+#          plainColumn: user_plain
+#          cipherColumn: user_cipher
+#          encryptorName: aes_encryptor
+#        order_id:
+#          cipherColumn: order_encrypt
+#          encryptorName: md5_encryptor
+
+######################################################################################################
+#
+# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
+#
+######################################################################################################
+#
+#schemaName: encrypt_db
+#
+#dataSources:
+#  ds_0:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  ds_1:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !ENCRYPT
+#  encryptors:
+#    aes_encryptor:
+#      type: AES
+#      props:
+#        aes-key-value: 123456abc
+#    md5_encryptor:
+#      type: MD5
+#  tables:
+#    t_encrypt:
+#      columns:
+#        user_id:
+#          plainColumn: user_plain
+#          cipherColumn: user_cipher
+#          encryptorName: aes_encryptor
+#        order_id:
+#          cipherColumn: order_cipher
+#          encryptorName: md5_encryptor

+ 109 - 0
docker/shardingproxy/conf/config-readwrite-splitting.yaml

@@ -0,0 +1,109 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+######################################################################################################
+# 
+# Here you can configure the rules for the proxy.
+# This example is configuration of replica-query rule.
+# 
+######################################################################################################
+#
+#schemaName: readwrite_splitting_db
+#
+#dataSources:
+#  primary_ds:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_primary_ds
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  replica_ds_0:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_replica_ds_0
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  replica_ds_1:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_replica_ds_1
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !READWRITE_SPLITTING
+#  dataSources:
+#    pr_ds:
+#      writeDataSourceName: primary_ds
+#      readDataSourceNames:
+#        - replica_ds_0
+#        - replica_ds_1
+
+######################################################################################################
+#
+# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
+#
+######################################################################################################
+
+#schemaName: readwrite_splitting_db
+#
+#dataSources:
+#  write_ds:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_write_ds?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  read_ds_0:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_0?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  read_ds_1:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_1?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !READWRITE_SPLITTING
+#  dataSources:
+#    pr_ds:
+#      writeDataSourceName: write_ds
+#      readDataSourceNames:
+#        - read_ds_0
+#        - read_ds_1

+ 179 - 0
docker/shardingproxy/conf/config-shadow.yaml

@@ -0,0 +1,179 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+######################################################################################################
+# 
+# Here you can configure the rules for the proxy.
+# This example is configuration of shadow rule.
+# 
+######################################################################################################
+#
+#schemaName: shadow_db
+#
+#dataSources:
+#  ds:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_ds_0
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  shadow_ds:
+#    url: jdbc:postgresql://127.0.0.1:5432/demo_ds_1
+#    username: postgres
+#    password: postgres
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !SHADOW
+#  enable: true
+#  dataSources:
+#    shadowDataSource:
+#      sourceDataSourceName: ds
+#      shadowDataSourceName: shadow_ds
+#  tables:
+#    t_order:
+#      dataSourceNames:
+#        - shadowDataSource
+#      shadowAlgorithmNames:
+#        - user-id-insert-match-algorithm
+#        - user-id-select-match-algorithm
+#    t_order_item:
+#      dataSourceNames:
+#        - shadowDataSource
+#      shadowAlgorithmNames:
+#        - user-id-insert-match-algorithm
+#        - user-id-update-match-algorithm
+#        - user-id-select-match-algorithm
+#    t_address:
+#      dataSourceNames:
+#        - shadowDataSource
+#      shadowAlgorithmNames:
+#        - user-id-insert-match-algorithm
+#        - user-id-select-match-algorithm
+#        - simple-note-algorithm
+#  shadowAlgorithms:
+#    user-id-insert-match-algorithm:
+#      type: COLUMN_REGEX_MATCH
+#      props:
+#        operation: insert
+#        column: user_id
+#        regex: "[1]"
+#    user-id-update-match-algorithm:
+#      type: COLUMN_REGEX_MATCH
+#      props:
+#        operation: update
+#        column: user_id
+#        regex: "[1]"
+#    user-id-select-match-algorithm:
+#      type: COLUMN_REGEX_MATCH
+#      props:
+#        operation: select
+#        column: user_id
+#        regex: "[1]"
+#    simple-note-algorithm:
+#      type: SIMPLE_NOTE
+#      props:
+#        shadow: true
+#        foo: bar
+
+######################################################################################################
+#
+# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
+#
+######################################################################################################
+#
+#schemaName: shadow_db
+#
+#dataSources:
+#  ds:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#  shadow_ds:
+#    url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
+#    username: root
+#    password:
+#    connectionTimeoutMilliseconds: 30000
+#    idleTimeoutMilliseconds: 60000
+#    maxLifetimeMilliseconds: 1800000
+#    maxPoolSize: 50
+#    minPoolSize: 1
+#
+#rules:
+#- !SHADOW
+#  enable: true
+#  dataSources:
+#    shadowDataSource:
+#      sourceDataSourceName: ds
+#      shadowDataSourceName: shadow_ds
+#  tables:
+#    t_order:
+#      dataSourceNames:
+#        - shadowDataSource
+#      shadowAlgorithmNames:
+#        - user-id-insert-match-algorithm
+#        - user-id-select-match-algorithm
+#    t_order_item:
+#      dataSourceNames:
+#        - shadowDataSource
+#      shadowAlgorithmNames:
+#        - user-id-insert-match-algorithm
+#        - user-id-update-match-algorithm
+#        - user-id-select-match-algorithm
+#    t_address:
+#      dataSourceNames:
+#        - shadowDataSource
+#      shadowAlgorithmNames:
+#        - user-id-insert-match-algorithm
+#        - user-id-select-match-algorithm
+#        - simple-note-algorithm
+#  shadowAlgorithms:
+#    user-id-insert-match-algorithm:
+#      type: COLUMN_REGEX_MATCH
+#      props:
+#        operation: insert
+#        column: user_id
+#        regex: "[1]"
+#    user-id-update-match-algorithm:
+#      type: COLUMN_REGEX_MATCH
+#      props:
+#        operation: update
+#        column: user_id
+#        regex: "[1]"
+#    user-id-select-match-algorithm:
+#      type: COLUMN_REGEX_MATCH
+#      props:
+#        operation: select
+#        column: user_id
+#        regex: "[1]"
+#    simple-note-algorithm:
+#      type: SIMPLE_NOTE
+#      props:
+#        shadow: true
+#        foo: bar

+ 84 - 0
docker/shardingproxy/conf/config-sharding.yaml

@@ -0,0 +1,84 @@
+######################################################################################################
+#
+# 用于配置:数据分片规则
+#
+######################################################################################################
+
+schemaName: data-center_db
+
+dataSources:
+  ds_0:
+    url: jdbc:mysql://192.168.0.60:3306/data-center_0?serverTimezone=UTC&useSSL=false
+    username: root
+    password: root
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+  ds_1:
+    url: jdbc:mysql://192.168.0.60:3306/data-center_1?serverTimezone=UTC&useSSL=false
+    username: root
+    password: root
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+
+rules:
+  - !SHARDING
+    tables: # 数据分片规则配置
+      t_order: # 订单逻辑表名称
+        actualDataNodes: ds_${0..1}.t_order_${0..1}
+        databaseStrategy: # 配置分库策略
+          standard:
+            shardingColumn: user_id
+            shardingAlgorithmName: database_user_inline
+        tableStrategy: # 分表策略
+          standard:
+            shardingColumn: order_id
+            shardingAlgorithmName: t_order_inline
+        keyGenerateStrategy:
+          column: order_id
+          keyGeneratorName: snowflake
+      t_order_item: # 子订单逻辑表名称
+        actualDataNodes: ds_${0..1}.t_order_item_${0..1}
+        databaseStrategy: # 配置分库策略
+          standard:
+            shardingColumn: user_id
+            shardingAlgorithmName: database_user_inline
+        tableStrategy: # 分表策略
+          standard:
+            shardingColumn: order_id
+            shardingAlgorithmName: t_order_item_inline
+        keyGenerateStrategy:
+          column: order_item_id
+          keyGeneratorName: snowflake
+    bindingTables: # 绑定表规则列表
+      - t_order,t_order_item
+
+    # 分片算法配置
+    shardingAlgorithms:
+      database_user_inline:
+        type: INLINE
+        props:
+          algorithm-expression: ds_${user_id % 2}
+      t_order_inline: # 订单表分片算法名称
+        type: INLINE
+        props:
+          algorithm-expression: t_order_${order_id % 2}
+          allow-range-query-with-inline-sharding: true
+      t_order_item_inline: # 子订单表分片算法名称
+        type: INLINE
+        props:
+          algorithm-expression: t_order_item_${order_id % 2}
+          allow-range-query-with-inline-sharding: true
+
+    # 分布式序列算法配置
+    keyGenerators:
+      snowflake:
+        type: SNOWFLAKE
+        props:
+          worker-id: 1
+

+ 45 - 0
docker/shardingproxy/conf/logback.xml

@@ -0,0 +1,45 @@
+<?xml version="1.0"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements.  See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to You 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.
+  -->
+
+<configuration>
+    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+    <logger name="org.apache.shardingsphere" level="info" additivity="false">
+        <appender-ref ref="console" />
+    </logger>
+    
+    <logger name="com.zaxxer.hikari" level="error">
+        <appender-ref ref="console" />
+    </logger>
+    
+    <logger name="com.atomikos" level="error">
+        <appender-ref ref="console" />
+    </logger>
+    
+    <logger name="io.netty" level="error">
+        <appender-ref ref="console" />
+    </logger>
+    
+    <root>
+        <level value="info" />
+        <appender-ref ref="console" />
+    </root>
+</configuration> 

+ 61 - 0
docker/shardingproxy/conf/server.yaml

@@ -0,0 +1,61 @@
+######################################################################################################
+#
+# 用于配置:数据接入迁移&弹性伸缩、分布式治理模式、权限、代理属性.
+#
+######################################################################################################
+
+#scaling:
+#  blockQueueSize: 10000 # 数据传输通道队列大小
+#  workerThread: 40 # 工作线程池大小,允许同时运行的迁移任务线程数
+#  clusterAutoSwitchAlgorithm:
+#    type: IDLE
+#    props:
+#      incremental-task-idle-minute-threshold: 30
+#  dataConsistencyCheckAlgorithm:
+#    type: DEFAULT
+#
+#mode:
+#  type: Cluster
+#  repository:
+#    type: ZooKeeper
+#    props:
+#      namespace: governance_ds
+#      server-lists: localhost:2181
+#      retryIntervalMilliseconds: 500
+#      timeToLiveSeconds: 60
+#      maxRetries: 3
+#      operationTimeoutMilliseconds: 500
+#  overwrite: false
+#
+rules:
+  - !AUTHORITY
+    users:
+      - root@%:root
+      - sharding@:sharding
+    provider:
+      type: ALL_PRIVILEGES_PERMITTED
+  - !TRANSACTION
+    defaultType: XA
+    providerType: Atomikos
+
+props:
+  max-connections-size-per-query: 1
+  kernel-executor-size: 16  # Infinite by default.
+  proxy-frontend-flush-threshold: 128  # The default value is 128.
+  #  proxy-opentracing-enabled: false
+  #  proxy-hint-enabled: false
+  sql-show: true
+  #  check-table-metadata-enabled: false
+  #  show-process-list-enabled: false
+  #    # Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
+  #    # The default value is -1, which means set the minimum value for different JDBC drivers.
+  #  proxy-backend-query-fetch-size: -1
+  check-duplicate-table-enabled: true
+#  sql-comment-parse-enabled: false
+#  proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
+#    # Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
+#    # if client connections are more than proxy-frontend-netty-executor-size, especially executing slow SQL.
+#  proxy-backend-executor-suitable: OLAP
+#  proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
+#  sql-federation-enabled: false
+

二進制
docker/shardingproxy/ext-lib/mysql-connector-java-8.0.11.jar


+ 51 - 0
ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/controller/TestShardingController.java

@@ -0,0 +1,51 @@
+package org.dromara.demo.controller;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import lombok.RequiredArgsConstructor;
+import org.dromara.common.core.domain.R;
+import org.dromara.demo.domain.ShardingOrder;
+import org.dromara.demo.mapper.ShardingOrderMapper;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+@RequiredArgsConstructor
+@RestController
+@RequestMapping("/sharding")
+public class TestShardingController {
+
+    private final ShardingOrderMapper torderMapper;
+
+    @GetMapping("/page")
+    public R<Page<ShardingOrder>> page() {
+        Page<ShardingOrder> page = new Page<>();
+        page.setCurrent(3L);
+        QueryWrapper<ShardingOrder> queryWrapper = new QueryWrapper<>();
+        queryWrapper.orderByAsc("order_id");
+        torderMapper.selectPage(page,queryWrapper);
+        //List<ShardingOrder> records = page.getRecords();
+        //System.out.println(page.getTotal());
+//        for(ShardingOrder order : page.getRecords()){
+//            System.out.print(order.getTotalMoney()+" ");
+//        }
+        return R.ok(page);
+    }
+
+    @GetMapping("/insert")
+    public R<Void> insert() {
+        for(Long i = 1L; i <= 100L; i++){
+            ShardingOrder torder = new ShardingOrder();
+            torder.setUserId(i);
+            torder.setTotalMoney(100 + Integer.parseInt(i+""));
+            torderMapper.insert(torder);
+        }
+
+        return R.ok("分库分表数据批量插入成功!");
+
+    }
+
+}

+ 16 - 0
ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/domain/ShardingOrder.java

@@ -0,0 +1,16 @@
+package org.dromara.demo.domain;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+@TableName("t_order")
+@Data
+public class ShardingOrder {
+
+
+    private Long orderId;
+
+    private Long userId;
+
+    private int totalMoney;
+}

+ 17 - 0
ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/domain/ShardingOrderItem.java

@@ -0,0 +1,17 @@
+package org.dromara.demo.domain;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+@TableName("t_order_item")
+@Data
+public class ShardingOrderItem {
+
+    private Long orderItemId;
+
+    private Long orderId;
+
+    private Long userId;
+
+    private int totalMoney;
+}

+ 14 - 0
ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/mapper/ShardingOrderItemMapper.java

@@ -0,0 +1,14 @@
+package org.dromara.demo.mapper;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+import org.apache.ibatis.annotations.Mapper;
+import org.dromara.demo.domain.ShardingOrderItem;
+
+@Mapper
+@DS("sharding")
+public interface ShardingOrderItemMapper extends BaseMapper<ShardingOrderItem> {
+
+
+}

+ 15 - 0
ruoyi-example/ruoyi-demo/src/main/java/org/dromara/demo/mapper/ShardingOrderMapper.java

@@ -0,0 +1,15 @@
+package org.dromara.demo.mapper;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+import org.apache.ibatis.annotations.Mapper;
+import org.dromara.demo.domain.ShardingOrder;
+
+
+@Mapper
+@DS("sharding")
+public interface ShardingOrderMapper extends BaseMapper<ShardingOrder> {
+
+
+}

+ 6 - 1
ruoyi-example/ruoyi-demo/src/main/resources/application.yml

@@ -39,13 +39,18 @@ spring:
       # 设置默认的数据源或者数据源组,默认值即为 master
       primary: master
       datasource:
-        # 主库数据源
+#     主库数据源
         master:
           type: ${spring.datasource.type}
           driver-class-name: com.mysql.cj.jdbc.Driver
           url: ${datasource.system-master.url}
           username: ${datasource.system-master.username}
           password: ${datasource.system-master.password}
+        sharding:
+          driver-class-name: com.mysql.cj.jdbc.Driver
+          url: jdbc:mysql://192.168.0.60:3307/data-center_db?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
+          username: root
+          password: root
 #        oracle:
 #          type: ${spring.datasource.type}
 #          driverClassName: oracle.jdbc.OracleDriver

+ 7 - 0
ruoyi-example/ruoyi-demo/src/main/resources/mapper/demo/ShardingOrderItemMapper.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="org.dromara.demo.mapper.ShardingOrderItemMapper">
+
+</mapper>

+ 7 - 0
ruoyi-example/ruoyi-demo/src/main/resources/mapper/demo/ShardingOrderMapper.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="org.dromara.demo.mapper.ShardingOrderMapper">
+
+</mapper>

+ 49 - 0
ruoyi-example/ruoyi-demo/src/test/java/org/dromara/demo/TOrderTest.java

@@ -0,0 +1,49 @@
+package org.dromara.demo;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+
+import org.dromara.demo.domain.ShardingOrder;
+import org.dromara.demo.mapper.ShardingOrderMapper;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class TOrderTest {
+
+    @Autowired
+    ShardingOrderMapper torderMapper;
+
+
+    @Test
+    void find() {
+        //Order order = orderMapper.selectById(1640990702722723841L);
+    }
+
+    @Test
+    void page() {
+        Page<ShardingOrder> page = new Page<>();
+        page.setCurrent(3L);
+        QueryWrapper<ShardingOrder> queryWrapper = new QueryWrapper<>();
+        queryWrapper.orderByAsc("order_id");
+        torderMapper.selectPage(page,queryWrapper);
+        System.out.println(page.getTotal());
+        for(ShardingOrder order : page.getRecords()){
+            System.out.print(order.getTotalMoney()+" ");
+        }
+    }
+
+    @Test
+    void insert() {
+        for(Long i = 1L; i <= 100L; i++){
+            ShardingOrder torder = new ShardingOrder();
+            torder.setUserId(i);
+            torder.setTotalMoney(100 + Integer.parseInt(i+""));
+            torderMapper.insert(torder);
+        }
+
+    }
+
+
+}