通过代理访问MySQL

本使用手册仅对节点版本为2.0.0-rc3的有效,如果需要在2.0.0-rc2中使用“通过代理访问MySQL”的访问方式去搭建分布式存储环境。请参考文档分布式存储搭建方法

逻辑架构图

多群组架构是指区块链节点支持启动多个群组,群组间交易处理、数据存储、区块共识相互隔离的。因此群组下的每一个节点对应一个AMDB实例,例如,区块链网络中,有三个节点A,B,C,其中A,B属于群组1,B,C属于群组2。节点A和C分别对应1个数据库实例,B节点对应了2个数据库实例,逻辑架构图如下:

../../_images/logic_archite.png

如上图所示,节点B属于多个群组,不同群组下的同一个节点,对应的AMDB服务和MySQL是分开的,为了区分不同群组下的同一个节点,将A,B,C三个节点,分别用Group1_A(Group1下的A节点,下同),Group1_B,Group2_B,Group2_C表示。下面以上图为例,描述搭建配置过程。

节点搭建

配置AMDB服务之前,需要完成联盟链的搭建和多群组的配置,具体参考如下步骤。

准备依赖

  • 创建文件夹
  1. mkdir -p ~/fisco && cd ~/fisco
  • 获取build_chain脚本
  1. curl -LO https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/`curl -s https://api.github.com/repos/FISCO-BCOS/FISCO-BCOS/releases | grep "\"v2\." | sort -u | tail -n 1 | cut -d \" -f 4`/build_chain.sh && chmod u+x build_chain.sh

生成配置文件

  1. # 生成区块链配置文件ipconf
  2. cat > ipconf << EOF
  3. 127.0.0.1:1 agencyA 1
  4. 127.0.0.1:1 agencyB 1,2
  5. 127.0.0.1:1 agencyC 2
  6. EOF
  7.  
  8. # 查看配置文件
  9. cat ipconf
  10. 127.0.0.1:1 agencyA 1
  11. 127.0.0.1:1 agencyB 1,2
  12. 127.0.0.1:1 agencyC 2

使用build_chain搭建区块链

  1. ### 搭建区块链(请先确认30600~30602,20800~20802,8565~8567端口没有被占用)
  2. bash build_chain.sh -f ipconf -p 30600,20800,8565
  3. ==============================================================
  4. Generating CA key...
  5. ==============================================================
  6. Generating keys ...
  7. Processing IP:127.0.0.1 Total:1 Agency:agencyA Groups:1
  8. Processing IP:127.0.0.1 Total:1 Agency:agencyB Groups:1,2
  9. Processing IP:127.0.0.1 Total:1 Agency:agencyC Groups:2
  10. ==============================================================
  11. Generating configurations...
  12. Processing IP:127.0.0.1 Total:1 Agency:agencyA Groups:1
  13. Processing IP:127.0.0.1 Total:1 Agency:agencyB Groups:1,2
  14. Processing IP:127.0.0.1 Total:1 Agency:agencyC Groups:2
  15. ==============================================================
  16. Group:1 has 2 nodes
  17. Group:2 has 2 nodes

修改节点ini文件

修改node0下的group.1.ini配置

修改~/fisco/nodes/127.0.0.1/node0/conf/group.1.ini文件中[storage]段的内容,设置为如下内容

  1. [storage]
  2. type=external
  3. topic=DB_Group1_A
  4. max_retry=100
修改node1下的group.1.ini配置

修改~/fisco/nodes/127.0.0.1/node1/conf/group.1.ini文件中[storage]段的内容,设置为如下内容

  1. [storage]
  2. type=external
  3. topic=DB_Group1_B
  4. max_retry=100
修改node1下的group.2.ini配置

修改~/fisco/nodes/127.0.0.1/node1/conf/group.2.ini文件中[storage]段的内容,设置为如下内容

  1. [storage]
  2. type=external
  3. topic=DB_Group2_B
  4. max_retry=100
修改node2下的group.2.ini配置

修改~/fisco/nodes/127.0.0.1/node2/conf/group.2.ini文件中[storage]段的内容,设置为如下内容

  1. [storage]
  2. type=external
  3. topic=DB_Group2_C
  4. max_retry=100

准备amdb代理

源码获取

  1. cd ~/fisco;
  2. git clone https://github.com/FISCO-BCOS/AMDB.git

源码编译

  1. cd AMDB;gradle build

编译完成之后,会生成一个dist目录,文件结构如下:

  1. ├── apps
  2. └── AMDB.jar
  3. ├── conf
  4. ├── amdb.properties
  5. ├── applicationContext.xml
  6. ├── contracts
  7. ├── Table.sol
  8. ├── TableTest.sol
  9. ├── db.properties
  10. ├── doc
  11. ├── amop.png
  12. ├── leveldb.png
  13. └── README.md
  14. ├── log4j2.xml
  15. └── mappers
  16. └── data_mapper.xml
  17. ├── lib
  18. ├── log
  19. └── start.sh

配置amdb代理

AMDB与节点连接过程,AMDB是client,节点是server,启动过程是AMDB服务主动连接节点,节点只需要配置AMDB关注的topic即可,关于topic的介绍请参考AMOP,AMDB需要通过证书准入。

