{"id":1735,"date":"2023-03-26T20:03:07","date_gmt":"2023-03-26T12:03:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1735"},"modified":"2023-04-23T21:31:23","modified_gmt":"2023-04-23T13:31:23","slug":"sharding-jdbc-implementation-for-read-write-separation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/sharding-jdbc-implementation-for-read-write-separation\/","title":{"rendered":"Sharding-JDBC \u5b9e\u73b0\u8bfb\u5199\u5206\u79bb"},"content":{"rendered":"<p>Sharding-JDBC \u662f Apache \u65d7\u4e0b\u7684 ShardingSphere \u4e2d\u7684\u4e00\u6b3e\u4ea7\u54c1\uff0c\u8f7b\u91cf\uff0c\u5f15\u5165 jar \u5373\u53ef\u5b8c\u6210\u8bfb\u5199\u5206\u79bb\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u589e\u5f3a\u7248\u7684 JDBC\uff0c\u73b0\u5728\u88ab\u4f7f\u7528\u7684\u8f83\u591a\u3002<\/p>\n<h2>maven \u4f9d\u8d56\u7684\u5e93<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u5f53\u524d\u6700\u65b0\u7248 sharding-jdbc --&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;org.apache.shardingsphere&lt;\/groupId&gt;\n  &lt;artifactId&gt;sharding-jdbc-core&lt;\/artifactId&gt;\n  &lt;version&gt;4.1.1&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;!-- \u7ed3\u5408\u5b98\u65b9\u6587\u6863\u4f7f\u7528 HikariCP \u6570\u636e\u5e93\u8fde\u63a5\u6c60 --&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;com.zaxxer&lt;\/groupId&gt;\n  &lt;artifactId&gt;HikariCP&lt;\/artifactId&gt;\n  &lt;version&gt;3.4.5&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;!-- MySQL 8.0.21 \u9a71\u52a8 --&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;mysql&lt;\/groupId&gt;\n  &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\n  &lt;version&gt;8.0.21&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>\u83b7\u53d6\u6570\u636e\u6e90\u7684\u5de5\u5177\u7c7b<\/h2>\n<pre><code class=\"language-java\">import com.zaxxer.hikari.HikariDataSource;\n\n\/**\n * \u83b7\u53d6 DataSource \u5de5\u5177\u7c7b\uff0c\u4f7f\u7528\u4e86 Hikari \u6570\u636e\u5e93\u8fde\u63a5\u6c60\n *\/\nimport javax.sql.DataSource;\n\npublic final class DataSourceUtil {\n\n    private static final int PORT = 3306;\n\n    \/**\n     * \u901a\u8fc7 Hikari \u6570\u636e\u5e93\u8fde\u63a5\u6c60\u521b\u5efa DataSource\n     * @param ip\n     * @param username\n     * @param password\n     * @param dataSourceName\n     * @return\n     *\/\n    public static DataSource createDataSource(String ip, String username, String password, String dataSourceName) {\n        HikariDataSource result = new HikariDataSource();\n        result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());\n        result.setJdbcUrl(String.format(&quot;jdbc:mysql:\/\/%s:%s\/%s?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&quot;, ip, PORT, dataSourceName));\n        result.setUsername(username);\n        result.setPassword(password);\n        return result;\n    }\n}<\/code><\/pre>\n<h2>\u6d4b\u8bd5 Sharding-JDBC \u8bfb\u5199\u5206\u79bb<\/h2>\n<p>\u4e3b\u5e93\uff1a172.31.32.101<br \/>\n\u4ece\u5e93\uff1a172.31.32.102<\/p>\n<p>\u89c2\u5bdf\u901a\u8fc7 Sharding-JDBC \u83b7\u53d6\u7684 DataSource \u662f\u5426\u4f1a\u81ea\u52a8\u5199\u5165\u5230\u4e3b\u5e93\uff0c\u4ece\u5e93\u662f\u5426\u4e3b\u52a8\u540c\u6b65\uff0c\u4ece\u5e93\u540c\u6b65\u6570\u636e\u7684\u5ef6\u8fdf\u65f6\u95f4<\/p>\n<h3>\u6d4b\u8bd5\u4ee3\u7801<\/h3>\n<pre><code class=\"language-java\">import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;\nimport org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;\n\nimport javax.sql.DataSource;\nimport java.sql.Connection;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.sql.Statement;\nimport java.time.LocalTime;\nimport java.util.*;\n\n\/**\n * \u6d4b\u8bd5 ShardingSphere \u8bfb\u5199\u5206\u79bb\n * \u4e3b\u5e93\uff1a172.31.32.101\n * \u4ece\u5e93\uff1a172.31.32.102\n *\n * \u89c2\u5bdf\u901a\u8fc7 ShardingSphere \u83b7\u53d6\u7684 DataSource \u662f\u5426\u4f1a\u81ea\u52a8\u5199\u5165\u5230\u4e3b\u5e93\uff0c\u4ece\u5e93\u662f\u5426\u4e3b\u52a8\u540c\u6b65\uff0c\u4ece\u5e93\u540c\u6b65\u6570\u636e\u7684\u5ef6\u8fdf\u65f6\u95f4\n *\/\npublic class Test {\n\n    \/\/\u4e3b\u5e93 DataSource\n    private static DataSource dsSlave = DataSourceUtil.createDataSource(&quot;172.31.32.102&quot;, &quot;root&quot;, &quot;password@appblog.cn&quot;, &quot;appblog&quot;);\n    \/\/\u4ece\u5e93 DataSource\n    private static DataSource dsMaster = DataSourceUtil.createDataSource(&quot;172.31.32.101&quot;, &quot;root&quot;, &quot;password@appblog.cn&quot;, &quot;appblog&quot;);\n\n    public static void main(String[] args) throws SQLException {\n        \/\/\u542f\u52a8\u7ebf\u7a0b\u6253\u5370\u4e3b\u5e93\u4e0e\u4ece\u5e93\u5f53\u524d cuser \u6570\u636e\u91cf\u4e0e\u65f6\u95f4\uff0c\u89c2\u5bdf\u4ece\u5e93\u540c\u6b65\u6570\u636e\u5ef6\u8fdf\n        printMasterAndSlaveData();\n\n        \/\/\u4ece ShardingSphere \u83b7\u53d6 DataSource\uff0c\u63d2\u5165\u6570\u636e\uff0c\u89c2\u5bdf\u63d2\u5165\u6570\u636e\u7684\u5e93\u662f\u5426\u4e3a\u4e3b\u5e93\n        DataSource ds = getMasterSlaveDataSource();\n        Connection conn = ds.getConnection();\n        Statement stt = conn.createStatement();\n        stt.execute(&quot;insert into cuser values(1, &#039;test&#039;)&quot;);\n    }\n\n    \/**\n     * \u542f\u52a8\u7ebf\u7a0b\u6253\u5370\uff0c\u4e24\u4e2a\u4e3b\u4ece\u5e93 cuser \u8868\u7684\u4fe1\u606f\u3001\u6570\u636e\u91cf\u3001\u5f53\u524d\u65f6\u95f4\n     * @throws SQLException\n     *\/\n    private static void printMasterAndSlaveData() throws SQLException {\n        Connection masterConn = dsMaster.getConnection();\n        Connection slaveConn = dsSlave.getConnection();\n        new Thread(() -&gt; {\n            while (true) {\n                try {\n                    System.out.println(&quot;------master------&quot; + LocalTime.now());\n                    print(masterConn);\n                    System.out.println(&quot;------slave------&quot; + LocalTime.now());\n                    print(slaveConn);\n                } catch (SQLException e) {\n                }\n            }\n        }).start();\n    }\n\n    private static void print(Connection conn) throws SQLException {\n        Statement statement = conn.createStatement();\n        statement.execute(&quot;select * from cuser&quot;);\n        ResultSet rs = statement.getResultSet();\n        int count = 0;\n        while (rs.next()) {\n            int id = rs.getInt(&quot;id&quot;);\n            String name = rs.getString(&quot;name&quot;);\n            System.out.println(id + &quot;-&quot; + name);\n            count++;\n        }\n        System.out.println(&quot;total: &quot; + count);\n    }\n\n    \/**\n     * \u8bbe\u7f6e ShardingSphere \u7684\u4e3b\u4ece\u5e93\n     * @return\n     * @throws SQLException\n     *\/\n    private static DataSource getMasterSlaveDataSource() throws SQLException {\n        MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration(&quot;ds_master_slave&quot;, &quot;ds_master&quot;, Arrays.asList(&quot;ds_slave&quot;));\n        return MasterSlaveDataSourceFactory.createDataSource(createDataSourceMap(), masterSlaveRuleConfig, new Properties());\n    }\n\n    \/**\n     * \u7528 \u4e3b\u4ece\u5e93\u7684 DataSource \u6784\u9020 map\n     * @return\n     *\/\n    private static Map&lt;String, DataSource&gt; createDataSourceMap() {\n        Map&lt;String, DataSource&gt; result = new HashMap&lt;&gt;();\n        result.put(&quot;ds_master&quot;, dsMaster);\n        result.put(&quot;ds_slave&quot;, dsSlave);\n        return result;\n    }\n}<\/code><\/pre>\n<h3>\u5206\u6790\u5ef6\u8fdf\u4fe1\u606f<\/h3>\n<p>\u6570\u636e\u9ed8\u8ba4\u914d\u7f6e\u7684\u60c5\u51b5\uff0c\u5728\u5185\u7f51\u4ece\u5e93\u540c\u6b65\u7684\u65f6\u95f4\u5ef6\u8fdf\uff0c\u5728 200ms \u5de6\u53f3\uff0c\u5f53\u7136\u8fd9\u4e2a\u7edf\u8ba1\u662f\u4e0d\u7cbe\u786e\u7684\uff0c\u53ea\u662f\u770b\u4e2a\u5927\u6982\u60c5\u51b5\uff0c\u7406\u8bba\u503c\u5e94\u8be5\u662f\u53ef\u4ee5\u505a\u6beb\u79d2\u7ea7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sharding-JDBC \u662f Apache \u65d7\u4e0b\u7684 ShardingSphere \u4e2d\u7684\u4e00\u6b3e\u4ea7\u54c1\uff0c\u8f7b\u91cf\uff0c\u5f15\u5165  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[436,298],"class_list":["post-1735","post","type-post","status-publish","format-standard","hentry","category-middleware","tag-sharding-jdbc","tag-298"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1735"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1735\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}