当前位置:网站首页>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 .
边栏推荐
- 使用BigDecimal的坑
- Google Colab装载Google Drive(Google Colab中使用Google Drive)
- 基础篇:带你从头到尾玩转注解
- Selenium+bs4 parsing +mysql capturing BiliBili Tarot data
- EXT2 file system
- Database multi table Association query problem
- Delete a record in the table in pl/sql by mistake, and the recovery method
- Gym - 102219J Kitchen Plates(暴力或拓扑序列)
- 网上可以开炒股账户吗安全吗
- ORM模型--数据记录的创建操作,查询操作
猜你喜欢

JS reverse tutorial second issue - Ape anthropology first question

“十二星座女神降临”全新活动推出

Switching value signal anti shake FB of PLC signal processing series

The new activity of "the arrival of twelve constellations and goddesses" was launched

Use 3 in data modeling σ Eliminate outliers for data cleaning

ORM模型--关联字段,抽象模型类

AI从感知走向智能认知

Agile course training

农牧业未来发展蓝图--垂直农业+人造肉

Internship log - day04
随机推荐
Internship log - day07
Arcgis操作: 批量修改属性表
企业实战|复杂业务关系下的银行业运维指标体系建设
Lecture 1: stack containing min function
Performance optimization record of the company's product "yunzhujia"
洛谷P2482 [SDOI2010]猪国杀
web3.0系列之分布式存储IPFS
CDZSC_ 2022 winter vacation personal training match level 21 (1)
CDZSC_2022寒假个人训练赛21级(2)
Codeforces - 1324d pair of topics
【原创】程序员团队管理的核心是什么?
Strategic cooperation subquery becomes the secret weapon of Octopus web browser
Lesson 1: hardness of eggs
使用BigDecimal的坑
用flinksql的方式 写进 sr的表,发现需要删除的数据没有删除,参照文档https://do
Sword finger offer II 107 Distance in matrix
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
2020 Zhejiang Provincial Games