当前位置:网站首页>Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
2022-07-07 10:04:00 【Shardingsphere community】
One 、 background
ShardingSphereDataSourceFactory yes Apache ShardingSphere-JDBC The most basic user of the end API, It is used to convert the user's rule configuration object , And become the standard DataSource The implementation of the . besides , It also provides for YAML Configured YamlShardingSphereDataSourceFactory, And for Spring Custom namespace and Spring Boot Starter.
DataSource It's standard Java JDBC Interface , Engineers can use it to further create compliance JDBC The standard Connection、Statement、PreparedStatement、ResultSet And other familiar standard objects . It's completely in line JDBC Interface implementation , For the use of the Engineer Apache ShardingSphere-JDBC With the use of native JDBC There is no difference between , It can also transparently connect various ORM frame .
Two 、 Pain points
Although standard JDBC Interface , It can be completely adapted in the development process , But through ShardingSphere API establish DataSource, But it has changed the original database driven loading mode of engineers . Although only a few changes are needed ( a line ) Start code of , But for those who want to smoothly migrate to ShardingSphere In terms of system , It actually increases the additional development cost ; And for systems that cannot master the source code ( Such as : External mining system ) Come on , Use ShardingSphere There are many difficulties .
all the time ,ShardingSphere All lack of JDBC The implementation of the driver , This is mainly limited by its original design intention . adopt Java Configured ShardingSphere-JDBC Flexibility can be increased to programmable level , but JDBC Of Driver The interface does not provide much space for additional configuration , Only through URL and Properties, Will greatly limit ShardingSphere Configuration flexibility .YAML Although the configuration can be compared with Driver Of URL Better fit , And more readable , But it belongs to the category of static configuration , Compared with the flexibility of dynamic configuration , Is obviously insufficient . therefore ,ShardingSphere-JDBC Adopt a similar strategy of database connection pool , Around JDBC Limitations of standard interfaces , Direct will DataSource Expose to users .
However , Changing a line of code and not changing a line of code is an insurmountable natural barrier , It also became ShardingSphere-JDBC The biggest pain point of ease of use .
3、 ... and 、 chance
With ShardingSphere Another product of —— ShardingSphere-Proxy Gradually mature , Its two important ecological functions —— Mixed deployment and DistSQL emerge as the times require .
ShardingSphere-JDBC Lightweight and high-performance features of , Make it more suitable for application-oriented runtime CRUD operation ;ShardingSphere-Proxy Ease of use and compatibility , Make it more suitable for database management and control DDL operation . The two products are used together , Learn from each other , It has become a more perfect new generation architecture scheme .
Both programming and SQL Expressive DistSQL, There is a perfect balance between flexibility and ease of use . therefore , stay ShardingSphere-JDBC The configuration properties of are greatly reduced in the architecture model , Use JDBC Of URL Connect the Governance Center , And USES the DistSQL Perform configuration operations , It's the best solution .DistSQL The security of , yes Java and YAML The configuration mode of , It naturally supports permission control and SQL Audit and other high-level capabilities , Give Way DBA The operation and maintenance database cluster is more handy .
Four 、 Realization
After the preconditions are met one by one ,ShardingSphere-JDBC 5.1.2 The version follows the trend , Provides JDBC drive . It can be used only through configuration changes , Engineers no longer need to change the code .
Driver class name
org.apache.shardingsphere.driver.ShardingSphereDriver
URL Configuration instructions
With
jdbc:shardingsphere
: The prefixThe configuration file :
xxx.yaml
, The configuration file format is the same as YAML Consistent configurationConfiguration file loading rules :
No prefix means that the configuration file is loaded from the absolute path
classpath: Prefix means loading configuration file from classpath
5、 ... and 、 Use steps
Use native drivers
Class.forName("org.apache.shardingsphere.driver.ShardingSphereDriver");String jdbcUrl = "jdbc:shardingsphere:classpath:config.yaml";String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";try ( Connection conn = DriverManager.getConnection(jdbcUrl); PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, 10); ps.setInt(2, 1000); try (ResultSet rs = preparedStatement.executeQuery()) { while(rs.next()) { // ... } }}
Use database connection pool
String driverClassName = "org.apache.shardingsphere.driver.ShardingSphereDriver";String jdbcUrl = "jdbc:shardingsphere:classpath:config.yaml";// With HikariCP For example HikariDataSource dataSource = new HikariDataSource();dataSource.setDriverClassName(driverClassName);dataSource.setJdbcUrl(jdbcUrl);String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";try ( Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, 10); ps.setInt(2, 1000); try (ResultSet rs = preparedStatement.executeQuery()) { while(rs.next()) { // ... } }}
reference information
- JDBC drive
https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/jdbc-driver/
6、 ... and 、 Conclusion
ShardingSphere-JDBC The emergence of drivers , send ShardingSphere The usability of has been enhanced unprecedentedly . In future planning ,JDBC The drive can be further simplified , Directly in URL Provide the address of the Governance Center in .Apache ShardingSphere It has made great strides towards diversified distributed clusters . A new era has come , Let's experience it ShardingSphere The powerful functions of the new version of !
The above is the whole content of this sharing , If there is a pair of Apache ShardingSphere Any questions or suggestions , Welcome to the GitHub issue The list presents , Or you can go to the Chinese community to exchange and discuss .
GitHub issue:
https://github.com/apache/shardingsphere/issues
Contribution Guide :
https://shardingsphere.apache.org/community/cn/contribute/
The Chinese community :
https://community.sphere-ex.com/
Apache ShardingSphere Official website :
https://shardingsphere.apache.org/
SphereEx Official website :
author
Zhang liang ,SphereEx CEO,Apache ShardingSphere PMC Chair. Mainly responsible for ShardingSphere Architecture evolution and overcoming technical difficulties .
边栏推荐
- 哈夫曼编码压缩文件
- Delete a record in the table in pl/sql by mistake, and the recovery method
- Guys, have you ever encountered the case of losing data when Flink CDC reads mysqlbinlog? Every time the task restarts, there is a probability of losing data
- First issue of JS reverse tutorial
- PostgreSQL reports an error when creating a trigger,
- Hcip first day notes sorting
- Qualifying 3
- AI moves from perception to intelligent cognition
- Pit using BigDecimal
- Parameter sniffing (1/2)
猜你喜欢
Internship log - day07
农牧业未来发展蓝图--垂直农业+人造肉
字节跳动 Kitex 在森马电商场景的落地实践
First issue of JS reverse tutorial
Basic use of JMeter to proficiency (I) creation and testing of the first task thread from installation
Performance optimization record of the company's product "yunzhujia"
基于智慧城市与储住分离数字家居模式垃圾处理方法
[untitled]
ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
Introduction to energy Router: Architecture and functions for energy Internet
随机推荐
Introduction to energy Router: Architecture and functions for energy Internet
【无标题】
Scratch crawler mysql, Django, etc
sql 里面使用中文字符判断有问题,哪位遇到过?比如value<>`无`
Detailed explanation of diffusion model
Luogu p2482 [sdoi2010] zhuguosha
根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
能源路由器入门必读:面向能源互联网的架构和功能
CDZSC_ 2022 winter vacation personal training match level 21 (2)
Deep understanding of UDP, TCP
Huffman encoded compressed file
What development models did you know during the interview? Just read this one
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消
剑指 Offer II 107. 矩阵中的距离
20排位赛3
PostgreSQL创建触发器的时候报错,
Hcip first day notes sorting
Use 3 in data modeling σ Eliminate outliers for data cleaning
CDZSC_ 2022 winter vacation personal training match level 21 (1)
phpcms实现PC网站接入微信Native支付