Linux部署Hyperledger Fabric2.0流程

其实该方法也基本适用于Ubuntu系统,区别在于CentOS用yum下载工具包,Ubuntu用apt-get下载工具包,用Ubuntu的小伙伴可灵活使用该方法

安装go语言环境到/usr/local下

wget链接获取来源链接为https://studygolang.com/dl,如果以下wget命令失效请到以上网站获取或下载
打开终端,输入指令

sudo wget -P /usr/local https://studygolang.com/dl/golang/go1.15.linux-amd64.tar.gz
cd /usr/local
sudo tar -zxvf go1.15.linux-amd64.tar.gz

添加环境变量

vim ~/.bashrc

将以下内容复制到bashrc文件中,按I插入,插入完成后按ESC退出插入,输出:wq!保存退出,如下图所示

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/go
Linux部署Hyperledger Fabric2.0流程_第1张图片
source ~/.bashrc

验证go是否安装成功

go version

安装docker

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
sudo yum -y install docker-ce

启动Docker-ce

sudo systemctl start docker
sudo systemctl enable docker

将当前用户添加到docker组中

sudo usermod -aG docker $USER
newgrp - docker

检查docker是否安装成功

docker -v

在这里插入图片描述

安装docker-compose

curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose 
chmod +x /usr/local/bin/docker-compose

查看docker是否安装成功

docker-compose -v

下载Hyperledger Fabric源码

安装git

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo rpm -ivh epel-release-latest-7.noarch.rpm
sudo yum install -y git

下面第一条指令中yujialing为你的用户名

cd /home/yujialing
mkdir go
mkdir go/src
mkdir go/src/github.com
mkdir go/src/github.com/hyperledger
mkdir go/pkg 
mkdir go/bin
cd go/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git

下载速度还是很慢的小伙伴可以Ctrl+C结束以上github克隆进程,试下我的gitee仓库克隆,一样的

git clone https://gitee.com/yu_jialing/fabric.git

下载完成

cd fabric
git checkout release-2.0

编译并拉取docker镜像

sudo yum install gcc
make all

查看docker镜像

sudo docker images
Linux部署Hyperledger Fabric2.0流程_第2张图片

下载Fabric的例程

git clone https://github.com/hyperledger/fabric-samples.git
git checkout v2.0.0

将编译后的可执行文件放入fabric-samples中

cd fabric-samples
cp -r build/bin fabric-samples

启动简单的联盟链网络

切换到first-network中的abstore智能合约文件中下载依赖并启动例程联盟链网络

cd ~/go/src/github.com/hyperledger/fabric/fabric-samples/chaincode/abstore/go
go env -w GOPROXY=https://goproxy.io,direct
go env -w GO111MODULE=on
go mod vendor
cd ../../..
cd first-network/
./byfn.sh up

出现END则表示网络启动成功,网络启动过程详情可以到first-network/scripts文件夹中的script.sh和utils.sh这两个脚本中查看
Linux部署Hyperledger Fabric2.0流程_第3张图片

fabric-gateway-java2.0.0实现与hyperledger联盟链的交互

建立一个maven工程
项目路径不要出现中文,否则将因读不到crypto-config里面的证书文件而报错
在pom.xml中导入依赖

<!-- hyperledger fabric2.0的java依赖 -->
<dependency>
    <groupId>org.hyperledger.fabric</groupId>
    <artifactId>fabric-gateway-java</artifactId>
    <version>2.0.0</version>
</dependency>  

回到虚拟机中将执行./byfn.sh up后生成的证书文件夹crypto-config(在first-network文件夹下)复制到项目中的resources文件夹下
Linux部署Hyperledger Fabric2.0流程_第4张图片

同时在resources文件夹下新建connection.json文件,用来放连接联盟链网络参数
Linux部署Hyperledger Fabric2.0流程_第5张图片

查看IP地址,connection.json要用到

ifconfig env33
Linux部署Hyperledger Fabric2.0流程_第6张图片

connection.json如下,里面涉及的IP地址是你开启区块链网络的主机地址,记得改下面的IP否则连接不上

