上效果图:

注意ShardingSphere还在孵化,选择要考虑。
- 如果项目中已经有相关包,不用重复引入:

- 如果你在使用blade,先删除spring自带数据源配置

- yml配置:
spring:
shardingsphere:
datasource:
names: master,slave0
slave0:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://111.222.333.444:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: admin
master:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://555.666.777.888:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: root
masterslave:
slave-data-source-names: slave0
name: ms
master-data-source-name: master
props:
sql:
show: true
- OK.已经完成了…开启trace级别日志查看insert和select效果吧
- 没有两个mysql?简单贴下docker-compose:
spring:
shardingsphere:
datasource:
names: master,slave0
slave0:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://111.222.333.444:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: admin
master:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://555.666.777.888:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: root
masterslave:
slave-data-source-names: slave0
name: ms
master-data-source-name: master
props:
sql:
show: true
- master:当前目录下mysql/conf.d/mysqld.cnf
spring:
shardingsphere:
datasource:
names: master,slave0
slave0:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://111.222.333.444:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: admin
master:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://555.666.777.888:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: root
masterslave:
slave-data-source-names: slave0
name: ms
master-data-source-name: master
props:
sql:
show: true
- slave:当前目录下mysql/conf.d/mysqld.cnf
spring:
shardingsphere:
datasource:
names: master,slave0
slave0:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://111.222.333.444:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: admin
master:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://555.666.777.888:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: root
masterslave:
slave-data-source-names: slave0
name: ms
master-data-source-name: master
props:
sql:
show: true
当然如果你只有一个机子,那就复制docker-compose中的mysql并且修改端口号和挂载文件地址。
- 大概主从同步流程
spring:
shardingsphere:
datasource:
names: master,slave0
slave0:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://111.222.333.444:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: admin
master:
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://555.666.777.888:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
username: root
masterslave:
slave-data-source-names: slave0
name: ms
master-data-source-name: master
props:
sql:
show: true
至此,简单的msyql主从也配好了。然后自行测试吧。