我 为什么 又在搭环境!!!
成功了成功了(别骂了别骂了
这是最最最最成功的了!!Let’s Begin~~~~~
文章目录
前期准备
1、环境
centos7
git 1.8.3.1
curl
go 1.14.13
docker 18.06.3
docker compose 1.23.2
2、巧用gitee
1、官网 https://gitee.com/
2、点击新建仓库
3、点击导入现有仓库
4、复制github链接
5、返回粘贴
6、 等待一段时间即可,注意填写仓库其他信息
3、使用网站下载github二进制文件
一、下载Fabric源码
在go/src/github.com/hyperledger文件夹下克隆源码(使用gitee)
git clone https://gitee.com/jessyl/fabric.git
查看版本
git branch -a
选择release-2.2
git branch release-2.2
二、获取fabric-samples、二进制文件、配置文件
1、进入scripts文件夹
2、编辑bootstrap.sh文件,加快下载速度,注意版本,后面需要
将fabric-samples下载地址改为gitee的下载地址(方法在前期准备模块)
跳过二进制、配置文件、镜像的下载,后面通过压缩包上传
./bootstrap.sh -db
下载完成会看到一个fabric-samples文件夹,此时在网上下载二进制文件和配置文件
3、查看bootstrap.sh文件要求的版本信息
fabric2.2 的fabric-ca版本: v1.9
https://github.com/hyperledger/fabric-ca/releases
fabric2.2 的fabric二进制版本: v2.2.1
https://github.com/hyperledger/fabric/releases
右键复制下载地址到上面提到的网站进行下载压缩包
4、将两个压缩包传入centos,放到fabric-samples进行解压
tar vxzf 压缩包名称
解压后会自动生成bin文件夹,config文件夹
bin文件夹里面的可执行文件为
5、编辑环境变量,随时随地可以使用这些文件
vim /etc/profile
source /etc/profile
三、获取docker镜像
1、下载docker文件
回到scripts文件夹
./bootstrap.sh -sb
下载超快,如果不快建议换一下源,比如阿里,又看到说163的要快一点??
下载下来的镜像文件使用 docker images
查看
[root@jessyl bin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hyperledger/fabric-tools 2.2.2 811cb2e1c60f 42 hours ago 436MB
hyperledger/fabric-tools amd64-2.2.2-snapshot-3496dfc 811cb2e1c60f 42 hours ago 436MB
<none> <none> 342eca1f24b2 42 hours ago 733MB
hyperledger/fabric-peer 2.2.2 c4977148d940 43 hours ago 55MB
hyperledger/fabric-peer amd64-2.2.2-snapshot-3496dfc c4977148d940 43 hours ago 55MB
<none> <none> 7401552fd0e2 43 hours ago 644MB
hyperledger/fabric-orderer 2.2.2 14b8e1bee522 43 hours ago 38.5MB
hyperledger/fabric-orderer amd64-2.2.2-snapshot-3496dfc 14b8e1bee522 43 hours ago 38.5MB
<none> <none> 9b1dcba8fddc 43 hours ago 589MB
hyperledger/fabric-ccenv 2.2.2 87a2078164b3 43 hours ago 502MB
hyperledger/fabric-ccenv amd64-2.2.2-snapshot-3496dfc 87a2078164b3 43 hours ago 502MB
busybox latest 219ee5171f80 4 days ago 1.23MB
golang 1.14.12-alpine 32d46fc8e22c 3 weeks ago 287MB
golang 1.14.12-alpine3.12 32d46fc8e22c 3 weeks ago 287MB
hyperledger/fabric-nodeenv 2.2 8d164f92ca34 3 weeks ago 294MB
hyperledger/fabric-nodeenv 2.2.1 8d164f92ca34 3 weeks ago 294MB
hyperledger/fabric-nodeenv latest 8d164f92ca34 3 weeks ago 294MB
hyperledger/fabric-javaenv 2.2 f7b2f8cce0e4 2 months ago 407MB
hyperledger/fabric-javaenv 2.2.1 f7b2f8cce0e4 2 months ago 407MB
hyperledger/fabric-javaenv latest f7b2f8cce0e4 2 months ago 407MB
hyperledger/fabric-ca 1.4 dbbc768aec79 2 months ago 158MB
hyperledger/fabric-ca 1.4.9 dbbc768aec79 2 months ago 158MB
hyperledger/fabric-ca latest dbbc768aec79 2 months ago 158MB
hyperledger/fabric-tools 2.2 e9b802fadb41 2 months ago 519MB
hyperledger/fabric-tools 2.2.1 e9b802fadb41 2 months ago 519MB
hyperledger/fabric-tools latest e9b802fadb41 2 months ago 519MB
hyperledger/fabric-peer 2.2 ece149884124 2 months ago 55MB
hyperledger/fabric-peer 2.2.1 ece149884124 2 months ago 55MB
hyperledger/fabric-peer latest ece149884124 2 months ago 55MB
hyperledger/fabric-orderer 2.2 78a16ddd2cf4 2 months ago 38.4MB
hyperledger/fabric-orderer 2.2.1 78a16ddd2cf4 2 months ago 38.4MB
hyperledger/fabric-orderer latest 78a16ddd2cf4 2 months ago 38.4MB
hyperledger/fabric-ccenv 2.2 8e554c280cac 2 months ago 586MB
hyperledger/fabric-ccenv 2.2.1 8e554c280cac 2 months ago 586MB
hyperledger/fabric-ccenv latest 8e554c280cac 2 months ago 586MB
hyperledger/fabric-baseos 2.2 0b99d26b26ad 2 months ago 6.85MB
hyperledger/fabric-baseos 2.2.1 0b99d26b26ad 2 months ago 6.85MB
hyperledger/fabric-baseos latest 0b99d26b26ad 2 months ago 6.85MB
redis 5 226c4c322bdb 2 months ago 98.3MB
hyperledger/fabric-orderer 2.1 2a851d51685b 4 months ago 39.4MB
hyperledger/fabric-orderer 2.1.1 2a851d51685b 4 months ago 39.4MB
hyperledger/fabric-orderer amd64-2.1.1-snapshot-74b96b3 2a851d51685b 4 months ago 39.4MB
<none> <none> 83de9f66a3f3 4 months ago 780MB
hyperledger/fabric-ccenv 2.1 4a6294d34624 4 months ago 554MB
hyperledger/fabric-ccenv 2.1.1 4a6294d34624 4 months ago 554MB
hyperledger/fabric-ccenv amd64-2.1.1-snapshot-74b96b3 4a6294d34624 4 months ago 554MB
hyperledger/fabric-baseos 2.2.2 0445d392507b 4 months ago 6.85MB
hyperledger/fabric-baseos 2.3 0445d392507b 4 months ago 6.85MB
hyperledger/fabric-baseos 2.3.0 0445d392507b 4 months ago 6.85MB
hyperledger/fabric-baseos amd64-2.2.2-snapshot-3496dfc 0445d392507b 4 months ago 6.85MB
hyperledger/fabric-baseos amd64-2.3.0-snapshot-90bd2a0 0445d392507b 4 months ago 6.85MB
hyperledger/fabric-baseos amd64-2.3.0-snapshot-cddfe91 0445d392507b 4 months ago 6.85MB
hyperledger/fabric-tools 2.2.0 5eb2356665e7 5 months ago 519MB
hyperledger/fabric-peer 2.2.0 760f304a3282 5 months ago 54.9MB
hyperledger/fabric-orderer 2.2.0 5fb8e97da88d 5 months ago 38.4MB
hyperledger/fabric-ccenv 2.2.0 aac435a5d3f1 5 months ago 586MB
hyperledger/fabric-baseos 2.2.0 aa2bdf8013af 5 months ago 6.85MB
hyperledger/fabric-nodeenv 2.2.0 ab88fe4d29dd 5 months ago 293MB
hyperledger/fabric-javaenv 2.2.0 56c30f316b23 5 months ago 504MB
couchdb 3.1 7962d7fc8ed7 6 months ago 189MB
golang 1.14.4-alpine3.12 3289bf11c284 6 months ago 370MB
hyperledger/fabric-tools 2.1 02b16ec21f30 6 months ago 522MB
hyperledger/fabric-tools 2.1.1 02b16ec21f30 6 months ago 522MB
hyperledger/fabric-peer 2.1 89cd71f28010 6 months ago 56.5MB
hyperledger/fabric-peer 2.1.1 89cd71f28010 6 months ago 56.5MB
hyperledger/fabric-baseos 2.1 d67ba930d449 6 months ago 6.88MB
hyperledger/fabric-baseos 2.1.1 d67ba930d449 6 months ago 6.88MB
alpine 3.12 a24bb4013296 6 months ago 5.57MB
hyperledger/fabric-javaenv 2.1 b22ec67c6494 6 months ago 505MB
hyperledger/fabric-javaenv 2.1.1 b22ec67c6494 6 months ago 505MB
hyperledger/fabric-ca 1.4.7 743a758fae29 6 months ago 154MB
alpine 3.11 f70734b6a266 7 months ago 5.61MB
hyperledger/fabric-nodeenv 2.1 9f6ae331087d 7 months ago 292MB
hyperledger/fabric-nodeenv 2.1.1 9f6ae331087d 7 months ago 292MB
golang 1.14.1-alpine3.11 760fdda71c8f 8 months ago 370MB
hyperledger/fabric-zookeeper 0.4 21e55e9a2862 9 months ago 276MB
hyperledger/fabric-zookeeper 0.4.20 21e55e9a2862 9 months ago 276MB
hyperledger/fabric-kafka 0.4 28a93b376dbe 9 months ago 270MB
hyperledger/fabric-kafka 0.4.20 28a93b376dbe 9 months ago 270MB
hyperledger/fabric-couchdb 0.4 03ac1654afc5 9 months ago 261MB
hyperledger/fabric-couchdb 0.4.20 03ac1654afc5 9 months ago 261MB
hello-world latest fce289e99eb9 23 months ago 1.84kB
2、对所有镜像文件进行备份到压缩包
#备份所有docker镜像
docker save $(docker images | grep -v REPOSITORY |grep -v "<none>" | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}') -o allDockerImages.tar
#恢复所有docker镜像
docker load -i allDockerImages.tar
#备份单个docker镜像
docker save busybox:latest -o busybox.tar
#恢复单个docker镜像
docker load -i busybox.tar
四、开启测试网络
1、进入如下路径的test-network案例中,启动测试网络
./network.sh up
2、启动成功
3、查看目前运行的docker镜像
4、关闭网络
./network.sh down
五、使用结构测试网络
1、使用启动脚本打开网络
./network.sh up
2、查看当前使用的镜像
- 与Fabric网络交互的每个节点和用户都必须属于作为网络成员的组织。属于Fabric网络成员的组织组通常称为联盟。测试网络有两个联盟成员,Org1和Org2。该网络还包括维护网络订购服务的一个订购者组织。
- 每个组织运营一个对等方。(对等方存储区块链分类帐并在将交易提交到分类帐之前对其进行验证。对等方运行包含业务逻辑的智能合约,该业务逻辑用于管理区块链分类账上的资产。)
org1(peer0.org1.example.com
) org2(peer0.org2.example.com
)
docker ps -a
2、创建通道
- 为Org1和Org2之间的事务创建Fabric通道。
- 通道是特定网络成员之间的专用通信层。频道只能由受邀加入该频道的组织使用,并且对网络的其他成员不可见。
- 每个通道都有一个单独的区块链分类帐。被邀请的组织将其同行“加入”渠道以存储渠道分类帐并验证渠道上的交易。
- network.sh脚本在Org1和Org2之间创建频道,并将其对等方加入该频道。
./network.sh createChannel # 默认名称为mychannel
没有报错即可
tips:
创建自定义名称的频道
./network.sh createChannel -c channel1
通过指定不同的通道名称来创建多个通道
./network.sh createChannel -c channel2
逐步建立网络并创建频道
./network.sh up createChannel
3、在通道上启动链码
- 创建通道后,可以开始使用智能合约与通道分类帐进行交互。
- 智能合约包含管理区块链分类账上资产的业务逻辑。网络成员运行的应用程序可以调用智能合约以在分类账上创建资产,以及更改和转移这些资产。应用程序还查询智能合约以读取分类账上的数据。
- 在Fabric中,智能合约以称为chaincode的软件包部署在网络上。Chaincode安装在组织的对等方上,然后部署到通道,然后可以在该通道中用于认可交易并与区块链分类账进行交互。
- 在将链码部署到通道之前,通道成员需要就建立链码管理的链码定义达成共识。当所需的组织数目达成一致时,可以将链码定义提交给通道,并准备开始使用链码。
./network.sh deployCC
如果是第一次部署链码,将安装链码依赖项。默认情况下,会安装资产转移(基本)链码的Go版本。
六、与网络互动
启用测试网络后,可以使用peerCLI与网络进行交互。peerCLI允许你从CLI调用已部署的智能合约,更新通道或安装和部署新的智能合约。
1、配置config和Org1环境变量
vim /etc/profile
如下配置,具体路径记得修改!!!
#fabric
export PATH=$PATH:/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin
export PATH=$PATH:/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/config
export FABRIC_CFG_PATH=/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/config
# Environment variavles for Org1
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
更新环境变量
source /etc/profile
2、初始化资产分类帐
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'
返回200成功
3、查询分类账
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
查询结果如下
4、资产转移
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'
返回成功
5、配置Org2环境变量
记得修改路径!!
# Environment variables for Org2
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=/root/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
6、查询转移到Org2的资产
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
七、关闭网络
使用完成后关闭网络
./network.sh down
来源:oschina
链接:https://my.oschina.net/u/4266968/blog/4791995