1、简介
实现动机
-
配置集中化:越来越多的运行时实例,使得散落的配置难于管理,配置不同步导致的问题十分严重。将配置集中于配置中心,可以更加有效进行管理。
-
配置动态化:配置修改后的分发,是配置中心可以提供的另一个重要能力。它可支持数据源、表与分片及读写分离策略的动态切换。
2、配置手册
使用治理功能需要指定一个注册中心。配置将全部存入注册中心,可以在每次启动时使用本地配置覆盖注册中心配置,也可以只通过注册中心读取配置。
注:本文使用zookeeper作为注册中心,其他配置中心可参考官网。
3、官方案例 -> orchestration-spring-boot-example
https://github.com/apache/shardingsphere-example
4、maven
<dependency>
<groupId>org.apache.shardingsphere.example</groupId>
<artifactId>example-spring-jpa</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-orchestration-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>io.shardingsphere</groupId>-->
<!--<artifactId>sharding-orchestration-reg-etcd</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
5、application.properties配置
spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.show_sql=false
spring.profiles.active=local-zookeeper-sharding-databases-tables
6、application-local-zookeeper-sharding-databases-tables.properties配置
spring.shardingsphere.datasource.names=ds_0,ds_1
spring.shardingsphere.datasource.ds_0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds_0.jdbc-url=jdbc:mysql://localhost:3306/sharding_sphere_1?serverTimezone=UTC
spring.shardingsphere.datasource.ds_0.username=root
spring.shardingsphere.datasource.ds_0.password=1234
spring.shardingsphere.datasource.ds_0.max-active=16
spring.shardingsphere.datasource.ds_1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds_1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds_1.jdbc-url=jdbc:mysql://localhost:3306/sharding_sphere_2?serverTimezone=UTC
spring.shardingsphere.datasource.ds_1.username=root
spring.shardingsphere.datasource.ds_1.password=1234
spring.shardingsphere.datasource.ds_1.max-active=16
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds_$->{user_id % 2}
spring.shardingsphere.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.sharding.default-datasource-name=ds_0
spring.shardingsphere.sharding.broadcast-tables=t_address
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds_$->{0..1}.t_order_$->{0..1}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.key-generator.props.worker.id=123
spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds_$->{0..1}.t_order_item_$->{0..1}
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.sharding.tables.t_order_item.key-generator.column=order_item_id
spring.shardingsphere.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order_item.key-generator.props.worker.id=123
#编排治理
#zookeeper内层节点名称
spring.shardingsphere.orchestration.name=demo_spring_boot_ds_sharding
spring.shardingsphere.orchestration.overwrite=true
#注册中心类型
spring.shardingsphere.orchestration.registry.type=zookeeper
#注册中心地址
spring.shardingsphere.orchestration.registry.server-lists=localhost:2181
#zookeeper外层节点名称
spring.shardingsphere.orchestration.registry.namespace=orchestration-spring-boot-demo
#是否查看sql 将使用该数据进行配置刷新测试
spring.shardingsphere.props.sql.show=false
7、启动类
package org.apache.shardingsphere.example.orchestration.spring.boot;
import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
import org.apache.shardingsphere.example.core.api.service.ExampleService;
import org.apache.shardingsphere.example.core.jpa.service.OrderServiceImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
@ComponentScan("org.apache.shardingsphere.example.core.jpa")
@EntityScan(basePackages = "org.apache.shardingsphere.example.core.jpa.entity")
@SpringBootApplication(exclude = JtaAutoConfiguration.class)
public class ExampleMain {
public static void main(final String[] args) throws SQLException {
try (ConfigurableApplicationContext applicationContext = SpringApplication.run(ExampleMain.class, args)) {
ExampleExecuteTemplate.run(applicationContext.getBean(ExampleService.class));
OrderServiceImpl bean = applicationContext.getBean(OrderServiceImpl.class);
bean.printData();
System.out.println("~~~~~~~~~~~update config~~~~~~~~~~~");
TimeUnit.SECONDS.sleep(15);
OrderServiceImpl bean2 = applicationContext.getBean(OrderServiceImpl.class);
bean2.printData();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
8、zookeeper更新配置
9、查看日志是否打印sql
略
10、查看数据源配置
get /orchestration-spring-boot-demo/demo_spring_boot_ds_sharding/config/schema/logic_db/datasource