当前位置:网站首页>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 .
边栏推荐
- CDZSC_ 2022 winter vacation personal training match level 21 (2)
- La différence entre viewpager 2 et viewpager et la mise en œuvre de la rotation viewpager 2
- Basic chapter: take you through notes
- Parameter sniffing (1/2)
- Internship log - day04
- Three years after graduation
- 2016 CCPC Hangzhou Onsite
- PostgreSQL创建触发器的时候报错,
- 网上可以开炒股账户吗安全吗
- MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
猜你喜欢

Internship log - day07

Flex flexible layout
![[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development](/img/21/6a183e4e10daed90c66235bdbdc3bf.png)
[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development

Performance optimization record of the company's product "yunzhujia"

AI moves from perception to intelligent cognition

基础篇:带你从头到尾玩转注解

Diffusion模型详解

Delete a record in the table in pl/sql by mistake, and the recovery method

20排位赛3

Bean 作⽤域和⽣命周期
随机推荐
“十二星座女神降临”全新活动推出
根据热门面试题分析Android事件分发机制(一)
La différence entre viewpager 2 et viewpager et la mise en œuvre de la rotation viewpager 2
Codeforces - 1324d pair of topics
Software modeling and analysis
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
C# 初始化程序时查看初始化到哪里了示例
Parameter sniffing (1/2)
20排位赛3
基础篇:带你从头到尾玩转注解
Performance optimization record of the company's product "yunzhujia"
小程序弹出半角遮罩层
C# Socke 服务器,客户端,UDP
中国首款电音音频类“山野电音”数藏发售来了!
Flex flexible layout
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
The industrial chain of consumer Internet is actually very short. It only undertakes the role of docking and matchmaking between upstream and downstream platforms
2020ccpc Weihai J - Steins; Game (SG function, linear basis)
ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
企业实战|复杂业务关系下的银行业运维指标体系建设