当前位置:网站首页>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-06 19:41:00 InfoQ

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 prefix
  • The configuration file :
    xxx.yaml
    , The configuration file format is the same as  YAML  Consistent configuration
  • Configuration 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 :
https://www.sphere-ex.com

author

Zhang liang ,SphereEx CEO,Apache ShardingSphere PMC Chair. Mainly responsible for  ShardingSphere  Architecture evolution and overcoming technical difficulties .
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061128470281.html