2.4 Fabric部署测试网络

 一、准备工作

1.1 修改机器 IP,变成静态 IP

    ```bash
    vi /etc/sysconfig/network-scripts/ifcfg-ens33

    TYPE=Ethernet 
    PROXY_METHOD=none 
    BROWSER_ONLY=no 
    BOOTPROTO=static   --dhcp
    IPADDR=192.168.88.152
    NETMASK=255.255.255.0
    GATEWAY=192.168.88.2
    DNS1=192.168.88.2    
    DEFROUTE=yes 
    IPV4_FAILURE_FATAL=no 
    IPV6INIT=yes 
    IPV6_AUTOCONF=yes 
    IPV6_DEFROUTE=yes 
    IPV6_FAILURE_FATAL=no 
    IPV6_ADDR_GEN_MODE=stable-privacy 
    NAME=ens33 
    DEVICE=ens33 
    ONBOOT=yes


    #修改配置文件之后需要重启网络服务才能使配置生效,重启网络服务命令如下: 
    service network restart

1.2. **cURL最新版安装**

   ```bash
   # 下载最新版的cURL并将其解压到指定目录(此处以/opt目录为例)
   wget https://curl.haxx.se/download/curl-7.63.0.tar.gz
   sudo tar xzvf curl-7.63.0.tar.gz -C /opt
   
   # 编译并安装cURL
   cd /opt/curl-7.63.0
   ./configure
   make
   sudo make install
   ```

   通过运行`curl --version`查看是否安装成功,若结果如下图所示,则表示安装成功。

   若出现下图所示错误,则需进行如下操作。

   ```bash
   # 在/etc/ld.so.conf加入/usr/local/lib这一行内容
   sudo echo "/usr/local/lib" >> /etc/ld.so.conf
   
   # 执行下列命令使加入的内容生效
   sudo /sbin/ldconfig -v
   ```

1.3. **Docker和Docker Compose安装**

 - Docker(参考[Get Docker CE for Ubuntu](https://docs.docker.com/install/linux/docker-ce/ubuntu/)) 

    ```bash
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
    sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    sudo yum makecache fast
    sudo yum -y install docker-ce
    sudo service docker start

    #设置开机自启动

    sudo systemctl enable docker.service
    docker version
    ```

 - Docker Compose

```bash
     # 下载Docker Compose二进制文件  注意:/usr/local/bin/docker-compose 后面不要有空格

sudo curl -L "https://gitee.com/wugenshui/docker-compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

      sudo chmod +x /usr/local/bin/docker-compose
      docker-compose --version
     ```

1.4. **Go安装**

```bash
   # 下载go1.19.4.linux-amd64.tar.gz并将其解压到指定目录(此处为/usr/loca
   wget https://dl.google.com/go/go1.19.4.linux-amd64.tar.gz

   tar xzvf go1.19.4.linux-amd64.tar.gz -C /usr/local
   
   #配置环境变量
   vi /etc/profile

    #在/etc/profile文件最后添加下面配置
    export GO111MODULE=on
    export GOROOT=/usr/local/go 
    export GOPATH=/home/gopath
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
   
   #创建/home/gopath目录
   mkdir /home/gopath
   #重新读取配置
   source /etc/profile
   #查看版本信息
   go version
   ```

 二、Fabric测试网络安装

1. 将fabric-samples下载到`$GOPATH/src/github.com/hyperledger`目录中

   ```bash
   mkdir -p $GOPATH/src/github.com/hyperledger
   cd $GOPATH/src/github.com/hyperledger

git clone https://github.com/GitHep/fabric-samples.git

   cd fabric-samples

