Hyperledger Fabric 2.x Java區塊鏈應用

語言: CN / TW / HK

點選上方“ 陶陶技術筆記 ”關注我

回覆“ 資料 ”獲取作者整理的大量學習資料!

file

一、說明

在上一篇文章中 《Hyperledger Fabric 2.x 自定義智慧合約》 分享了智慧合約的安裝並使用 cli 客戶端進行合約的呼叫;本文將使用 Java 程式碼基於 fabric-gateway-java 進行區塊鏈網路的訪問與交易,並整合 SpringBoot 框架。

Fabric Gateway SDK 實現Fabric的程式設計模型,提供了一系列簡單的API給應用程式與Fabric區塊鏈網路進行互動;

網路拓撲圖:

file

應用程式將各自的網路互動委託給其閘道器,每個閘道器都瞭解網路通道拓撲,包括組織的多個Peer節點和排序節點,使應用程式專注於業務邏輯;Peer節點可以使用gossip協議在組織內部和組織之間相互通訊。

二、Mavn依賴

新增閘道器sdk的依賴:

<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId>
<version>2.2.3</version>
</dependency>

三、準備配置檔案

工程的目錄結構如下圖所示:

file

3.1. 準備網路證書

建立目錄 crypto-configordererpeer 節點的證書檔案複製進來。

證書檔案從 fabric-samplestest-network 目錄中複製 ordererOrganizationspeerOrganizations 資料夾:

file

3.2. 建立網路配置

建立檔案 connection.json 內容如下:

{
"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.28.134: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.28.134: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.28.134: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.28.134: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.28.134: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"
}
]
}
}
}

需按實際情況修改url中的地址,內容中分別包含了 channelsorganizationsordererspeersca 的配置

3.3. SpringBoot配置

application.yml 中新增以下內容,用於訪問閘道器的相關配置:

fabric:
# wallet資料夾路徑(自動建立)
walletDirectory: wallet
# 網路配置檔案路徑
networkConfigPath: connection.json
# 使用者證書路徑
certificatePath: crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]
# 使用者私鑰路徑
privateKeyPath: crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk
# 訪問的組織名
mspid: Org1MSP
# 使用者名稱
username: user1
# 通道名字
channelName: mychannel
# 鏈碼名字
contractName: mycc

四、連接合約

分別構建閘道器、通道和合約的Bean物件,程式碼如下:

/**
* 連線閘道器
*/

@Bean
public Gateway connectGateway() throws IOException, InvalidKeyException, CertificateException {
//使用org1中的user1初始化一個閘道器wallet賬戶用於連線網路
Wallet wallet = Wallets.newFileSystemWallet(Paths.get(this.walletDirectory));
X509Certificate certificate = readX509Certificate(Paths.get(this.certificatePath));
PrivateKey privateKey = getPrivateKey(Paths.get(this.privateKeyPath));
wallet.put(username, Identities.newX509Identity(this.mspid, certificate, privateKey));

//根據connection.json 獲取Fabric網路連線物件
Gateway.Builder builder = Gateway.createBuilder()
.identity(wallet, username)
.networkConfig(Paths.get(this.networkConfigPath));

//連線閘道器
return builder.connect();
}

/**
* 獲取通道
*/

@Bean
public Network network(Gateway gateway) {
return gateway.getNetwork(this.channelName);
}

/**
* 獲取合約
*/

@Bean
public Contract contract(Network network) {
return network.getContract(this.contractName);
}

五、合約呼叫

建立controller類,注入Contract物件呼叫合約方法:

@Resource
private Contract contract;

@Resource
private Network network;

@GetMapping("/getUser")
public String getUser(String userId) throws ContractException {
byte[] queryAResultBefore = contract.evaluateTransaction("getUser",userId);
return new String(queryAResultBefore, StandardCharsets.UTF_8);
}

@GetMapping("/addUser")
public String addUser(String userId, String userName, String money) throws ContractException, InterruptedException, TimeoutException {
byte[] invokeResult = contract.createTransaction("addUser")
.setEndorsingPeers(network.getChannel().getPeers(EnumSet.of(Peer.PeerRole.ENDORSING_PEER)))
.submit(userId, userName, money);
String txId = new String(invokeResult, StandardCharsets.UTF_8);
return txId;
}

六、測試介面

呼叫介面 getUser

http://127.0.0.1:9001/getUser?userId=1

返回:

{
"money": 300,
"name": "zlt",
"userId": "1"
}

呼叫介面 addUser

http://127.0.0.1:9001/addUser?userId=6&userName=test6&money=600

返回:

2ae291bb6a366b5ba01ad49e4237da8def9e9828cc2c982e8c49d4b763af0157

七、程式碼下載

gitee: https://gitee.com/zlt2000/my-fabric-application-java

github: https://github.com/zlt2000/my-fabric-application-java