环境准备
安装 JDK
使用 JDK 8 或以上版本。
安装 Maven
使用 Maven 3.6 或以上版本。
MySQL
本地安装MySQL。 并且执行ShardingSphere初始化脚本src/resources/manual_schema.sql ,后续执行example时依赖该脚本:
DROP SCHEMA IF EXISTS demo_ds;
DROP SCHEMA IF EXISTS demo_ds_0;
DROP SCHEMA IF EXISTS demo_ds_1;
DROP SCHEMA IF EXISTS demo_write_ds;
DROP SCHEMA IF EXISTS demo_read_ds_0;
DROP SCHEMA IF EXISTS demo_read_ds_1;
DROP SCHEMA IF EXISTS demo_write_ds_0;
DROP SCHEMA IF EXISTS demo_write_ds_0_read_0;
DROP SCHEMA IF EXISTS demo_write_ds_0_read_1;
DROP SCHEMA IF EXISTS demo_write_ds_1;
DROP SCHEMA IF EXISTS demo_write_ds_1_read_0;
DROP SCHEMA IF EXISTS demo_write_ds_1_read_1;
DROP SCHEMA IF EXISTS shadow_demo_ds;
DROP SCHEMA IF EXISTS shadow_demo_ds_0;
DROP SCHEMA IF EXISTS shadow_demo_ds_1;
DROP SCHEMA IF EXISTS demo_shadow_write_ds;
DROP SCHEMA IF EXISTS demo_shadow_read_ds;
DROP SCHEMA IF EXISTS demo_read_ds;
CREATE SCHEMA IF NOT EXISTS demo_ds;
CREATE SCHEMA IF NOT EXISTS demo_ds_0;
CREATE SCHEMA IF NOT EXISTS demo_ds_1;
CREATE SCHEMA IF NOT EXISTS demo_write_ds;
CREATE SCHEMA IF NOT EXISTS demo_read_ds_0;
CREATE SCHEMA IF NOT EXISTS demo_read_ds_1;
CREATE SCHEMA IF NOT EXISTS demo_write_ds_0;
CREATE SCHEMA IF NOT EXISTS demo_write_ds_0_read_0;
CREATE SCHEMA IF NOT EXISTS demo_write_ds_0_read_1;
CREATE SCHEMA IF NOT EXISTS demo_write_ds_1;
CREATE SCHEMA IF NOT EXISTS demo_write_ds_1_read_0;
CREATE SCHEMA IF NOT EXISTS demo_write_ds_1_read_1;
CREATE SCHEMA IF NOT EXISTS shadow_demo_ds;
CREATE SCHEMA IF NOT EXISTS shadow_demo_ds_0;
CREATE SCHEMA IF NOT EXISTS shadow_demo_ds_1;
CREATE SCHEMA IF NOT EXISTS demo_shadow_write_ds;
CREATE SCHEMA IF NOT EXISTS demo_shadow_read_ds;
CREATE SCHEMA IF NOT EXISTS demo_read_ds;
准备仓库
将仓库clone到本地。
git clone https://github.com/apache/shardingsphere.git
cd shardingsphere
编译并安装所有模块到 Maven 本地仓库缓存,同时会生成 ANTLR .g4 语法文件对应的解析器 Java 类,这样在 IDE 就不会有相关的编译错误了。
mvn clean install -Dmaven.javadoc.skip=true -Dcheckstyle.skip=true -Drat.skip=true -Djacoco.skip=true -DskipITs -DskipTests -Prelease
导入IDEA
跟普通Maven项目一样,导入IDEA即可,需要在根目录的pom.xml 增加module的配置,否则IDEA无法识别到examples 中的项目:
<modules>
<module>shardingsphere-spi</module>
<module>shardingsphere-sql-parser</module>
<module>shardingsphere-distsql</module>
<module>shardingsphere-db-protocol</module>
<module>shardingsphere-infra</module>
<module>shardingsphere-mode</module>
<module>shardingsphere-kernel</module>
<module>shardingsphere-jdbc</module>
<module>shardingsphere-proxy</module>
<module>shardingsphere-features</module>
<module>shardingsphere-agent</module>
<module>shardingsphere-test</module>
<module>shardingsphere-distribution</module>
<module>examples</module>
</modules>
运行Example
examples 中有很多样例,以ShardingRawYamlConfigurationExample 为例,需要在src/main/resources/META-INF/sharding-auto-tables.yaml 修改数据源:
dataSources:
ds_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
之后直接运行ShardingRawYamlConfigurationExample 即可,如看到以下日志,则运行成功:
[INFO ] 2022-05-09 17:25:53,770 --main-- [com.zaxxer.hikari.HikariDataSource] HikariPool-1 - Starting...
[INFO ] 2022-05-09 17:25:54,098 --main-- [com.zaxxer.hikari.HikariDataSource] HikariPool-1 - Start completed.
[INFO ] 2022-05-09 17:25:54,107 --main-- [com.zaxxer.hikari.HikariDataSource] HikariPool-2 - Starting...
[INFO ] 2022-05-09 17:25:54,231 --main-- [com.zaxxer.hikari.HikariDataSource] HikariPool-2 - Start completed.
[INFO ] 2022-05-09 17:25:56,027 --main-- [ShardingSphere-SQL] Logic SQL: CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))
[INFO ] 2022-05-09 17:25:56,027 --main-- [ShardingSphere-SQL] SQLStatement: MySQLCreateTableStatement(containsNotExistClause=true)
[INFO ] 2022-05-09 17:25:56,028 --main-- [ShardingSphere-SQL] Actual SQL: ds_1 ::: CREATE TABLE IF NOT EXISTS t_order_1 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))
[INFO ] 2022-05-09 17:25:56,028 --main-- [ShardingSphere-SQL] Actual SQL: ds_1 ::: CREATE TABLE IF NOT EXISTS t_order_3 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))
[INFO ] 2022-05-09 17:25:56,028 --main-- [ShardingSphere-SQL] Actual SQL: ds_0 ::: CREATE TABLE IF NOT EXISTS t_order_0 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))
...
|