提交 4b8086f8 编写于 作者: L liya.cookie

complete broadcast table for raw-jdbc

上级 1b8e8fd7
......@@ -29,7 +29,7 @@ public class DataSourceUtil {
private static final String USER_NAME = "root";
private static final String PASSWORD = "liya76133951";
private static final String PASSWORD = "";
public static DataSource createDataSource(final String dataSourceName) {
HikariDataSource result = new HikariDataSource();
......
......@@ -17,7 +17,11 @@
package io.shardingsphere.example.common.entity;
public class Country {
import java.io.Serializable;
public class Country implements Serializable {
private static final long serialVersionUID = 4522167390518926493L;
private long id;
......
package io.shardingsphere.example.common.jpa.entity;
import io.shardingsphere.example.common.entity.Country;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_country")
public class CountryEntity extends Country {
@Id
@Column(name = "id")
@Override
public long getId() {
return super.getId();
}
@Column(name = "name")
@Override
public String getName() {
return super.getName();
}
@Column(name = "code")
@Override
public String getCode() {
return super.getCode();
}
@Column(name = "language")
@Override
public String getLanguage() {
return super.getLanguage();
}
}
package io.shardingsphere.example.common.jpa.repository;
import io.shardingsphere.example.common.entity.Country;
import io.shardingsphere.example.common.repository.CountryRepository;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
@Repository
@Transactional
public class CountryRepositoryImpl implements CountryRepository {
private final static AtomicLong ID_INCREASE = new AtomicLong();
@PersistenceContext
private EntityManager entityManager;
@Override
public void createTableIfNotExists() {
throw new UnsupportedOperationException("createTableIfNotExists for JPA");
}
@Override
public void dropTable() {
throw new UnsupportedOperationException("truncateTable for JPA");
}
@Override
public void truncateTable() {
throw new UnsupportedOperationException("dropTable for JPA");
}
@Override
public Long insert(Country country) {
long id = ID_INCREASE.incrementAndGet();
country.setId(id);
entityManager.persist(country);
return id;
}
@Override
public void delete(Long id) {
Query query = entityManager.createQuery("DELETE FROM CountryEntity o WHERE o.id = ?1");
query.setParameter(1, id);
query.executeUpdate();
}
@Override
public List<Country> selectAll() {
return (List<Country>) entityManager.createQuery("SELECT o FROM CountryEntity o").getResultList();
}
}
......@@ -17,8 +17,11 @@
package io.shardingsphere.example.common.jpa.service;
import io.shardingsphere.example.common.entity.Country;
import io.shardingsphere.example.common.jpa.entity.CountryEntity;
import io.shardingsphere.example.common.jpa.entity.OrderEntity;
import io.shardingsphere.example.common.jpa.entity.OrderItemEntity;
import io.shardingsphere.example.common.repository.CountryRepository;
import io.shardingsphere.example.common.repository.OrderItemRepository;
import io.shardingsphere.example.common.repository.OrderRepository;
import org.springframework.stereotype.Service;
......@@ -27,6 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@Service
public class JPACommonServiceImpl implements JPACommonService {
......@@ -36,6 +40,9 @@ public class JPACommonServiceImpl implements JPACommonService {
@Resource
private OrderItemRepository orderItemRepository;
@Resource
private CountryRepository countryRepository;
@Override
public void initEnvironment() {
......@@ -49,9 +56,9 @@ public class JPACommonServiceImpl implements JPACommonService {
@Transactional
public void processSuccess() {
System.out.println("-------------- Process Success Begin ---------------");
List<Long> orderIds = insertData();
InsertResult insertResult = insertData();
printData();
deleteData(orderIds);
deleteData(insertResult.getOrderIds(),insertResult.getCountryIds());
printData();
System.out.println("-------------- Process Success Finish --------------");
}
......@@ -64,9 +71,15 @@ public class JPACommonServiceImpl implements JPACommonService {
System.out.println("-------------- Process Failure Finish --------------");
throw new RuntimeException("Exception occur for transaction test.");
}
private List<Long> insertData() {
private InsertResult insertData(){
System.out.println("---------------------------- Insert Data ----------------------------");
List<Long> orderIds = insertOrderData();
List<Long> countryIds = insertCountryData();
return new InsertResult(orderIds,countryIds);
}
private List<Long> insertOrderData() {
List<Long> result = new ArrayList<>(10);
for (int i = 1; i <= 10; i++) {
OrderEntity order = new OrderEntity();
......@@ -82,13 +95,38 @@ public class JPACommonServiceImpl implements JPACommonService {
}
return result;
}
private List<Long> insertCountryData(){
List<Long> result = new ArrayList<>();
Locale[] locales = Locale.getAvailableLocales();
int i = 0;
for(Locale l:locales){
final String country = l.getCountry();
if(country == null || "".equals(country)){
continue;
}
CountryEntity currCountry = new CountryEntity();
currCountry.setName(l.getDisplayCountry(l));
currCountry.setLanguage(l.getLanguage());
currCountry.setCode(l.getCountry());
countryRepository.insert(currCountry);
result.add(currCountry.getId());
if(++i == 10){
break;
}
}
return result;
}
private void deleteData(final List<Long> orderIds) {
private void deleteData(final List<Long> orderIds,final List<Long> countryIds) {
System.out.println("---------------------------- Delete Data ----------------------------");
for (Long each : orderIds) {
orderRepository.delete(each);
orderItemRepository.delete(each);
}
for (Long each: countryIds){
countryRepository.delete(each);
}
}
@Override
......@@ -101,5 +139,29 @@ public class JPACommonServiceImpl implements JPACommonService {
for (Object each : orderItemRepository.selectAll()) {
System.out.println(each);
}
System.out.println("---------------------------- Print Country Data -------------------");
for (Object each : countryRepository.selectAll()) {
System.out.println(each);
}
}
private static class InsertResult{
private List<Long> orderIds;
private List<Long> countryIds;
public InsertResult(List<Long> orderIds, List<Long> countryIds) {
this.orderIds = orderIds;
this.countryIds = countryIds;
}
public List<Long> getOrderIds() {
return orderIds;
}
public List<Long> getCountryIds() {
return countryIds;
}
}
}
......@@ -17,6 +17,7 @@
package io.shardingsphere.example.sharding.data.jdbc;
import io.shardingsphere.example.common.jdbc.repository.CountryRepositroyImpl;
import io.shardingsphere.example.common.jdbc.repository.OrderItemRepositoryImpl;
import io.shardingsphere.example.common.jdbc.repository.RangeOrderRepositoryImpl;
import io.shardingsphere.example.common.jdbc.service.CommonServiceImpl;
......@@ -47,6 +48,6 @@ public class JavaRangeConfigurationExample {
}
private static CommonService getCommonService(final DataSource dataSource) {
return new CommonServiceImpl(new RangeOrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource));
return new CommonServiceImpl(new RangeOrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource),new CountryRepositroyImpl(dataSource));
}
}
......@@ -17,6 +17,7 @@
package io.shardingsphere.example.sharding.data.jdbc;
import io.shardingsphere.example.common.jdbc.repository.CountryRepositroyImpl;
import io.shardingsphere.example.common.jdbc.repository.OrderItemRepositoryImpl;
import io.shardingsphere.example.common.jdbc.repository.OrderRepositoryImpl;
import io.shardingsphere.example.common.jdbc.service.CommonServiceImpl;
......@@ -48,6 +49,6 @@ public class YamlConfigurationExample {
}
private static CommonService getCommonService(final DataSource dataSource) {
return new CommonServiceImpl(new OrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource));
return new CommonServiceImpl(new OrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource),new CountryRepositroyImpl(dataSource));
}
}
......@@ -17,6 +17,7 @@
package io.shardingsphere.example.sharding.data.jdbc;
import io.shardingsphere.example.common.jdbc.repository.CountryRepositroyImpl;
import io.shardingsphere.example.common.jdbc.repository.OrderItemRepositoryImpl;
import io.shardingsphere.example.common.jdbc.repository.RangeOrderRepositoryImpl;
import io.shardingsphere.example.common.jdbc.service.CommonServiceImpl;
......@@ -48,6 +49,6 @@ public class YamlRangeConfigurationExample {
}
private static CommonService getCommonService(final DataSource dataSource) {
return new CommonServiceImpl(new RangeOrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource));
return new CommonServiceImpl(new RangeOrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource),new CountryRepositroyImpl(dataSource));
}
}
......@@ -41,6 +41,7 @@ public final class ShardingDatabasesAndTablesConfigurationPrecise implements Exa
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.getBroadcastTables().add("t_country");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
......@@ -48,12 +49,14 @@ public final class ShardingDatabasesAndTablesConfigurationPrecise implements Exa
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
......@@ -62,8 +65,4 @@ public final class ShardingDatabasesAndTablesConfigurationPrecise implements Exa
result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
......@@ -43,6 +43,7 @@ public final class ShardingDatabasesAndTablesConfigurationRange implements Examp
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.getBroadcastTables().add("t_country");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new StandardShardingStrategyConfiguration("user_id", new PreciseModuloShardingDatabaseAlgorithm(), new RangeModuloShardingDatabaseAlgorithm()));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(
......@@ -52,12 +53,14 @@ public final class ShardingDatabasesAndTablesConfigurationRange implements Examp
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
......@@ -66,8 +69,4 @@ public final class ShardingDatabasesAndTablesConfigurationRange implements Examp
result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
......@@ -45,12 +45,14 @@ public final class ShardingDatabasesConfigurationPrecise implements ExampleConfi
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
......@@ -59,8 +61,4 @@ public final class ShardingDatabasesConfigurationPrecise implements ExampleConfi
result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
......@@ -40,6 +40,7 @@ public final class ShardingDatabasesConfigurationRange implements ExampleConfigu
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBroadcastTables().add("t_country");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new StandardShardingStrategyConfiguration("user_id", new PreciseModuloShardingDatabaseAlgorithm(), new RangeModuloShardingDatabaseAlgorithm()));
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
......@@ -47,12 +48,14 @@ public final class ShardingDatabasesConfigurationRange implements ExampleConfigu
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
......@@ -61,8 +64,4 @@ public final class ShardingDatabasesConfigurationRange implements ExampleConfigu
result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
......@@ -45,6 +45,7 @@ public final class ShardingMasterSlaveConfigurationPrecise implements ExampleCon
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.getBroadcastTables().add("t_country");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseModuloShardingDatabaseAlgorithm()));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
shardingRuleConfig.setMasterSlaveRuleConfigs(getMasterSlaveRuleConfigurations());
......@@ -53,12 +54,14 @@ public final class ShardingMasterSlaveConfigurationPrecise implements ExampleCon
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order", "ds_${0..1}.t_order_${[0, 1]}");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${[0, 1]}");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${[0, 1]}");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static List<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() {
......@@ -77,8 +80,4 @@ public final class ShardingMasterSlaveConfigurationPrecise implements ExampleCon
result.put("demo_ds_master_1_slave_1", DataSourceUtil.createDataSource("demo_ds_master_1_slave_1"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
......@@ -47,6 +47,7 @@ public final class ShardingMasterSlaveConfigurationRange implements ExampleConfi
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.getBroadcastTables().add("t_country");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(
new StandardShardingStrategyConfiguration("user_id", new PreciseModuloShardingDatabaseAlgorithm(), new RangeModuloShardingDatabaseAlgorithm()));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(
......@@ -57,12 +58,14 @@ public final class ShardingMasterSlaveConfigurationRange implements ExampleConfi
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order", "ds_${0..1}.t_order_${[0, 1]}");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${[0, 1]}");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${[0, 1]}");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static List<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() {
......@@ -81,8 +84,4 @@ public final class ShardingMasterSlaveConfigurationRange implements ExampleConfi
result.put("demo_ds_master_1_slave_1", DataSourceUtil.createDataSource("demo_ds_master_1_slave_1"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
......@@ -40,20 +40,21 @@ public final class ShardingTablesConfigurationPrecise implements ExampleConfigur
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.getBroadcastTables().add("t_country");
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm()));
Properties properties = new Properties();
properties.put("sql.show", true);
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, properties);
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
}
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order", "demo_ds.t_order_${[0, 1]}");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item", "demo_ds.t_order_item_${[0, 1]}");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "demo_ds.t_order_item_${[0, 1]}");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
......@@ -61,8 +62,4 @@ public final class ShardingTablesConfigurationPrecise implements ExampleConfigur
result.put("demo_ds", DataSourceUtil.createDataSource("demo_ds"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
......@@ -41,6 +41,7 @@ public final class ShardingTablesConfigurationRange implements ExampleConfigurat
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.getBroadcastTables().add("t_country");
shardingRuleConfig.setDefaultTableShardingStrategyConfig(
new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm(), new RangeModuloShardingTableAlgorithm()));
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties());
......@@ -48,12 +49,14 @@ public final class ShardingTablesConfigurationRange implements ExampleConfigurat
private static TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order", "demo_ds.t_order_${[0, 1]}");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties()));
return result;
}
private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
return new TableRuleConfiguration("t_order_item", "demo_ds.t_order_item_${[0, 1]}");
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "demo_ds.t_order_item_${[0, 1]}");
result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", new Properties()));
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
......@@ -61,8 +64,4 @@ public final class ShardingTablesConfigurationRange implements ExampleConfigurat
result.put("demo_ds", DataSourceUtil.createDataSource("demo_ds"));
return result;
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
return new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", new Properties());
}
}
dataSources:
ds_master: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_slave_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_slave_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -21,4 +21,4 @@ masterSlaveRule:
slaveDataSourceNames: [ds_slave_0, ds_slave_1]
props:
sql.show: true
sql.show: false
dataSources:
ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -19,8 +19,13 @@ shardingRule:
column: order_id
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
defaultDatabaseStrategy:
standard:
......@@ -31,4 +36,4 @@ shardingRule:
none:
props:
sql.show: true
sql.show: false
dataSources:
ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -29,8 +29,13 @@ shardingRule:
shardingColumn: order_id
preciseAlgorithmClassName: io.shardingsphere.example.algorithm.PreciseModuloShardingTableAlgorithm
rangeAlgorithmClassName: io.shardingsphere.example.algorithm.RangeModuloShardingTableAlgorithm
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
defaultDatabaseStrategy:
standard:
......
dataSources:
ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -26,9 +26,14 @@ shardingRule:
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
algorithmExpression: t_order_item_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
defaultDatabaseStrategy:
inline:
......@@ -38,4 +43,4 @@ shardingRule:
none:
props:
sql.show: true
sql.show: false
dataSources:
ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -19,8 +19,13 @@ shardingRule:
column: order_id
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
defaultDatabaseStrategy:
inline:
......@@ -30,4 +35,4 @@ shardingRule:
none:
props:
sql.show: true
sql.show: false
dataSources:
ds_master_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_0_slave_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_0_slave_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_1_slave_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_1_slave_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -49,8 +49,13 @@ shardingRule:
shardingColumn: order_id
preciseAlgorithmClassName: io.shardingsphere.example.algorithm.PreciseModuloShardingTableAlgorithm
rangeAlgorithmClassName: io.shardingsphere.example.algorithm.RangeModuloShardingTableAlgorithm
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
defaultDatabaseStrategy:
standard:
......
dataSources:
ds_master_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_0_slave_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_0_slave_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_0_slave_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_1_slave_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_0
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_master_1_slave_1: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_1
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_master_1_slave_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -46,9 +46,14 @@ shardingRule:
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
algorithmExpression: t_order_item_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
defaultDatabaseStrategy:
inline:
......@@ -66,4 +71,4 @@ shardingRule:
loadBalanceAlgorithmType: ROUND_ROBIN
props:
sql.show: true
sql.show: false
dataSources:
ds: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -24,8 +24,13 @@ shardingRule:
shardingColumn: order_id
preciseAlgorithmClassName: io.shardingsphere.example.algorithm.PreciseModuloShardingTableAlgorithm
rangeAlgorithmClassName: io.shardingsphere.example.algorithm.RangeModuloShardingTableAlgorithm
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
props:
sql.show: false
dataSources:
ds: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
......@@ -22,8 +22,13 @@ shardingRule:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_country
props:
sql.show: true
sql.show: false
......@@ -24,6 +24,7 @@ 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 org.springframework.core.env.PropertySource;
@ComponentScan("io.shardingsphere.example.common.jpa")
@EntityScan(basePackages = "io.shardingsphere.example.common.jpa.entity")
......
......@@ -27,3 +27,4 @@ sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.column=order_item_id
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
sharding.jdbc.config.sharding.broadcast-tables=t_country
......@@ -2,13 +2,13 @@ sharding.jdbc.datasource.names=ds_0,ds_1
sharding.jdbc.datasource.ds_0.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.ds_0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds_0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
sharding.jdbc.datasource.ds_0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
sharding.jdbc.datasource.ds_0.username=root
sharding.jdbc.datasource.ds_0.password=
sharding.jdbc.datasource.ds_1.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.ds_1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds_1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
sharding.jdbc.datasource.ds_1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
sharding.jdbc.datasource.ds_1.username=root
sharding.jdbc.datasource.ds_1.password=
......@@ -21,3 +21,4 @@ sharding.jdbc.config.sharding.tables.t_order.key-generator.type=SNOWFLAKE
sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds_$->{0..1}.t_order_item
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.column=order_item_id
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
sharding.jdbc.config.sharding.broadcast-tables=t_country
......@@ -55,6 +55,7 @@ sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.column=order_item_id
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
sharding.jdbc.config.sharding.broadcast-tables=t_country
sharding.jdbc.config.sharding.master-slave-rules.ds_0.master-data-source-name=ds_master_0
sharding.jdbc.config.sharding.master-slave-rules.ds_0.slave-data-source-names=ds_master_0_slave_0, ds_master_0_slave_1
......
......@@ -17,3 +17,4 @@ sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.column=order_item_id
sharding.jdbc.config.sharding.tables.t_order_item.key-generator.type=SNOWFLAKE
sharding.jdbc.config.sharding.broadcast-tables=t_country
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册