区块链 fisco bcos 2.72 在多机器上部署多个docker节点 ubuntu
Posted 软件工程小施同学
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区块链 fisco bcos 2.72 在多机器上部署多个docker节点 ubuntu相关的知识,希望对你有一定的参考价值。
一、安装
1. 安装依赖
安装curl、openssl:
sudo apt-get update
sudo apt install -y curl openssl
2. 安装docker:
(1)安装docker
# 更新包索引
sudo apt-get update
# 安装基础依赖库
sudo apt-get install \\
apt-transport-https \\
ca-certificates \\
curl \\
gnupg-agent \\
software-properties-common
# 添加Docker官方GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 添加docker仓库
sudo add-apt-repository \\
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \\
$(lsb_release -cs) \\
stable"
# 更新包索引
sudo apt-get update
# 安装Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
(2)加入docker用户组
sudo groupadd docker
sudo usermod -aG docker $USER
(3)安装Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
3. 下载安装脚本
## 创建操作目录
cd ~ && mkdir -p fisco && cd fisco
## 下载脚本
curl -#LO https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/v2.7.2/build_chain.sh && chmod u+x build_chain.sh
注解
如果因为网络问题导致长时间无法下载build_chain.sh脚本:
- 多试几次
- 也可以换成 curl -#LO https://gitee.com/FISCO-BCOS/FISCO-BCOS/raw/master/tools/build_chain.sh && chmod u+x build_chain.sh
4. 搭建单群组4节点区块链
(1)查看ip
每个机器一个节点
machine name | ip |
fiscobcos_docker1 | 192.168.64.137 |
fiscobcos_docker2 | 192.168.64.138 |
fiscobcos_docker3 | 192.168.64.139 |
fiscobcos_docker4 | 192.168.64.140 |
(2)生成区块链所有节点的配置
以下所有都在fiscobcos_docker1(192.168.64.137)机器操作
将节点信息写入到ipconf文件中
# 这里所有区块链节点均属于agencyA,并仅启动了群组1
# (注: 下面的ip信息需要根据真实的机器IP填写)
cat >> ipconf << EOF
192.168.64.137 agencyA 1
192.168.64.138 agencyA 1
192.168.64.139 agencyA 1
192.168.64.140 agencyA 1
EOF
(3)基于配置文件生成区块链节点配置
在fisco目录下执行下面的指令,生成一条单群组4节点的区块链。
请确保机器的30300,20200,8545
端口没有被占用,也可以通过-p
参数指定其他端口。
bash build_chain.sh -d -f ipconf -p 30300,20200,8545
- -d表示以docker方式启动
- -f表示读取ipconf配置文件
- -p表示指定端口号
命令执行成功会输出All completed
。
如果执行出错,请检查nodes/build.log
文件中的错误信息。
shijianfeng@ubuntu:~/fisco$ bash build_chain.sh -d -f ipconf -p 30300,20200,8545
==============================================================
Generating CA key...
==============================================================
Generating keys and certificates ...
Processing IP=192.168.64.137 Total=1 Agency=agencyA Groups=1
Processing IP=192.168.64.138 Total=1 Agency=agencyA Groups=1
Processing IP=192.168.64.139 Total=1 Agency=agencyA Groups=1
Processing IP=192.168.64.140 Total=1 Agency=agencyA Groups=1
==============================================================
Generating configuration files ...
Processing IP=192.168.64.137 Total=1 Agency=agencyA Groups=1
Processing IP=192.168.64.138 Total=1 Agency=agencyA Groups=1
Processing IP=192.168.64.139 Total=1 Agency=agencyA Groups=1
Processing IP=192.168.64.140 Total=1 Agency=agencyA Groups=1
==============================================================
Group:1 has 4 nodes
==============================================================
[INFO] Docker tag : latest
[INFO] IP List File : ipconf
[INFO] Start Port : 30300 20200 8545
[INFO] Server IP : 192.168.64.137 192.168.64.138 192.168.64.139 192.168.64.140
[INFO] Output Dir : /home/shijianfeng/fisco/nodes
[INFO] CA Path : /home/shijianfeng/fisco/nodes/cert/
==============================================================
[INFO] Execute the download_console.sh script in directory named by IP to get FISCO-BCOS console.
e.g. bash /home/shijianfeng/fisco/nodes/192.168.64.137/download_console.sh -f
==============================================================
[INFO] All completed. Files in /home/shijianfeng/fisco/nodes
至此,成功生成了多机4节点配置,每台机器的区块链节点配置均位于nodes
文件夹下,如下:
shijianfeng@ubuntu:~/fisco$ ls nodes/
192.168.64.137 192.168.64.138 192.168.64.139 192.168.64.140 cert
(4)拷贝区块链节点配置
生成区块链节点配置后,需要将每个节点配置拷贝到对应机器上,可通过scp
命令执行拷贝,具体如下:
# 为每台机器创建操作目录~/fisco
# (注: 这里使用shijianfeng用户进行操作; 实际操作时,可使用自己的账户进行类似操作, IP也需要替换成自己的机器IP)
ssh shijianfeng@192.168.64.138 "mkdir -p ~/fisco"
ssh shijianfeng@192.168.64.139 "mkdir -p ~/fisco"
ssh shijianfeng@192.168.64.140 "mkdir -p ~/fisco"
# 拷贝节点配置
# 拷贝节点配置到192.168.64.137的~/fisco路径
scp -r nodes/192.168.64.137/ shijianfeng@192.168.64.137:~/fisco/192.168.64.137
# 拷贝节点配置到192.168.64.138的~/fisco路径
scp -r nodes/192.168.64.138/ shijianfeng@192.168.64.138:~/fisco/192.168.64.138
# 拷贝节点配置到192.168.64.139的~/fisco路径
scp -r nodes/192.168.64.139/ shijianfeng@192.168.64.139:~/fisco/192.168.64.139
# 拷贝节点配置到192.168.64.140的~/fisco路径
scp -r nodes/192.168.64.140/ shijianfeng@192.168.64.138:~/fisco/192.168.64.140
如果报错scp ssh 22: connection refused
请看https://blog.csdn.net/u013288190/article/details/115873491
5. 启动区块链节点
区块链节点配置拷贝成功后,需要启动所有节点,可通过某台机器发起ssh
操作远程启动区块链节点,也可登录上所有物理机后,在对应的物理机上启动区块链节点。
本文采用直接登录机器启动区块链节点
执行nodes/node_ip/start_all.sh
# (注: 这里使用fisco用户进行操作; 实际操作时,可使用自己的账户进行类似操作, IP也需要替换成自己的机器IP)
# 登录192.168.64.137并启动区块链节点
$ ssh fisco@192.168.64.137
$ bash ~/fisco/192.168.64.137/start_all.sh
# 登录192.168.64.138并启动区块链节点
$ ssh fisco@192.168.64.138
$ bash ~/fisco/192.168.64.138/start_all.sh
# 登录192.168.64.139并启动区块链节点
$ ssh fisco@192.168.64.139
$ bash ~/fisco/192.168.64.139/start_all.sh
# 登录192.168.64.140并启动区块链节点
$ ssh fisco@192.168.64.140
$ bash ~/fisco/192.168.64.140/start_all.sh
启动时,会查找本地是否存在FISCO-BCOS对应版本的节点镜像,不存在则从docker hub下载。
$ bash nodes/127.0.0.1/start_all.sh
try to start node0
try to start node1
try to start node2
try to start node3
Unable to find image 'fiscoorg/fiscobcos:v2.7.2' locally # 本地镜像不存在
Unable to find image 'fiscoorg/fiscobcos:v2.7.2' locally
Unable to find image 'fiscoorg/fiscobcos:v2.7.2' locally
Unable to find image 'fiscoorg/fiscobcos:v2.7.2' locally
v2.7.2: Pulling from fiscoorg/fiscobcos # 下载镜像
d519e2592276: Pulling fs layer
d22d2dfcfa9c: Pulling fs layer
b3afe92c540b: Pulling fs layer
d0e4ee611225: Waiting
af59874f6897: Waiting
v2.7.2: Pulling from fiscoorg/fiscobcos
d519e2592276: Pulling fs layer
d22d2dfcfa9c: Pulling fs layer
b3afe92c540b: Pulling fs layer
d0e4ee611225: Waiting
af59874f6897: Waiting
v2.7.2: Pulling from fiscoorg/fiscobcos
d519e2592276: Pulling fs layer
Pull complete d519e2592276: Pull complete
Extracting 162B/162B
Pull complete d22d2dfcfa9c: Pull complete
af59874f6897: Extracting 24.48MB/26.71MB
Pull complete b3afe92c540b: Pull complete
d519e2592276: Pull complete
Pull complete d0e4ee611225: Pull complete
b3afe92c540b: Pull complete
Pull complete af59874f6897: Pull complete
af59874f6897: Pull complete
Digest: sha256:7a4e4f60bafe8362b402e0f86245c9a840544845fc57007259d2e4b023da6149
Status: Downloaded newer image for fiscoorg/fiscobcos:v2.7.2
Digest: sha256:7a4e4f60bafe8362b402e0f86245c9a840544845fc57007259d2e4b023da6149
Status: Image is up to date for fiscoorg/fiscobcos:v2.7.2
Digest: sha256:7a4e4f60bafe8362b402e0f86245c9a840544845fc57007259d2e4b023da6149
Status: Image is up to date for fiscoorg/fiscobcos:v2.7.2
Digest: sha256:7a4e4f60bafe8362b402e0f86245c9a840544845fc57007259d2e4b023da6149
Status: Image is up to date for fiscoorg/fiscobcos:v2.7.2
8df0d79bf764cd5693e00bbf5c91e2ed7ec69901049e79049082d7ab0da707c8 # 启动节点的 CONTAINER ID
c27dcaa0adfbf45820506f659913e6dacb8a148425801ea80ab078100a8d57b7 # 启动节点的 CONTAINER ID
e66fe7c68d3a8db5d8c18fe5749eba25fd41350ebbaa7accbc8374e03bfba690 # 启动节点的 CONTAINER ID
2bb7b89e2a46b9a4f92fbf9b48b39023540fccdae14de52e091fa17ea219905e # 启动节点的 CONTAINER ID
node0 start successfully
node2 start successfully
node3 start successfully
node1 start successfully
6. 查看节点
登录每台机器,执行如下命令判断是否启动成功:
(1)检查容器状态是否正常
$ docker ps -a | egrep fiscobcos
8df0d79bf764 fiscoorg/fiscobcos:v2.7.2 "/usr/local/bin/fisc…" 5 minutes ago Up 5 minutes rootfisconodes127.0.0.1node2
2bb7b89e2a46 fiscoorg/fiscobcos:v2.7.2 "/usr/local/bin/fisc…" 5 minutes ago Up 5 minutes rootfisconodes127.0.0.1node1
e66fe7c68d3a fiscoorg/fiscobcos:v2.7.2 "/usr/local/bin/fisc…" 5 minutes ago Up 5 minutes rootfisconodes127.0.0.1node0
c27dcaa0adfb fiscoorg/fiscobcos:v2.7.2 "/usr/local/bin/fisc…" 5 minutes ago Up 5 minutes rootfisconodes127.0.0.1node3
容器状态为UP
时,说明节点正常启动。
(2)检查是否所有节点都连接上
可以通过检查日志来确认节点的p2p连接数目、共识是否正常。
查看节点node0连接的节点数
tail -f ~/fisco/*/node0/log/* |grep -i connected
正常情况会不停地输出连接信息,从输出可以看出node0与另外3个节点有连接。
info|2019-01-21 17:30:58.316769| [P2P][Service] heartBeat,connected count=3
info|2019-01-21 17:31:08.316922| [P2P][Service] heartBeat,connected count=3
info|2019-01-21 17:31:18.317105| [P2P][Service] heartBeat,connected count=3
(3)检查共识是否正常
登录每台机器,进入操作目录,执行如下命令判断节点共识是否正常:
tail -f ~/fisco/*/node0/log/* |grep -i +++
正常情况会不停输出++++Generating seal
,表示共识正常。
info|2020-12-22 17:24:43.729402|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=1,hash=2e133146...
info|2020-12-22 17:24:47.740603|[g:1][CONSENSUS][SEALER]++++++++++++++++ Generating seal on,blkNum=1,tx=0,nodeIdx=1,hash=eb199760...
到此docker环境已经部署完成,可通过控制台对其发起交易。
二、安装控制台、部署合约、发送交易
以下所有都在fiscobcos_docker1(192.168.64.137)机器操作
第一步. 准备依赖
- 安装java (推荐使用java 14).
# ubuntu系统安装java
sudo apt install -y default-jdk
我安装的java sdk 1.8
第二步. 下载并配置控制台
(1)下载控制台
# 创建操作目录
mkdir -p ~/fisco && cd ~/fisco
curl -#LO https://github.com/FISCO-BCOS/console/releases/download/v2.7.1/download_console.sh
# 若因为网络问题导致长时间无法下载,请尝试以下命令:
curl -#LO https://gitee.com/FISCO-BCOS/console/raw/master/tools/download_console.sh
bash download_console.sh
(2)配置控制台: 拷贝SDK证书
这里选择从fiscobcos_docker1(192.168.64.137)拷贝SDK证书,具体操作如下:
# 从192.168.64.137拷贝证书到conf目录下
scp 192.168.64.137:~/fisco/192.168.64.137/sdk/* ~/fisco/console/conf
(3)配置控制台: 修改控制台配置
# 拷贝控制台配置
cp -n ~/fisco/console/conf/config-example.toml ~/fisco/console/conf/config.toml
# 修改控制台连接信息(操作中,控制台连接的IP信息请根据实际情况填写)
sed -i 's/peers=\\["127.0.0.1:20200", "127.0.0.1:20201"\\]/peers=["192.168.64.137:20200", "192.168.64.138:20200", "192.168.64.139:20200", "192.168.64.140:20200"]/g' ~/fisco/console/conf/config.toml
第三步. 启动并使用控制台
启动控制台
bash ~/fisco/console/start.sh
控制台启动成功后,会输出如下信息:
=============================================================================================
Welcome to FISCO BCOS console(2.7.0)!
Type 'help' or 'h' for help. Type 'quit' or 'q' to quit console.
________ ______ ______ ______ ______ _______ ______ ______ ______
| | \\/ \\ / \\ / \\ | \\ / \\ / \\ / \\
| $$$$$$$$\\$$$$$| $$$$$$| $$$$$$| $$$$$$\\ | $$$$$$$| $$$$$$| $$$$$$| $$$$$$\\
| $$__ | $$ | $$___\\$| $$ \\$| $$ | $$ | $$__/ $| $$ \\$| $$ | $| $$___\\$$
| $$ \\ | $$ \\$$ \\| $$ | $$ | $$ | $$ $| $$ | $$ | $$\\$$ \\
| $$$$$ | $$ _\\$$$$$$| $$ __| $$ | $$ | $$$$$$$| $$ __| $$ | $$_\\$$$$$$\\
| $$ _| $$_| \\__| $| $$__/ | $$__/ $$ | $$__/ $| $$__/ | $$__/ $| \\__| $$
| $$ | $$ \\\\$$ $$\\$$ $$\\$$ $$ | $$ $$\\$$ $$\\$$ $$\\$$ $$
\\$$ \\$$$$$$ \\$$$$$$ \\$$$$$$ \\$$$$$$ \\$$$$$$$ \\$$$$$$ \\$$$$$$ \\$$$$$$
=============================================================================================
第四步、使用控制台发送交易
# 获取节点信息
[group:1]> getNodeVersion
ClientVersion{
version='2.6.0',
supportedVersion='2.6.0',
chainId='1',
buildTime='20200819 15:47:59',
buildType='Darwin/appleclang/RelWithDebInfo',
gitBranch='HEAD',
gitCommitHash='e4a5ef2ef64d1943fccc4ebc61467a91779fb1c0'
}
# 得到其它节点
[group:1]> getPeers
[
PeerInfo{
nodeID='6d5e0b06cc540a2bf72f414e77d2c6d533832cbc0143ce9edc0da466c94c7c4738022cfc9a48ab4fa7e371a7205637f77b2dc19b94b913b450d05663ddcf8ed7',
iPAndPort='127.0.0.1:30301',
node='node1',
agency='agency',
topic='[
_block_notify_1
]'
},
PeerInfo{
nodeID='cd8f46dcdd5ec84f53b7869285825ca648bee4db1b1b8247a0ad51357f0a16035a55aa2626b37d9d477a7e57d30e640be0abf991ac92275129111651eae3c37d',
iPAndPort='127.0.0.1:38922',
node='node2',
agency='agency',
topic='[
]'
},
PeerInfo{
nodeID='1cecce7910aa97c12281985f7fb938a51a6681663982fafbeceda61585f539f109770eee0354f4d678c6aece2639764d4a81851fa16532df4e6d5cf679b93d69',
iPAndPort='127.0.0.1:30303',
node='node3',
agency='agency',
topic='[
]'
}
]
# 获取当前块高
[group:1]> getBlockNumber
0
# 部署和调用HelloWorld合约
[group:1]> deploy HelloWorld
transaction hash: 0x0e1f192c97a625471597a3483ec230604ff4bb3d22826d118e606ba64bdb0bf9
contract address: 0x302b771c6e7525dc16f945510c210bae0976ad64
[group:1]> call HelloWorld 0x302b771c6e7525dc16f945510c210bae0976ad64 get
---------------------------------------------------------------------------------------------
Return code: 0
description: transaction executed successfully
Return message: Success
---------------------------------------------------------------------------------------------
Return values:
[
"Hello,World!"
]
---------------------------------------------------------------------------------------------
# 获取当前块高(块高增加1)
[group:1]> getBlockNumber
1
# 修改HelloWorld合约状态变量的值
[group:1]> call HelloWorld 0x302b771c6e7525dc16f945510c210bae0976ad64 set "hello, fisco"
transaction hash: 0xaa318a24b8a049778c7167bc2631eedadaeaba326a6cf17c79772036a0edc239
---------------------------------------------------------------------------------------------
transaction status: 0x0
description: transaction executed successfully
---------------------------------------------------------------------------------------------
Output
Receipt message: Success
Return message: Success
Return value: []
---------------------------------------------------------------------------------------------
Event logs
Event: {}
# 获取HelloWorld合约状态变量的值
[group:1]> call HelloWorld 0x302b771c6e7525dc16f945510c210bae0976ad64 get
---------------------------------------------------------------------------------------------
Return code: 0
description: transaction executed successfully
Return message: Success
---------------------------------------------------------------------------------------------
Return values:
[
"hello,fisco"
]
---------------------------------------------------------------------------------------------
# 获取当前块高
[group:1]> getBlockNumber
2
https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/tutorial/multihost.html
https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/installation.html#id8
以上是关于区块链 fisco bcos 2.72 在多机器上部署多个docker节点 ubuntu的主要内容,如果未能解决你的问题,请参考以下文章