ShardingSphere Proxy 主從讀寫 入門使用


ShardingSphere Proxy 主從讀寫 入門使用


基於ShardingSphere5.0.0Alpha

環境准備

MySQL搭建

    准備MySQL

配置ShardingSphere-Proxy

    這里使用5.0.0版本的

  • 1.下載ShardingSphere-Proxy

  • 2.下載完成后解壓放到自己的目錄下,比如我這里是放到:D:/temp/ssp

  • 3.下載MySQL-connect.jar

  • 4.將MySQL-connect.jar,放到ShardingSphere-Proxy根目錄的lib下,比如我這里是:D:/temp/ssp/lib/

  • 5.寫入配置文件,里面本身就有配置文件,這里需要寫入的有兩份,如果之前存在的就直接替換:server.xml和config-master_slave.yaml

    server.xml 這份感覺不用改,如果和下面的一樣就不用改

######################################################################################################
#
# If you want to configure orchestration, authorization and proxy properties, please refer to this file.
#
######################################################################################################

# orchestration:
#  name: orchestration_ds
#  overwrite: true
#  registry:
#    type: zookeeper
#    serverLists: localhost:2181
#    namespace: orchestration

authentication:
  users:
    root:
      password: root
    sharding:
      password: sharding
      authorizedSchemas: test

props:
  max.connections.size.per.query: 1
  acceptor.size: 16  # The default value is available processors count * 2.
  executor.size: 16  # Infinite by default.
  proxy.frontend.flush.threshold: 128  # The default value is 128.
    # LOCAL: Proxy will run with LOCAL transaction.
    # XA: Proxy will run with XA transaction.
  # BASE: Proxy will run with B.A.S.E transaction.
  proxy.transaction.type: LOCAL
  proxy.opentracing.enabled: false
  query.with.cipher.column: true
  sql.show: false

    config-master_slave.yaml,這個文件的配置關鍵字一定不要安裝官方example的那個來,完全不行,要按照官方文檔來,比如JDBCURL需要改成url,大致的文件內容如下:

######################################################################################################
# 
# Here you can configure the rules for the proxy.
# This example is configuration of master-slave rule.
#   
# If you want to use master-slave, please refer to this file; 
# if you want to use sharding, please refer to the config-sharding.yaml.
# 
######################################################################################################

schemaName: test

dataSources:
  master_ds:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  slave_ds_0:
    url: jdbc:mysql://localhost:3309/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  slave_ds_1:
    url: jdbc:mysql://localhost:3310/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50


rules:
- !REPLICA_QUERY
  dataSources:
    master_ds:
      primaryDataSourceName: master_ds
      replicaDataSourceNames:
        - slave_ds_0
        - slave_ds_1

運行

    經過前面的步驟基本配置差不多了,現在只需要運行Sharding-Proxy根目錄下的 bin/start.bat就行了,運行命令大致如下:

# 進入相應的目錄下,指定在13306端口運行,不指定默認運行在3307端口
.\start.bat 13306

    這里需要注意的是,Java11運行好像有點問題,需要用1.8來運行,而不確定Java運行版本的,可以直接寫個腳本:start.bat(start.sh類似),整個腳本修改大致如下:

@rem
@rem Licensed to the Apache Software Foundation (ASF) under one or more
@rem contributor license agreements.  See the NOTICE file distributed with
@rem this work for additional information regarding copyright ownership.
@rem The ASF licenses this file to You under the Apache License, Version 2.0
@rem (the "License"); you may not use this file except in compliance with
@rem the License.  You may obtain a copy of the License at
@rem
@rem     http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@echo off & setlocal enabledelayedexpansion

cd %~dp0

set SERVER_NAME=ShardingSphere-Proxy

set CLASS_PATH="..;..\lib\*;..\ext-lib\*"

set PORT=%1

set CONFIG=%2

if "%PORT%"=="-h" (
    goto print_usage
)
if "%PORT%"=="--help" (
    goto print_usage
)

if "%PORT%"=="" (
set MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap
) else ( if "%CONFIG%"=="" (
    set MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap %PORT%
    echo The port is configured as %PORT%
    set CLASS_PATH=../conf;%CLASS_PATH%
    ) else (
    set MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap %PORT% %CONFIG%
    echo The port is configured as %PORT%
    echo The configuration path is %CONFIG%
    set CLASS_PATH=../%CONFIG%;%CLASS_PATH%
    )
    echo The classpath is %CLASS_PATH%
)

echo Starting the %SERVER_NAME% ...

# 在這里就直接設置死了Java的運行版本,如果不能正常運行,請去掉這行
F:\Software\Java\jdk1.8.0_261\bin\java.exe -server -Xmx2g -Xms2g -Xmn1g -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -Dfile.encoding=UTF-8 -classpath %CLASS_PATH% %MAIN_CLASS%

goto exit

:print_usage
 echo "usage: start.bat [port] [config_dir]"
 echo "  port: proxy listen port, default is 3307"
 echo "  config_dir: proxy config directory, default is conf"
 pause

:exit
 pause

    運行正常的話,就可以看到刷刷刷的一排正常日志打出,這樣就OK了

ShardingSphere Proxy連接測試

    使用docker mysql命令有問題,成功連接了但是有bug,但使用MySQL workbench連接和程序連接是沒有問題的

MySQL Workbench鏈接

    使用圖形化界面MySQL Workbench連接:本地ip、13306端口、root、root,連接成功,查詢和刪除,相應數據變化正確

Java JDBC連接測試

    下面是整個測試代碼,可直接運行,運行下來也沒有問題:

package com.example.demo.shardingsphere.proxy.jdbc;

import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * ShardingSphere Proxy Raw JDBC測試
 */
public class ShardingSphereProxyRawJdbcTest {

    @Test @Transactional
    public void test() throws SQLException {
        System.out.println("Start test");
        DataSource SSDataSource = createShardingSphereProxyDataSource();
        DataSource slaveDataSource = createSlaveDataSource();

        // 測試通過SSP是否數據修改和查詢正常
        Connection ssCon = SSDataSource.getConnection();
        Statement ssState = ssCon.createStatement();
        ssState.execute("UPDATE `users` SET `money` = '200' WHERE (`id` = '1');");

        ResultSet ret = ssState.executeQuery("select * from test.users where id=1;");
        while (ret.next()) {
            System.out.println(ret.getLong("money"));
            assert ret.getLong("money") == 200;
        }

        // 測試從庫是否數據同步更改
        Connection slaveCon = slaveDataSource.getConnection();
        Statement slaveState = slaveCon.createStatement();
        ResultSet ret2 = slaveState.executeQuery("select * from test.users where id=1;");
        while (ret2.next()) {
            System.out.println(ret2.getLong("money"));
            assert ret2.getLong("money") == 200;
        }

        ret2.close();
        ret.close();
        ssState.close();
        slaveState.close();
        ssCon.close();
        slaveCon.close();
    }

    private DataSource createSlaveDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3309/test?serverTimezone=UTC&useUnicode=true&characterEncoding" +
                "=utf-8&useSSL=false&allowPublicKeyRetrieval=true");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }

    private DataSource createShardingSphereProxyDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:13306/test?serverTimezone=UTC&useUnicode=true&characterEncoding" +
                "=utf-8&useSSL=false&allowPublicKeyRetrieval=true");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }
}

參考鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM