通过代理访问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个数据库实例,逻辑架构图如下:
如上图所示,节点B属于多个群组,不同群组下的同一个节点,对应的AMDB服务和MySQL是分开的,为了区分不同群组下的同一个节点,将A,B,C三个节点,分别用Group1_A(Group1下的A节点,下同),Group1_B,Group2_B,Group2_C表示。下面以上图为例,描述搭建配置过程。
节点搭建
配置AMDB服务之前,需要完成联盟链的搭建和多群组的配置,具体参考如下步骤。
准备依赖
- 创建文件夹
- mkdir -p ~/fisco && cd ~/fisco
- 获取
build_chain
脚本
- 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
生成配置文件
- # 生成区块链配置文件ipconf
- cat > ipconf << EOF
- 127.0.0.1:1 agencyA 1
- 127.0.0.1:1 agencyB 1,2
- 127.0.0.1:1 agencyC 2
- EOF
- # 查看配置文件
- cat ipconf
- 127.0.0.1:1 agencyA 1
- 127.0.0.1:1 agencyB 1,2
- 127.0.0.1:1 agencyC 2
使用build_chain搭建区块链
- ### 搭建区块链(请先确认30600~30602,20800~20802,8565~8567端口没有被占用)
- bash build_chain.sh -f ipconf -p 30600,20800,8565
- ==============================================================
- Generating CA key...
- ==============================================================
- Generating keys ...
- Processing IP:127.0.0.1 Total:1 Agency:agencyA Groups:1
- Processing IP:127.0.0.1 Total:1 Agency:agencyB Groups:1,2
- Processing IP:127.0.0.1 Total:1 Agency:agencyC Groups:2
- ==============================================================
- Generating configurations...
- Processing IP:127.0.0.1 Total:1 Agency:agencyA Groups:1
- Processing IP:127.0.0.1 Total:1 Agency:agencyB Groups:1,2
- Processing IP:127.0.0.1 Total:1 Agency:agencyC Groups:2
- ==============================================================
- Group:1 has 2 nodes
- Group:2 has 2 nodes
修改节点ini文件
修改node0下的group.1.ini配置
修改~/fisco/nodes/127.0.0.1/node0/conf/group.1.ini文件中[storage]段的内容,设置为如下内容
- [storage]
- type=external
- topic=DB_Group1_A
- max_retry=100
修改node1下的group.1.ini配置
修改~/fisco/nodes/127.0.0.1/node1/conf/group.1.ini文件中[storage]段的内容,设置为如下内容
- [storage]
- type=external
- topic=DB_Group1_B
- max_retry=100
修改node1下的group.2.ini配置
修改~/fisco/nodes/127.0.0.1/node1/conf/group.2.ini文件中[storage]段的内容,设置为如下内容
- [storage]
- type=external
- topic=DB_Group2_B
- max_retry=100
修改node2下的group.2.ini配置
修改~/fisco/nodes/127.0.0.1/node2/conf/group.2.ini文件中[storage]段的内容,设置为如下内容
- [storage]
- type=external
- topic=DB_Group2_C
- max_retry=100
准备amdb代理
源码获取
- cd ~/fisco;
- git clone https://github.com/FISCO-BCOS/AMDB.git
源码编译
- cd AMDB;gradle build
编译完成之后,会生成一个dist目录,文件结构如下:
- ├── apps
- │ └── AMDB.jar
- ├── conf
- │ ├── amdb.properties
- │ ├── applicationContext.xml
- │ ├── contracts
- │ │ ├── Table.sol
- │ │ ├── TableTest.sol
- │ ├── db.properties
- │ ├── doc
- │ │ ├── amop.png
- │ │ ├── leveldb.png
- │ │ └── README.md
- │ ├── log4j2.xml
- │ └── mappers
- │ └── data_mapper.xml
- ├── lib
- ├── log
- └── start.sh
配置amdb代理
AMDB与节点连接过程,AMDB是client,节点是server,启动过程是AMDB服务主动连接节点,节点只需要配置AMDB关注的topic即可,关于topic的介绍请参考AMOP,AMDB需要通过证书准入。
证书配置
- cp ~/fisco/nodes/127.0.0.1/sdk/* ~/fisco/AMDB/dist/conf/
amdb实例拷贝
- cd ~/fisco;
- ###dist_Group1_A是节点Group1_A对应的amdb实例
- cp AMDB/dist/ dist_Group1_A -R
- ###dist_Group1_B是节点Group1_B对应的amdb实例
- cp AMDB/dist/ dist_Group1_B -R
- ###dist_Group2_B是节点Group2_B对应的amdb实例
- cp AMDB/dist/ dist_Group2_B -R
- ###dist_Group2_C是节点Group2_C对应的amdb实例
- cp AMDB/dist/ dist_Group2_C -R
经过上述步骤,可以看到~/fisco目录的文件结构如下:
- drwxrwxr-x 8 fisco fisco 4096 May 7 15:53 AMDB
- -rwxrw-r-- 1 fisco fisco 37539 May 7 14:58 build_chain.sh
- drwxrwxr-x 5 fisco fisco 4096 May 7 15:58 dist_Group1_A
- drwxrwxr-x 5 fisco fisco 4096 May 7 15:58 dist_Group1_B
- drwxrwxr-x 5 fisco fisco 4096 May 7 15:59 dist_Group2_B
- drwxrwxr-x 5 fisco fisco 4096 May 7 15:59 dist_Group2_C
- -rw-rw-r-- 1 fisco fisco 68 May 7 14:59 ipconf
- drwxrwxr-x 4 fisco fisco 4096 May 7 15:08 nodes
DB创建
- MySQL -uroot -p123456
- CREATE DATABASE `bcos_Group1_A`;
- CREATE DATABASE `bcos_Group1_B`;
- CREATE DATABASE `bcos_Group2_B`;
- CREATE DATABASE `bcos_Group2_C`;
配置文件配置
amdb.properties配置AMDB服务需要连接的节点信息,db.properties配置数据库的连接信息。这里假设MySQL的配置信息如下:
- |节点|db_ip|db_port|db_username|db_passwd|db_name|
- |Group1_A|127.0.0.1|3306|root|123456|bcos_Group1_A|
- |Group1_B|127.0.0.1|3306|root|123456|bcos_Group1_B|
- |Group2_B|127.0.0.1|3306|root|123456|bcos_Group2_B|
- |Group2_C|127.0.0.1|3306|root|123456|bcos_Group2_C|
为Group1的A节点配置amdb代理
将~/fisco/dist_Group1_A/conf/amdb.properties配置为如下内容:
- node.ip=127.0.0.1
- node.listen_port=20800
- node.topic=DB_Group1_A
将~/fisco/dist_Group1_A/conf/db.properties配置为如下内容:
- db.ip=127.0.0.1
- db.port=3306
- db.user=root
- db.password=123456
- db.database=bcos_Group1_A
将~/fisco/dist_Group1_A/conf/applicationContext.xml修改为如下配置(部分信息)
- <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
- <property name="allChannelConnections">
- <list>
- <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
- <property name="groupId" value="1" />
- <property name="connectionsStr">
- <list>
- <value>127.0.0.1:20800</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
- <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
- <property name="groupId" value="1" />
- <property name="orgID" value="fisco" />
- <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
- <property name="topics">
- <list>
- <value>${node.topic}</value>
- </list>
- </property>
- <property name="pushCallback" ref="DBHandler"/>
- </bean>
为Group1的B节点配置amdb代理
将~/fisco/dist_Group1_B/conf/amdb.properties配置为如下内容:
- node.ip=127.0.0.1
- node.listen_port=20801
- node.topic=DB_Group1_B
将~/fisco/dist_Group1_B/conf/db.properties配置为如下内容:
- db.ip=127.0.0.1
- db.port=3306
- db.user=root
- db.password=123456
- db.database=bcos_Group1_B
将~/fisco/dist_Group1_B/conf/applicationContext.xml修改为如下配置(部分信息)
- <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
- <property name="allChannelConnections">
- <list>
- <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
- <property name="groupId" value="1" />
- <property name="connectionsStr">
- <list>
- <value>127.0.0.1:20801</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
- <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
- <property name="groupId" value="1" />
- <property name="orgID" value="fisco" />
- <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
- <property name="topics">
- <list>
- <value>${node.topic}</value>
- </list>
- </property>
- <property name="pushCallback" ref="DBHandler"/>
- </bean>
为Group2的B节点配置amdb代理
将~/fisco/dist_Group2_B/conf/amdb.properties配置为如下内容:
- node.ip=127.0.0.1
- node.listen_port=20801
- node.topic=DB_Group2_B
将~/fisco/dist_Group2_B/conf/db.properties配置为如下内容:
- db.ip=127.0.0.1
- db.port=3306
- db.user=root
- db.password=123456
- db.database=bcos_Group2_B
将~/fisco/dist_Group2_B/conf/applicationContext.xml修改为如下配置(部分信息)
- <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
- <property name="allChannelConnections">
- <list>
- <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
- <property name="groupId" value="1" />
- <property name="connectionsStr">
- <list>
- <value>127.0.0.1:20801</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
- <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
- <property name="groupId" value="2" />
- <property name="orgID" value="fisco" />
- <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
- <!-- communication topic configuration of the node -->
- <property name="topics">
- <list>
- <value>${node.topic}</value>
- </list>
- </property>
- <property name="pushCallback" ref="DBHandler"/>
- </bean>
为Group2的C节点配置amdb代理
将~/fisco/dist_Group2_C/conf/amdb.properties配置为如下内容:
- node.ip=127.0.0.1
- node.listen_port=20802
- node.topic=DB_Group2_C
将~/fisco/dist_Group2_C/conf/db.properties配置为如下内容:
- db.ip=127.0.0.1
- db.port=3306
- db.user=root
- db.password=123456
- db.database=bcos_Group2_C
将~/fisco/dist_Group2_C/conf/applicationContext.xml修改为如下配置(部分信息)
- <bean id="groupChannelConnectionsConfig" class="org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig">
- <property name="allChannelConnections">
- <list>
- <bean id="group1" class="org.fisco.bcos.channel.handler.ChannelConnections">
- <property name="groupId" value="1" />
- <property name="connectionsStr">
- <list>
- <value>127.0.0.1:20802</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
- <bean id="DBChannelService" class="org.fisco.bcos.channel.client.Service">
- <property name="groupId" value="2" />
- <property name="orgID" value="fisco" />
- <property name="allChannelConnections" ref="groupChannelConnectionsConfig"></property>
- <!-- communication topic configuration of the node -->
- <property name="topics">
- <list>
- <value>${node.topic}</value>
- </list>
- </property>
- <property name="pushCallback" ref="DBHandler"/>
- </bean>
启动amdb代理
- cd ~/fisco/dist_Group1_A;sh start.sh
- cd ~/fisco/dist_Group1_B;sh start.sh
- cd ~/fisco/dist_Group2_B;sh start.sh
- cd ~/fisco/dist_Group2_C;sh start.sh
启动节点
- cd ~/fisco/nodes/127.0.0.1;sh start_all.sh
检查进程
- ps -ef|grep org.bcos.amdb.server.Main|grep -v grep
- fisco 110734 1 1 17:25 ? 00:00:10 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
- fisco 110778 1 1 17:25 ? 00:00:11 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
- fisco 110803 1 1 17:25 ? 00:00:10 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
- fisco 122676 1 16 17:38 ? 00:00:08 java -cp conf/:apps/*:lib/* org.bcos.amdb.server.Main
- ps -ef|grep fisco-bcos|grep -v grep
- 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
- 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
- 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链接的节点数(其他节点类似)
- tail -f nodes/127.0.0.1/node0/log/log* | grep connected
正常情况会看到类似下面的输出,从输出可以看出node0与另外2个节点有连接。
- info|2019-05-07 21:47:22.849910| [P2P][Service] heartBeat connected count,size=2
- info|2019-05-07 21:47:32.849970| [P2P][Service] heartBeat connected count,size=2
- info|2019-05-07 21:47:42.850024| [P2P][Service] heartBeat connected count,size=2
执行下面指令,检查是否在共识
- tail -f nodes/127.0.0.1/node0/log/log* | grep +++
正常情况会不停输出++++Generating seal表示共识正常。
- 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...
- 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...
- 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”中的使用控制台发送交易章节。