当前位置:网站首页>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.
边栏推荐
- When the "Ai x scientific computing" is in progress, Huawei's mindspore competition question is hot, waiting for you!
- rxjs Observable 设计原理背后的 Pull 和 Push 思路
- Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
- MySQL query table field information
- 5.移植uboot-设置默认环境变量,裁剪,并分区
- 高效远程办公的基石:有效沟通 |社区征文
- (JS) status mode
- Spark - Task 与 Partition 一一对应与参数详解
- Graduation season · advanced technology Er - workers in the workplace
- crypto 1~5
猜你喜欢

斐波那锲数列与冒泡排序法在C语言中的用法

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

Hit the industry directly! The first model selection tool in the industry was launched by the flying propeller

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

Live broadcast by technical experts from China Kuwait Fangde: how to build a private cloud platform based on openstack and CEPH| Issue 27

【每日3题(3)】重新格式化电话号码
![[daily 3 questions (3)] reformat the phone number](/img/ba/0cfe8c084e626615934065b4dee453.png)
[daily 3 questions (3)] reformat the phone number

Data analysis method and Thinking: funnel analysis

什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手

如何通过WinDbg获取方法参数值
随机推荐
Thoroughly understand JVM Series 7: what is the flow process of objects in the generational model?
极限导论总结
Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
Encore une fois, le chemin de l'amélioration de redis Cloud
nuc980 已成功启动
Doodle cloud development demo login
Limit introduction summary
Easydss is deployed on Disk C, and the video playback cannot be played normally. How to solve this problem?
软件工程导论——第五章——总体设计
【每日3题(1)】判断国际象棋棋盘中一个格子的颜色
涂鸦云开发 demo 登录
How to properly manage temporary files when writing shell scripts
斐波那锲数列与冒泡排序法在C语言中的用法
ModbusTCP协议WIFI无线学习型单路红外模块(小壳版)
The former security director of Uber faced fraud allegations and concealed the data leakage event
重建中国科研自信——2022最新自然指数排行榜(Nature Index 2022 )公布,中国的研究产出增幅最大...
掌握一些shell 通配符巧妙的运用,会让我们写脚本事半功倍
9 款好用到爆的 JSON 处理工具,极大提高效率!
Modbustcp protocol network learning single channel infrared module (medium shell version)
嵌入式驱动开发之uboot---uboot 中的常见命令参数参数