证书配置
  1. cp ~/fisco/nodes/127.0.0.1/sdk/* ~/fisco/AMDB/dist/conf/
amdb实例拷贝
  1. cd ~/fisco;
  2. ###dist_Group1_A是节点Group1_A对应的amdb实例
  3. cp AMDB/dist/ dist_Group1_A -R
  4. ###dist_Group1_B是节点Group1_B对应的amdb实例
  5. cp AMDB/dist/ dist_Group1_B -R
  6. ###dist_Group2_B是节点Group2_B对应的amdb实例
  7. cp AMDB/dist/ dist_Group2_B -R
  8. ###dist_Group2_C是节点Group2_C对应的amdb实例
  9. cp AMDB/dist/ dist_Group2_C -R

经过上述步骤,可以看到~/fisco目录的文件结构如下:

  1. drwxrwxr-x 8 fisco fisco 4096 May 7 15:53 AMDB
  2. -rwxrw-r-- 1 fisco fisco 37539 May 7 14:58 build_chain.sh
  3. drwxrwxr-x 5 fisco fisco 4096 May 7 15:58 dist_Group1_A
  4. drwxrwxr-x 5 fisco fisco 4096 May 7 15:58 dist_Group1_B
  5. drwxrwxr-x 5 fisco fisco 4096 May 7 15:59 dist_Group2_B
  6. drwxrwxr-x 5 fisco fisco 4096 May 7 15:59 dist_Group2_C
  7. -rw-rw-r-- 1 fisco fisco 68 May 7 14:59 ipconf
  8. drwxrwxr-x 4 fisco fisco 4096 May 7 15:08 nodes
DB创建
  1. MySQL -uroot -p123456
  2. CREATE DATABASE `bcos_Group1_A`;
  3. CREATE DATABASE `bcos_Group1_B`;
  4. CREATE DATABASE `bcos_Group2_B`;
  5. CREATE DATABASE `bcos_Group2_C`;

配置文件配置

amdb.properties配置AMDB服务需要连接的节点信息,db.properties配置数据库的连接信息。这里假设MySQL的配置信息如下:

  1. |节点|db_ip|db_port|db_username|db_passwd|db_name|
  2. |Group1_A|127.0.0.1|3306|root|123456|bcos_Group1_A|
  3. |Group1_B|127.0.0.1|3306|root|123456|bcos_Group1_B|
  4. |Group2_B|127.0.0.1|3306|root|123456|bcos_Group2_B|
  5. |Group2_C|127.0.0.1|3306|root|123456|bcos_Group2_C|
为Group1的A节点配置amdb代理

将~/fisco/dist_Group1_A/conf/amdb.properties配置为如下内容:

  1. node.ip=127.0.0.1
  2. node.listen_port=20800
  3. node.topic=DB_Group1_A

将~/fisco/dist_Group1_A/conf/db.properties配置为如下内容:

  1. db.ip=127.0.0.1
  2. db.port=3306
  3. db.user=root
  4. db.password=123456
  5. db.database=bcos_Group1_A

将~/fisco/dist_Group1_A/conf/applicationContext.xml修改为如下配置(部分信息)

  1. <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
  2. <property name="allChannelConnections">
  3. <list>
  4. <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
  5. <property name="groupId" value="1" />
  6. <property name="connectionsStr">
  7. <list>
  8. <value>127.0.0.1:20800</value>
  9. </list>
  10. </property>
  11. </bean>
  12. </list>
  13. </property>
  14. </bean>
  15.  
  16. <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
  17. <property name="groupId" value="1" />
  18. <property name="orgID" value="fisco" />
  19. <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
  20. <property name="topics">
  21. <list>
  22. <value>${node.topic}</value>
  23. </list>
  24. </property>
  25. <property name="pushCallback" ref="DBHandler"/>
  26. </bean>
为Group1的B节点配置amdb代理

将~/fisco/dist_Group1_B/conf/amdb.properties配置为如下内容:

  1. node.ip=127.0.0.1
  2. node.listen_port=20801
  3. node.topic=DB_Group1_B

将~/fisco/dist_Group1_B/conf/db.properties配置为如下内容:

  1. db.ip=127.0.0.1
  2. db.port=3306
  3. db.user=root
  4. db.password=123456
  5. db.database=bcos_Group1_B

将~/fisco/dist_Group1_B/conf/applicationContext.xml修改为如下配置(部分信息)

  1. <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
  2. <property name="allChannelConnections">
  3. <list>
  4. <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
  5. <property name="groupId" value="1" />
  6. <property name="connectionsStr">
  7. <list>
  8. <value>127.0.0.1:20801</value>
  9. </list>
  10. </property>
  11. </bean>
  12. </list>
  13. </property>
  14. </bean>
  15.  
  16. <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
  17. <property name="groupId" value="1" />
  18. <property name="orgID" value="fisco" />
  19. <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
  20. <property name="topics">
  21. <list>
  22. <value>${node.topic}</value>
  23. </list>
  24. </property>
  25. <property name="pushCallback" ref="DBHandler"/>
  26. </bean>
为Group2的B节点配置amdb代理

将~/fisco/dist_Group2_B/conf/amdb.properties配置为如下内容:

  1. node.ip=127.0.0.1
  2. node.listen_port=20801
  3. node.topic=DB_Group2_B

将~/fisco/dist_Group2_B/conf/db.properties配置为如下内容:

  1. db.ip=127.0.0.1
  2. db.port=3306
  3. db.user=root
  4. db.password=123456
  5. db.database=bcos_Group2_B

将~/fisco/dist_Group2_B/conf/applicationContext.xml修改为如下配置(部分信息)

  1. <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
  2. <property name="allChannelConnections">
  3. <list>
  4. <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
  5. <property name="groupId" value="1" />
  6. <property name="connectionsStr">
  7. <list>
  8. <value>127.0.0.1:20801</value>
  9. </list>
  10. </property>
  11. </bean>
  12. </list>
  13. </property>
  14. </bean>
  15.  
  16. <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
  17. <property name="groupId" value="2" />
  18. <property name="orgID" value="fisco" />
  19. <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
  20.  
  21. <!-- communication topic configuration of the node -->
  22. <property name="topics">
  23. <list>
  24. <value>${node.topic}</value>
  25. </list>
  26. </property>
  27. <property name="pushCallback" ref="DBHandler"/>
  28. </bean>
为Group2的C节点配置amdb代理

将~/fisco/dist_Group2_C/conf/amdb.properties配置为如下内容:

  1. node.ip=127.0.0.1
  2. node.listen_port=20802
  3. node.topic=DB_Group2_C

将~/fisco/dist_Group2_C/conf/db.properties配置为如下内容:

  1. db.ip=127.0.0.1
  2. db.port=3306
  3. db.user=root
  4. db.password=123456
  5. db.database=bcos_Group2_C

将~/fisco/dist_Group2_C/conf/applicationContext.xml修改为如下配置(部分信息)

  1. <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
  2. <property name="allChannelConnections">
  3. <list>
  4. <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
  5. <property name="groupId" value="1" />
  6. <property name="connectionsStr">
  7. <list>
  8. <value>127.0.0.1:20802</value>
  9. </list>
  10. </property>
  11. </bean>
  12. </list>
  13. </property>
  14. </bean>
  15.  
  16. <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
  17. <property name="groupId" value="2" />
  18. <property name="orgID" value="fisco" />
  19. <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
  20.  
  21. <!-- communication topic configuration of the node -->
  22. <property name="topics">
  23. <list>
  24. <value>${node.topic}</value>
  25. </list>
  26. </property>
  27. <property name="pushCallback" ref="DBHandler"/>
  28. </bean>

启动amdb代理

  1. cd ~/fisco/dist_Group1_A;sh start.sh
  2. cd ~/fisco/dist_Group1_B;sh start.sh
  3. cd ~/fisco/dist_Group2_B;sh start.sh
  4. cd ~/fisco/dist_Group2_C;sh start.sh

启动节点

  1. cd ~/fisco/nodes/127.0.0.1;sh start_all.sh

检查进程

  1. ps -ef|grep org.bcos.amdb.server.Main|grep -v grep
  2. fisco 110734 1 1 17:25 ? 00:00:10 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
  3. fisco 110778 1 1 17:25 ? 00:00:11 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
  4. fisco 110803 1 1 17:25 ? 00:00:10 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
  5. fisco 122676 1 16 17:38 ? 00:00:08 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
  6.  
  7. ps -ef|grep fisco-bcos|grep -v grep
  8. fisco 111061 1 0 17:25 pts/0 00:00:04 /data/home/fisco/nodes/127.0.0.1/node2/../fisco-bcos -c config.ini
  9. fisco 111065 1 0 17:25 pts/0 00:00:04 /data/home/fisco/nodes/127.0.0.1/node0/../fisco-bcos -c config.ini
  10. fisco 122910 1 1 17:38 pts/0 00:00:02 /data/home/fisco/nodes/127.0.0.1/node1/../fisco-bcos -c config.ini

启动成功,会看到有4个java进程,3个fisco-bcos进程。不成功的话请参考日志确认配置是否正确。

检查日志输出

执行下面指令,查看节点node0链接的节点数(其他节点类似)

  1. tail -f nodes/127.0.0.1/node0/log/log* | grep connected

正常情况会看到类似下面的输出,从输出可以看出node0与另外2个节点有连接。

  1. info|2019-05-07 21:47:22.849910| [P2P][Service] heartBeat connected count,size=2
  2. info|2019-05-07 21:47:32.849970| [P2P][Service] heartBeat connected count,size=2
  3. info|2019-05-07 21:47:42.850024| [P2P][Service] heartBeat connected count,size=2

执行下面指令,检查是否在共识

  1. tail -f nodes/127.0.0.1/node0/log/log* | grep +++

正常情况会不停输出++++Generating seal表示共识正常。

  1. info|2019-05-07 21:48:54.942111| [g:1][p:65544][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=6,tx=0,nodeIdx=1,hash=355790f7...
  2. info|2019-05-07 21:48:56.946022| [g:1][p:65544][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=6,tx=0,nodeIdx=1,hash=4ef772bb...
  3. info|2019-05-07 21:48:58.950222| [g:1][p:65544][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=6,tx=0,nodeIdx=1,hash=48341ee5...

使用控制台发送交易

请参考“节点直连MySQL”中的使用控制台发送交易章节。