当前位置:网站首页>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 .
边栏推荐
- How to become a senior digital IC Design Engineer (1-6) Verilog coding Grammar: Classic Digital IC Design
- [Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
- ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
- Writing file types generated by C language
- 洛谷P2482 [SDOI2010]猪国杀
- The Himalaya web version will pop up after each pause. It is recommended to download the client solution
- CDZSC_2022寒假个人训练赛21级(1)
- Arthas simple instructions
- 高斯消元
- 2020 Zhejiang Provincial Games
猜你喜欢
随机推荐
内存==c语言1
为什么安装mysql时starting service报错?(操作系统-windows)
Applet popup half angle mask layer
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
Pit encountered by vs2015 under win7 (successful)
C# 初始化程序时查看初始化到哪里了示例
Flex flexible layout
Lecture 1: stack containing min function
Writing file types generated by C language
PostgreSQL reports an error when creating a trigger,
Check the example of where the initialization is when C initializes the program
flink. CDC sqlserver. 可以再次写入sqlserver中么 有连接器的 dem
Google Colab装载Google Drive(Google Colab中使用Google Drive)
C# Socke 服务器,客户端,UDP
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
Applet sliding, clicking and switching simple UI
一大波开源小抄来袭
Performance optimization record of the company's product "yunzhujia"
Garbage disposal method based on the separation of smart city and storage and living digital home mode
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消

![[original] what is the core of programmer team management?](/img/11/d4b9929e8aadcaee019f656cb3b9fb.png)






