docker deploy sharding-proxy

1. Background

     Sharding-proxy positioned as a transparent proxy client database, database package provides a binary protocol server version for complete support for heterogeneous languages. PostgreSQL currently offers the first version, it can use any compatible PostgreSQL client access protocols (such as: PostgreSQL Command Client, Navicat, etc.) operating data, more friendly for the DBA.

  • Is completely transparent to the application, it can be used directly as PostgreSQL use.
  • It applies to any compatible MySQL / PostgreSQL agreement of the client.

2, installation docker, this step is nothing to speak of

3, making sharding proxy Mirror

    First Production profile: config-sharding.yaml divided sheets used, the end of the text

schemaName: sharding_db

dataSources:
  ds0: 
    url: jdbc:postgresql://127.0.0.1:5432/ds0
    username: postgres
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
  ds1:
    url: jdbc:postgresql://127.0.0.1:5432/ds1
    username: postgres
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65

Shardidagriule:
  tables:
    t_order: 
      actualDataNodes: ds${0..1}.t_order${0..1}
      databaseStrategy:
        inline:
          shardingColumn: user_id
          algorithmExpression: ds${user_id % 2}
      tableStrategy: 
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_id
    t_order_item:
      actualDataNodes: ds${0..1}.t_order_item${0..1}
      databaseStrategy:
        inline:
          shardingColumn: user_id
          algorithmExpression: ds${user_id % 2}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_item${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_item_id
  bindingTables:
    - t_order,t_order_item
  defaultTableStrategy:
    none:
    
  encryptRule:
    encryptors:
      encryptor_aes:
        type: aes
        props:
          aes.key.value: 123456abc
    tables:
      t_order:
        columns:
          order_id:
            plainColumn: order_plain
            cipherColumn: order_cipher
            encryptor: encryptor_aes

 

The above configuration file is divided into three: schemaName, dataSources, shardingRule

schemaName defined sharding proxy database name;

DataSource definition data source can be added a plurality of, each data source;

shardingRule defined fragmentation rules.

By the example that is to be sliced ​​library ds0 and DS1, a logic table for each library fragment and t_order_item t_order two tables, Table fragmentation rules are order_id% 2, so that each bank has four truth table, postgres library built in advance and tables: t_order_0, t_order_1, t_order_item_0, t_order_item_1, libraries fragmentation rule user_id% 2.

 

Production server.yaml files, change file is a common configuration, some of the login information of sharding peoxy

authentication:
  users:
    root:
      password: root
    sharding:
      password: sharding 
      authorizedSchemas: sharding_db
props:
  max.connections.size.per.query: 1
  acceptor.size: 16 
  executor.size: 16 
  proxy.transaction.enabled: false
  proxy.opentracing.enabled: false
  sql.show: true

 

Write dockerfile

FROM sharding-proxy

ADD config-sharding.yaml /opt/sharding-proxy/conf/config-sharding.yaml
ADD server.yaml   /opt/sharding-proxy/conf/server.yaml

 

Mirrored

docker build -t sharding-proxy:v1 .

 

4, making postgresql mirror, and initialize the database

From the above configuration file config-sharding.yaml, pgsql need to build two libraries ds0 ds1, and each library has four real table, you need to advance in postgres built libraries and tables: t_order_0, t_order_1, t_order_item_0, t_order_item_1

Writing shell scripts to initialize the database

#!/bin/bash

dbname=ds0
 
# sql to check whether given database exist
sql1="select count(1) from pg_catalog.pg_database where datname = '$dbname'"
 
# depending on how PATH is set psql may require a fully qualified path
cmd="psql -t -c \"$sql1\""
 
db_exists=`eval $cmd`
 
if [ $db_exists -eq 0 ] ; then
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE DATABASE ds0;
    CREATE DATABASE ds1;
    \c ds0;
      CREATE TABLE t_order_0( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10)); 
      CREATE TABLE t_order_1( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10));
      CREATE TABLE t_order_item_0( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10));
      CREATE TABLE t_order_item_1( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10));   
      \ C DS1;
      CREATE TABLE t_order_0( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10)); 
      CREATE TABLE t_order_1( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10));
      CREATE TABLE t_order_item_0( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10));
      CREATE TABLE t_order_item_1( order_id INT NOT NULL, user_id  INT  NOT NULL, status   CHAR(10));    
EOSQL
be

 

Write dockerfile

FROM 10.1.11.71/onlineshop/postgres:9.4 

ADD init-user-db.sh /docker-entrypoint-initdb.d/init-user-db.sh

 

Mirrored

docker build -t postfres:9.4.1

 

 

5, boot image

docker run -id -d -e POSTGRES_PASSWORD=123456 -p 5432:5432  postgres:9.4.2
docker run -it -d -p 3307:3307 -v /root/sharding-proxy/config-sharding.yaml:/opt/sharding-proxy/conf/config-sharding.yaml sharding-proxy:v1

 

6, connection test

Pgsql connection and view the default library table is created

 

 Connection sharding proxy, nacicat temporarily Rom do not know why, even with pgsql cli is possible

  

psql -U sharding_db  -h postgres -p 3307

 

Insert test data

 

 

In view log data sharding proxy container which is inserted into a specific database which table

 

 

Respectively, to check the data to see ds0 ds1

 

 

 

Direct access to the sharding proxy in

 

Guess you like

Origin www.cnblogs.com/zphqq/p/12504859.html