2. 安装Fabric Binaries和Fabric相关的Docker镜像

 ```bash

注:fabric2.4.1版本下载以下版本
      wget https://github.com/hyperledger/fabric/releases/download/v2.4.1/hyperledger-fabric-linux-amd64-2.4.1.tar.gz
     wget https://github.com/hyperledger/fabric-ca/releases/download/v1.5.5/hyperledger-fabric-ca-linux-amd64-1.5.5.tar.gz
     
     tar xzvf hyperledger-fabric-linux-amd64-2.4.1.tar.gz -C $GOPATH/src/github.com/hyperledger/
     tar xzvf hyperledger-fabric-ca-linux-amd64-1.5.5.tar.gz -C $GOPATH/src/github.com/hyperledger/

     #bin和config文件放入fabric-samples文件夹下

     cd $GOPATH/src/github.com/hyperledger
     mv bin/ fabric-samples
     mv config/ fabric-samples
     
     #向/etc/profile中写入环境变量
     sudo echo 'export PATH=$GOPATH/src/github.com/hyperledger/fabric-samples/bin:$PATH' >> /etc/profile
        sudo echo 'export FABRIC_CFG_PATH=$GOPATH/src/github.com/hyperledger/fabric-samples/config' >> /etc/profile
     
     #使环境变量生效
     source /etc/profile

     #查看peer是否生效
     which peer

     ```

 - 下载相关Docker镜像

docker pull hyperledger/fabric-peer:2.4.1
docker tag hyperledger/fabric-peer:2.4.1 hyperledger/fabric-peer:latest
docker pull hyperledger/fabric-orderer:2.4.1
docker tag hyperledger/fabric-orderer:2.4.1 hyperledger/fabric-orderer:latest
docker pull hyperledger/fabric-ccenv:2.4.1
docker tag hyperledger/fabric-ccenv:2.4.1 hyperledger/fabric-ccenv:latest
docker pull hyperledger/fabric-tools:2.4.1
docker tag hyperledger/fabric-tools:2.4.1 hyperledger/fabric-tools:latest
docker pull hyperledger/fabric-couchdb:0.4.14
docker tag hyperledger/fabric-couchdb:0.4.14 hyperledger/fabric-couchdb:latest
docker pull hyperledger/fabric-kafka:0.4.14
docker tag hyperledger/fabric-kafka:0.4.14 hyperledger/fabric-kafka:latest
docker pull hyperledger/fabric-zookeeper:0.4.14
docker tag hyperledger/fabric-zookeeper:0.4.14 hyperledger/fabric-zookeeper:latest
docker pull hyperledger/fabric-ca:1.5.5
docker tag hyperledger/fabric-ca:1.5.5 hyperledger/fabric-ca:latest
docker pull hyperledger/fabric-baseos:2.4.1
docker tag hyperledger/fabric-baseos:2.4.1 hyperledger/fabric-baseos:latest
docker pull hyperledger/fabric-javaenv:2.4.1
docker tag hyperledger/fabric-javaenv:2.4.1 hyperledger/fabric-javaenv:latest

启动test-network测试网络

cd $GOPATH/src/github.com/hyperledger/fabric-sample/test-network
./network.sh up
./network.sh createChannel

创建go链码

#使用peer lifecycle chaincode package命令创建链码包
peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1

 打包有以下两个报错,解决方案如下

错误1:
[root@localhost test-network]# peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --l      abel fabcar_1
Error: failed to normalize chaincode path: 'go list' failed with: go: github.com/hyperledger/[email protected]: Ge      t "https://proxy.golang.org/github.com/hyperledger/fabric-contract-api-go/@v/v1.1.0.mod": dial tcp 172.217.163.49:443: connect      : connection refused
go: downloading github.com/hyperledger/fabric-contract-api-go v1.1.0
go: github.com/hyperledger/[email protected]: Get "https://proxy.golang.org/github.com/hyperledger/fabric-contract      -api-go/@v/v1.1.0.mod": dial tcp 172.217.163.49:443: connect: connection refused: exit status 1

解决方案:
[root@localhost test-network]# go env -w GOPROXY=https://goproxy.io,direct
[root@localhost test-network]# go env -w GO111MODULE=on

错误2:
[root@localhost test-network]# peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --l      abel fabcar_1
Error: failed to normalize chaincode path: 'go list' failed with: verifying github.com/cucumber/[email protected]/go.mod: checksum       mismatch
        downloaded: h1:FQ2MobPXycdSajAK3inNgLSAKGFmZqbE4S/CExz41Ys=
        go.sum:     h1:Cp3tEV1LRAyH/RuCThcxHS/+9ORZ+FMzPva2AZ5Ki+A=
