懒得按以前格式写了,纯粘代码
几点注意:
1、冲突问题,官网的配置未解决curator问题;(P.S. 最新版5.0.0α也没有处理好spring cloud 2.x+的问题,会造成no value bound,详见issue#8299
https://github.com/apache/shardingsphere/issues/8299
2、配置问题,官网的配置文档还停留在4.0.0的配置,看源码,配置的读取代码为
@Generated
public void setOrchestration(Map<String, YamlCenterRepositoryConfiguration> orchestration) {
this.orchestration = orchestration;
}
可以看到接受的配置为Map<String, YamlCenterRepositoryConfiguration> orchestration,
所以配置应该是spring.shardingsphere.orchestration.名称.属性,而不是旧的spring.shardingsphere.orchestration.registry.xxx,这点具体可以看github的example
另:nacos居然只能做config center,配置为registry_center会报错,没有该枚举类型。有点尴尬,这样我还要额外再引入zk,那我何不一次性zk搞定呢。
spring.shardingsphere.orchestration.<String>.orchestration-type=registry_center,config_center
spring.shardingsphere.orchestration.<String>.instance-type=zookeeper
spring.shardingsphere.orchestration.<String>.server-lists=localhost:2181
spring.shardingsphere.orchestration.<String>.namespace=sharding-test
spring.shardingsphere.orchestration.<String>.props.overwrite=false
3、shardingsphere-ui运行的问题,官网提供的ui下载包有点问题,解压出来缺少jar,主要是缺胳膊少腿,如图,需要自己把jar包补充进去
具体代码如下(非生产代码,抽空额外做了个测试DEMO)
application.properties
mybatis.type-aliases-package=team.zhh.**.entity
mybatis.mapperLocations=classpath:mybatis/**/*Mapper.xml
logging.level.team.zhh.dao=info
spring.shardingsphere.datasource.names=master39,slave136,slave137
spring.shardingsphere.masterslave.name=testcloud
spring.shardingsphere.masterslave.master-data-source-name=master39
spring.shardingsphere.masterslave.slave-data-source-names=slave136,slave137
spring.shardingsphere.masterslave.load-balance-algorithm-type=random
spring.shardingsphere.datasource.master39.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master39.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master39.jdbcUrl=jdbc:mysql://192.168.10.39:3306/testbase?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT&allowMultiQueries=true
spring.shardingsphere.datasource.master39.username=root
spring.shardingsphere.datasource.master39.password=123456
spring.shardingsphere.datasource.master39.minimum-idle=8
spring.shardingsphere.datasource.master39.max-lifetime=1800000
spring.shardingsphere.datasource.master39.connection-timeout=30000
spring.shardingsphere.datasource.master39.idle-timeout=30000
spring.shardingsphere.datasource.master39.validation-timeout=3000
spring.shardingsphere.datasource.master39.connection-init-sql=SELECT 1
spring.shardingsphere.datasource.master39.maximum-pool-size=8
spring.shardingsphere.datasource.master39.pool-name=masterPool
spring.shardingsphere.datasource.master39.auto-commit=true
spring.shardingsphere.datasource.master39.enabled=true
spring.shardingsphere.datasource.slave136.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave136.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave136.jdbcUrl=jdbc:mysql://192.168.10.136:3306/testbase?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT&allowMultiQueries=true
spring.shardingsphere.datasource.slave136.username=root
spring.shardingsphere.datasource.slave136.password=123456
spring.shardingsphere.datasource.slave136.minimum-idle=8
spring.shardingsphere.datasource.slave136.max-lifetime=1800000
spring.shardingsphere.datasource.slave136.connection-timeout=30000
spring.shardingsphere.datasource.slave136.idle-timeout=30000
spring.shardingsphere.datasource.slave136.validation-timeout=3000
spring.shardingsphere.datasource.slave136.connection-init-sql=SELECT 1
spring.shardingsphere.datasource.slave136.maximum-pool-size=8
spring.shardingsphere.datasource.slave136.pool-name=s1pool
spring.shardingsphere.datasource.slave136.auto-commit=true
spring.shardingsphere.datasource.slave136.enabled=true
spring.shardingsphere.datasource.slave137.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave137.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave137.jdbcUrl=jdbc:mysql://192.168.10.137:3306/testbase?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT&allowMultiQueries=true
spring.shardingsphere.datasource.slave137.username=root
spring.shardingsphere.datasource.slave137.password=123456
spring.shardingsphere.datasource.slave137.minimum-idle=8
spring.shardingsphere.datasource.slave137.max-lifetime=1800000
spring.shardingsphere.datasource.slave137.connection-timeout=30000
spring.shardingsphere.datasource.slave137.idle-timeout=30000
spring.shardingsphere.datasource.slave137.validation-timeout=3000
spring.shardingsphere.datasource.slave137.connection-init-sql=SELECT 1
spring.shardingsphere.datasource.slave137.maximum-pool-size=8
spring.shardingsphere.datasource.slave137.pool-name=s2pool
spring.shardingsphere.datasource.slave137.auto-commit=true
spring.shardingsphere.datasource.slave137.enabled=true
spring.shardingsphere.props.sql.show=true
#spring.shardingsphere.orchestration.testsharding.orchestration-type=
#spring.shardingsphere.orchestration.testsharding.instance-type=nacos
#spring.shardingsphere.orchestration.testsharding.server-lists=localhost:8848
#spring.shardingsphere.orchestration.testsharding.namespace=sharding-test
#spring.shardingsphere.orchestration.testsharding.props.overwrite=true
spring.shardingsphere.orchestration.testsharding2.orchestration-type=registry_center,config_center
spring.shardingsphere.orchestration.testsharding2.instance-type=zookeeper
spring.shardingsphere.orchestration.testsharding2.server-lists=localhost:2181
spring.shardingsphere.orchestration.testsharding2.namespace=sharding-test
spring.shardingsphere.orchestration.testsharding2.props.overwrite=false
pom.