Fabcar 案例部署
本教程基于 Fabric-1.4.1
进行部署,部署环境是Centos7
官方文档:点击这里
准备好一个环境初始化好的虚拟机(教程地址)
原理
利用fabric-node-sdk程序来与Fabric网络进行交互
准备工作
移除以往运行过的链码镜像和所有的容器:
# 查看镜像
docker images
# 查看容器
docker ps -a
# 停止并移除所有容器
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)
# 移除指定的镜像
docker rmi 镜像ID
安装node和npm
# 创建目录并进入目录
mkdir -p /home/scripts && cd /home/scripts
# 下载node
wget https://nodejs.org/download/release/v8.9.4/node-v8.9.4-linux-x64.tar.gz
# 解压node
tar -zxvf node-v8.9.4-linux-x64.tar.gz
# 复制到/opt目录
mv node-v8.9.4-linux-x64 /opt
# 建立软连接
ln -s /opt/node-v8.9.4-linux-x64/bin/node /usr/local/bin/node
ln -s /opt/node-v8.9.4-linux-x64/bin/npm /usr/local/bin/npm
# 查看版本
node -v
npm -v
设置淘宝镜像
npm config set registry http://registry.npm.taobao.org/
npm install -g cnpm --registry=https://registry.npm.taobao.org
升级node和npm
Note: 可选操作
# npm升级到最新版本,-g全局安装
cnpm install npm -g
# 已经升级了
npm -v
# 安装nodejs的版本管理模块n
cnpm install -g n
# 查看版本
n -V
# nodejs版本升级到最新,参数可换:latest/stable/lts/vx.x.x
n latest
# 安装稳定版
n stable
# 安装指定版本
n v7.10.0
# 查看已安装版本
n
# 删除指定版本
n rm 6.4.0
克隆Fabric-sample库
工具下载
# 创建工作目录并进入该目录
mkdir -p /home/scripts && cd /home/scripts
# 下载脚本
curl -sS https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o ./bootstrap.sh
# 赋予脚本执行权限
chmod +x ./bootstrap.sh
# 执行脚本来克隆fabric-samples库
# 镜像我们在前面的课程拉取过,这里跳过特定平台二进制文件的下载(很慢,自行通过浏览器下载,再上传到虚拟机)
./bootstrap.sh 1.4.1 1.4.1 0.4.15 -d -b
# 如果没有拉取fabric镜像的执行下面这条命令
./bootstrap.sh 1.4.1 1.4.1 0.4.15 -b
# 将工具解压到fabric-tools目录(自行上传,使用rz命令或者Filezilla工具均可以上传)
tar -zxvf hyperledger-fabric-linux-amd64-1.4.1.tar.gz -C ./fabric-tools
# 将fabric-tools目录下的bin文件夹复制到fabric-samples目录下
cp -r ./fabric-tools/bin ./fabric-samples/bin
启动网络
# 进入fabcar目录
cd /home/scripts/fabric-samples/fabcar
# 启动网络、创建通道、部署合约
./startFabric.sh javascript
安装依赖
# 进入javascript目录
cd /home/scripts/fabric-samples/fabcar/javascript
# 安装依赖
npm install
注意:如果出现以下错误
npm WARN deprecated mkdirp@0.5.3: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated mkdirp@0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
> pkcs11js@1.0.20 install /home/scripts/fabric-samples/fabcar/javascript/node_modules/pkcs11js
> node-gyp rebuild
make: 进入目录“/home/scripts/fabric-samples/fabcar/javascript/node_modules/pkcs11js/build”
CXX(target) Release/obj.target/pkcs11/src/main.o
make: g++:命令未找到
make: *** [Release/obj.target/pkcs11/src/main.o] 错误 127
make: 离开目录“/home/scripts/fabric-samples/fabcar/javascript/node_modules/pkcs11js/build”
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack at ChildProcess.emit (events.js:311:20)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
gyp ERR! System Linux 3.10.0-862.el7.x86_64
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/scripts/fabric-samples/fabcar/javascript/node_modules/pkcs11js
gyp ERR! node -v v12.16.1
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok
> grpc@1.24.2 install /home/scripts/fabric-samples/fabcar/javascript/node_modules/grpc
> node-pre-gyp install --fallback-to-build --library=static_library
node-pre-gyp WARN Using request for node-pre-gyp https download
[grpc] Success: "/home/scripts/fabric-samples/fabcar/javascript/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-glibc/grpc_node.node" is installed via remote
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN fabcar@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: pkcs11js@1.0.20 (node_modules/pkcs11js):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: pkcs11js@1.0.20 install: `node-gyp rebuild`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
added 507 packages from 1082 contributors in 25.868s
1 package is looking for funding
run `npm fund` for details
安装gcc
# 说明没有安装gcc的环境
yum install -y gcc-c++
操作fabcar
# 登记管理员
node enrollAdmin.js
# 注册用户
node registerUser.js
# 执行查询
node query.js
# 执行交易
node invoke.js
# 再次执行查询
node query.js