{
     
  "name": "basic-network",
  "version": "1.0.0",
  "client": {
     
    "organization": "Org1",
    "connection": {
     
      "timeout": {
     
        "peer": {
     
          "endorser": "300"
        },
        "orderer": "300"
      }
    }
  },
  "channels": {
     
    "mychannel": {
     
      "orderers": [
        "orderer.example.com"
      ],
      "peers": {
     
        "peer0.org1.example.com": {
     
          "endorsingPeer": true,
          "chaincodeQuery": true,
          "ledgerQuery": true,
          "eventSource": true
        },
        "peer0.org2.example.com": {
     
          "endorsingPeer": true,
          "chaincodeQuery": true,
          "ledgerQuery": true,
          "eventSource": true
        }
      }
    }
  },
  "organizations": {
     
    "Org1": {
     
      "mspid": "Org1MSP",
      "peers": [
        "peer0.org1.example.com"
      ],
      "certificateAuthorities": [
        "ca-org1"
      ],
      "adminPrivateKeyPEM": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
      },
      "signedCertPEM": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]"
      }
    },
    "Org2": {
     
      "mspid": "Org2MSP",
      "peers": [
        "peer0.org2.example.com"
      ],
      "certificateAuthorities": [
        "ca-org2"
      ],
      "adminPrivateKeyPEM": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/[email protected]/msp/keystore/priv_sk"
      },
      "signedCertPEM": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/[email protected]/msp/signcerts/[email protected]"
      }
    }
  },
  "orderers": {
     
    "orderer.example.com": {
     
      "url": "grpcs://192.168.171.xxx:7050",
      "mspid": "OrdererMSP",
      "grpcOptions": {
     
        "ssl-target-name-override": "orderer.example.com",
        "hostnameOverride": "orderer.example.com"
      },
      "tlsCACerts": {
     
        "path": "src/main/resources/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt"
      },
      "adminPrivateKeyPEM": {
     
        "path": "src/main/resources/crypto-config/ordererOrganizations/example.com/users/[email protected]/msp/keystore/priv_sk"
      },
      "signedCertPEM": {
     
        "path": "src/main/resources/crypto-config/ordererOrganizations/example.com/users/[email protected]/msp/signcerts/[email protected]"
      }
    }
  },
  "peers": {
     
    "peer0.org1.example.com": {
     
      "url": "grpcs://192.168.171.xxx:7051",
      "grpcOptions": {
     
        "ssl-target-name-override": "peer0.org1.example.com",
        "hostnameOverride": "peer0.org1.example.com",
        "request-timeout": 120001
      },
      "tlsCACerts": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt"
      }
    },
    "peer0.org2.example.com": {
     
      "url": "grpcs://192.168.171.xxx:9051",
      "grpcOptions": {
     
        "ssl-target-name-override": "peer0.org2.example.com",
        "hostnameOverride": "peer0.org2.example.com",
        "request-timeout": 120001
      },
      "tlsCACerts": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"
      }
    }
  },
  "certificateAuthorities": {
     
    "ca-org1": {
     
      "url": "https://192.168.171.xxx:7054",
      "grpcOptions": {
     
        "verify": true
      },
      "tlsCACerts": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem"
      },
      "registrar": [
        {
     
          "enrollId": "admin",
          "enrollSecret": "adminpw"
        }
      ]
    },
    "ca-org2": {
     
      "url": "https://192.168.171.xxx:8054",
      "grpcOptions": {
     
        "verify": true
      },
      "tlsCACerts": {
     
        "path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/ca/ca.org2.example.com-cert.pem"
      },
      "registrar": [
        {
     
          "enrollId": "admin",
          "enrollSecret": "adminpw"
        }
      ]
    }
  }
}

运行以下代码(随便命名demo.java就行)

package com.scau.amops.hyperledger;

import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.gateway.*;
import org.hyperledger.fabric.gateway.impl.GatewayImpl;
import org.hyperledger.fabric.sdk.Peer;

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.EnumSet;
/**
 * 

* 超级账本DEMO *

*/
@Slf4j public class demo{ private Gateway gateway; private Network network; private static final Path NETWORK_CONFIG_PATH = Paths.get("src", "main", "resources", "connection.json"); private static final Path credentialPath = Paths.get("src", "main","resources", "crypto-config", "peerOrganizations", "org1.example.com", "users", "[email protected]", "msp"); public static void main(String[] args) { X509Certificate certificate = null; PrivateKey privateKey = null; Gateway gateway = null; try { //使用org1中的user1初始化一个网关wallet账户用于连接网络 Wallet wallet = Wallets.newInMemoryWallet(); Path certificatePath = credentialPath.resolve(Paths.get("signcerts", "[email protected]")); certificate = readX509Certificate(certificatePath); Path privateKeyPath = credentialPath.resolve(Paths.get("keystore", "priv_sk")); privateKey = getPrivateKey(privateKeyPath); wallet.put("user",Identities.newX509Identity("Org1MSP",certificate,privateKey)); //根据connection.json 获取Fabric网络连接对象 GatewayImpl.Builder builder = (GatewayImpl.Builder) Gateway.createBuilder(); builder.identity(wallet, "user").networkConfig(NETWORK_CONFIG_PATH); //连接网关 gateway = builder.connect(); //获取mychannel通道 Network network = gateway.getNetwork("mychannel"); //获取合约对象 Contract contract = network.getContract("mycc"); //查询a的余额 byte[] queryAResultBefore = contract.evaluateTransaction("query","a"); System.out.println("交易前:"+new String(queryAResultBefore, StandardCharsets.UTF_8)); // a转50给b byte[] invokeResult = contract.createTransaction("invoke") .setEndorsingPeers(network.getChannel().getPeers(EnumSet.of(Peer.PeerRole.ENDORSING_PEER))) .submit("a", "b", "50"); System.out.println(new String(invokeResult, StandardCharsets.UTF_8)); //查询交易结果 byte[] queryAResultAfter = contract.evaluateTransaction("query","a"); System.out.println("交易后:"+new String(queryAResultAfter, StandardCharsets.UTF_8)); } catch (Exception e) { e.printStackTrace(); } } private static X509Certificate readX509Certificate(final Path certificatePath) throws IOException, CertificateException { try (Reader certificateReader = Files.newBufferedReader(certificatePath, StandardCharsets.UTF_8)) { return Identities.readX509Certificate(certificateReader); } } private static PrivateKey getPrivateKey(final Path privateKeyPath) throws IOException, InvalidKeyException { try (Reader privateKeyReader = Files.newBufferedReader(privateKeyPath, StandardCharsets.UTF_8)) { return Identities.readPrivateKey(privateKeyReader); } } }

运行结果显示
Linux部署Hyperledger Fabric2.0流程_第7张图片
到虚拟机上查一查,a的余额的确变为40

docker exec -it cli bash
peer chaincode query -C mychannel -n mycc -c '{"Args":["query", "a"]}'
Linux部署Hyperledger Fabric2.0流程_第8张图片

成功实现外部与联盟链网络交互
虚拟机关机前记得将区块链网络关掉,不然下次开机启动区块链网络则报错

cd ~/go/src/github.com/hyperledger/fabric/fabric-samples/first-network
./byfn.sh down

持续更新中!!!
(将更新:修改启动网络的脚本实现其他智能合约的应用)

你可能感兴趣的:(Hyperledger,Fabric,2.x,区块链,linux,区块链,分布式,数据安全,信息安全,智能合约)