SECURITY ERROR
This download does NOT match an earlier download recorded in go.sum.
The bits may have been replaced on the origin server, or an attacker may
have intercepted the download attempt.
For more information, see 'go help module-auth'.: exit status 1

解决方案:
[root@localhost test-network]# cd ../chaincode/fabcar/go
[root@localhost test-network]# go env -w GOPROXY=https://goproxy.cn,direct
[root@localhost test-network]# go clean -modcache
[root@localhost test-network]# go mod tidy





安装java链码包

cd $GOPATH/src/github.com/hyperledger/fabric-sample/chaincode
git clone https://gitee.com/kernelHP/hyperledger-fabric-contract-java-demo.git

---打包项目
cd $GOPATH/src/github.com/hyperledger/fabric-samples/test-network
peer lifecycle chaincode package hyperledger-fabric-contract-java-demo.tar.gz --path ../chaincode/hyperledger-fabric-contract-java-demo --lang java --label hyperledger-fabric-contract-java-demo_1

---切换组织Org1--
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

---org1安装链码--
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

---查询包ID--
peer lifecycle chaincode queryinstalled

---切换组织Org2--
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

---org2安装链码--
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

---查询包ID--
peer lifecycle chaincode queryinstalled

---通过链码时,我们将使用包ID,因此,将包ID保存为环境变量,将返回的包ID粘贴到下面的命令中--
export CC_PACKAGE_ID=hyperledger-fabric-contract-java-demo_1:743062218b4b3c5d46c486ec21dc78a9054959dbab0dc82956acd22f0fda9d96

---Org2通过链码定义--
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem


---切换组织Org1--
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051

---Org1通过链码定义--
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem



=====将链码定义提交给通道=====
---使用peer lifecycle chaincode checkcommitrediness命令来检查通道成员是否已批准相同的链码定义--
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json


---使用peer lifecycle chaincode commit命令将链码定义提交到通道。commit命令还需要由组织管理员提交--
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --version 1.0 --collections-config ../chaincode/hyperledger-fabric-contract-java-demo/collections_config.json --signature-policy "OR('Org1MSP.member','Org2MSP.member')" --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --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


---使用peer lifecycle chaincode querycommitted命令来确认链码定义已提交给通道
peer lifecycle chaincode querycommitted --channelID mychannel --name hyperledger-fabric-contract-java-demo --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem





=========调用链码=================
---创建猫cat-0--- 
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 hyperledger-fabric-contract-java-demo --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":"createCat","Args":["cat-0","tom","3","蓝色","大懒猫"]}'

---产看猫cat-0---
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'

---更新猫cat-0---
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 hyperledger-fabric-contract-java-demo --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":"updateCat","Args":["cat-0","tom","3","白色","超级大懒猫"]}'

---查看猫cat-0---
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'

---删除猫cat-0---
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 hyperledger-fabric-contract-java-demo --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":"deleteCat","Args":["cat-0","tom","3","白色","超级大懒猫"]}'

---查看猫cat-0---
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'
https://gitee.com/kernelHP/hyperledger-fabric-app-java-demo.git
3、CouchDB可视化数据库

嵌入couchDB启动方式

./network.sh up createChannel -s couchdb

通过couchDB做其它字段检索或分页查询

---创建数据----
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 hyperledger-fabric-contract-java-demo --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":"createCat","Args":["cat-0" , "tom" ,  "3" , "蓝色" , "大懒猫"]}'

---通过name检索查询----
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCatByName" , "tom"]}'

---分页查询---
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCatPageByName" , "tom" , "1" , ""]}'

添加索引方式

====方式1===
JSON 索引文件必须放在链码目录的 META-INF/statedb/couchdb/indexes 路径下。索引文件名称可以任意。

{
  "index":{
      "fields":["docType","owner"] // Names of the fields to be queried
  },
  "ddoc":"indexOwnerDoc", // (optional) Name of the design document in which the index will be created.
  "name":"indexOwner",
  "type":"json"
}

====方式2===
通过可视化页面编辑
http://182.92.99.138:5984/_utils/
账号密码在:$GOPATH/src/github.com/hyperledger/fabric-samples/test-network/docker/docker-compose-couchdb.yaml

1

你可能感兴趣的:(运维,linux,服务器)