当前位置:网站首页>Adding sharding sphere5.0.0 sub tables to the ruoyi framework (adding custom sub table policies through SPI)
Adding sharding sphere5.0.0 sub tables to the ruoyi framework (adding custom sub table policies through SPI)
2022-06-29 11:27:00 【Procedural ape (siege lion)】
sharding shphere As a sub database and sub table component , When processing table splitting business , Compared with realizing the table splitting function by yourself , There are still many obvious advantages .
For one from 0 At the beginning springboot project , add to sharding sphere It is relatively simple to divide databases and tables , Sometimes it is necessary to add... To the existing program framework sharding sphere Database and table function of , This requires specific configuration according to the characteristics of the framework itself .
1. Add dependency
stay ruoyi-framework\pom.xml Module add sharding-jdbc Integration dependence :
<!-- sharding-jdbc Sub database and sub table -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.0.0</version>
</dependency>2. Create a test library
The same database as the main program is used here , So there is no need to create a separate database .
3. Create two test order tables
create table sys_order
(
order_id bigint(20) not null comment ' Order ID',
user_id bigint(64) not null comment ' The user id ',
status char(1) not null comment ' state (0 Successful trade 1 The deal failed )',
order_no varchar(64) default null comment ' Order flow ',
primary key (order_id)
) engine=innodb comment = ' Order information sheet ';
create table sys_order_0
(
order_id bigint(20) not null comment ' Order ID',
user_id bigint(64) not null comment ' The user id ',
status char(1) not null comment ' state (0 Successful trade 1 The deal failed )',
order_no varchar(64) default null comment ' Order flow ',
primary key (order_id)
) engine=innodb comment = ' Order information sheet ';
create table sys_order_1
(
order_id bigint(20) not null comment ' Order ID',
user_id bigint(64) not null comment ' The user id ',
status char(1) not null comment ' state (0 Successful trade 1 The deal failed )',
order_no varchar(64) default null comment ' Order flow ',
primary key (order_id)
) engine=innodb comment = ' Order information sheet ';4. Add data source to configuration file
stay application-druid.yml Add test data source , Location is at the same level as the master data source :
# Data source configuration
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# Main database data source
master:
url: jdbc:mysql://localhost:3306/ry473?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: password
# Order Library 1
shardsource:
enabled: true
url: jdbc:mysql://localhost:3306/ry473?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: password 5. Code generation tools generate code components
Generate through the code generation function sys_order Related components and added to the project , Include page 、 controller 、 Service layer 、mapper as well as mapper.xml Documents, etc. .
6. add to shardingsphere Configuration class
package com.ruoyi.framework.config;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.ruoyi.framework.config.properties.DruidProperties;
/**
* sharding Configuration information
*
* @author ruoyi
*/
@Configuration
public class ShardingDataSourceConfig
{
@Bean
@ConfigurationProperties("spring.datasource.druid.shardsource")
@ConditionalOnProperty(prefix = "spring.datasource.druid.shardsource", name = "enabled", havingValue = "true")
public DataSource shardDataSource(DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
// @Bean
// @ConfigurationProperties("spring.datasource.druid.order2")
// @ConditionalOnProperty(prefix = "spring.datasource.druid.order2", name = "enabled", havingValue = "true")
// public DataSource order2DataSource(DruidProperties druidProperties)
// {
// DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
// return druidProperties.dataSource(dataSource);
// }
@Bean(name = "shardingDataSource")
public DataSource shardingDataSource(@Qualifier("shardDataSource") DataSource shardDataSource) throws SQLException
{
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("order", shardDataSource);
// dataSourceMap.put("order2", order2DataSource);
// sys_order Table rule configuration
// TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("sys_order", "order$->{1..2}.sys_order_$->{0..1}");
ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("sys_order", "order.sys_order_$->{0..1}");
orderTableRuleConfig.setTableShardingStrategy(
new StandardShardingStrategyConfiguration("order_id", "tableShardingAlgorithm"));
// Configure the sub database strategy
// orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "order$->{user_id % 2 + 1}"));
// Configure sub table strategy
// orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "sys_order_$->{order_id % 2}"));
// Distributed primary key
orderTableRuleConfig.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
// Configure sharding rules
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTables().add(orderTableRuleConfig);
Properties pDatabase = new Properties();
pDatabase.setProperty("shardCount", "2");
// Set up the sub table policy
ShardingSphereAlgorithmConfiguration ssactable = new ShardingSphereAlgorithmConfiguration(
"ORDER_ID_SHARD", pDatabase);
shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", ssactable);
// built-in Snowflake Distributed sequence algorithm configuration
Properties snowflakeProp = new Properties();
snowflakeProp.setProperty("worker-id", "1");
shardingRuleConfig.getKeyGenerators().put("snowflake",
new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", snowflakeProp));
// Get data source object
DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap,
Collections.singleton(shardingRuleConfig),
getProperties());
return dataSource;
}
/**
* System parameter configuration
*/
private Properties getProperties()
{
Properties shardingProperties = new Properties();
shardingProperties.put("sql.show", true);
return shardingProperties;
}
}
7. druid Configuration class configuration
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
setDataSource(targetDataSources, DataSourceType.SHARDING.name(), "shardingDataSource");
return new DynamicDataSource(masterDataSource, targetDataSources);
}8. Add a table splitting policy
package com.ruoyi.framework.config;
import java.util.Collection;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class TableShardingAlgorithm implements StandardShardingAlgorithm<Long>{
private static Logger log = LoggerFactory.getLogger(TableShardingAlgorithm.class);
private Properties props;
// @Value("${sharding.shardcount}")
private Long shardCount = 2l;
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
@Override
public String doSharding(Collection<String> tableNames, PreciseShardingValue<Long> shardingValue) {
Long lShard = 0l;
lShard = (Long)shardingValue.getValue();
// Read the set parameters
if(props != null)
{
String shardCountStr = props.getProperty("shardCount", "2");
shardCount = Long.parseLong(shardCountStr);
}
Long lowMod = lShard % 2;
Long shardTableMod = lowMod;
// Convert to a formatted string
String shardModStr = "_" + shardTableMod;
for (String table : tableNames) {
if (table.endsWith(shardModStr)) {
return table;
}
}
return "";
}
@Override
public String getType() {
// TODO Auto-generated method stub
return "ORDER_ID_SHARD";
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public Collection<String> doSharding(Collection<String> tableNames,
RangeShardingValue<Long> shardingValue) {
return null;
}
}
9. adopt spi Set up the sub table policy
stay resource Create under directory META-INF\services Catalog , create a file org.apache.shardingsphere.sharding.spi.ShardingAlgorithm, And add the following to the file :
com.ruoyi.framework.config.TableShardingAlgorithmCustom policy , Need to pass through spi Mechanism to configure .
10. Add annotations to the implementation file of the service
Through annotation @DataSource(DataSourceType.SHARDING), The framework can determine that the corresponding operations need to use the database connection of the split table .
package com.ruoyi.system.service.impl;
import java.util.List;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysOrderMapper;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.common.core.text.Convert;
/**
* Order information Service The business layer deals with
*
* @author ruoyi
* @date 2022-06-27
*/
@Service
public class SysOrderServiceImpl implements ISysOrderService
{
@Autowired
private SysOrderMapper sysOrderMapper;
/**
* Query order information
*
* @param orderId Order info PK
* @return Order information
*/
@Override
@DataSource(DataSourceType.SHARDING)
public SysOrder selectSysOrderByOrderId(Long orderId)
{
return sysOrderMapper.selectSysOrderByOrderId(orderId);
}
/**
* Query the order information list
*
* @param sysOrder Order information
* @return Order information
*/
@Override
@DataSource(DataSourceType.SHARDING)
public List<SysOrder> selectSysOrderList(SysOrder sysOrder)
{
return sysOrderMapper.selectSysOrderList(sysOrder);
}
/**
* Add order information
*
* @param sysOrder Order information
* @return result
*/
@Override
@DataSource(DataSourceType.SHARDING)
public int insertSysOrder(SysOrder sysOrder)
{
return sysOrderMapper.insertSysOrder(sysOrder);
}
/**
* Modify order information
*
* @param sysOrder Order information
* @return result
*/
@Override
@DataSource(DataSourceType.SHARDING)
public int updateSysOrder(SysOrder sysOrder)
{
return sysOrderMapper.updateSysOrder(sysOrder);
}
/**
* Batch delete order information
*
* @param orderIds Order information PK to be deleted
* @return result
*/
@Override
@DataSource(DataSourceType.SHARDING)
public int deleteSysOrderByOrderIds(String orderIds)
{
return sysOrderMapper.deleteSysOrderByOrderIds(Convert.toStrArray(orderIds));
}
/**
* Delete order information
*
* @param orderId Order info PK
* @return result
*/
@Override
@DataSource(DataSourceType.SHARDING)
public int deleteSysOrderByOrderId(Long orderId)
{
return sysOrderMapper.deleteSysOrderByOrderId(orderId);
}
}
11. Perform the test
Through page , You can add and delete data , In the database table , You can see that the data is saved in different tables .

surface sys_order_0 Data in :

surface sys_order_1 Data in :

It can be seen that the data with odd mantissa has arrived sys_order_1, Data with even mantissa has arrived sys_order_0.
边栏推荐
- EasyDSS部署在C盘,录像回看无法正常播放该如何解决?
- 西门子S7-200SMART控制步进电机的具体方法及示例程序
- nuc980 已成功启动
- 【各种**问题系列】OLTP和OLAP是啥?
- csdn涨薪秘籍之腾讯自动化软件测试面试题(含答案)
- Creating postgre enterprise database by ArcGIS
- 斐波那锲数列与冒泡排序法在C语言中的用法
- Evaluation of IP location query interface Ⅱ
- Multi thread communication between client and server (primary version)
- Bs-gx-018 student examination system based on SSM
猜你喜欢

适合小白的树莓派opencv4.0安装

如何识别出轮廓准确的长和宽

(JS) array de duplication

ModbusTCP协议网络学习型单路红外模块(双层板)

专访 SUSS NiFT 负责人:Web3 的未来离不开“人人为我,我为人人”的治理

Interview questions of Tencent automation software test of CSDN salary increase secret script (including answers)

中科方德技术专家直播:如何基于 OpenStack、Ceph 构建私有云平台? | 第 27 期

云原生开发必备:首个通用无代码开发平台 iVX 编辑器

Easydss is deployed on Disk C, and the video playback cannot be played normally. How to solve this problem?
![[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code](/img/76/bcf0118c8eea2b45b47eda4a68d3fd.png)
[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
随机推荐
How to obtain method parameter values through WinDbg
Uboot for embedded driver development -- common command parameters in uboot
Creating postgre enterprise database by ArcGIS
彻头彻尾理解JVM系列之七:对象在分代模型中的流转过程是怎样的?
map合并相同的键,值合并为列表
Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)
非凸联合创始人李佐凡:将量化作为自己的终身事业
misc3~7
中科方德技术专家直播:如何基于 OpenStack、Ceph 构建私有云平台? | 第 27 期
涂鸦云开发 demo 登录
Pipeline aggregations管道聚合- parent-2
[daily 3 questions (3)] reformat the phone number
Interview questions of Tencent automation software test of CSDN salary increase secret script (including answers)
(JS) array flat
ModbusTCP协议网络学习型单路红外模块(中壳版)
Ikvm Net project progress
毕业季·进击的技术er - 职场打工人
又拍云 Redis 的改进之路
在线文本过滤小于指定长度工具
crypto 1~5