日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
HyperledgerFabric2.xJava區(qū)塊鏈應(yīng)用

一、說明

在上一篇文章中 《Hyperledger Fabric 2.x 自定義智能合約》 分享了智能合約的安裝并使用 cli 客戶端進行合約的調(diào)用;本文將使用 Java 代碼基于 fabric-gateway-java 進行區(qū)塊鏈網(wǎng)絡(luò)的訪問與交易,并集成 SpringBoot 框架。

銅陵網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。

Fabric Gateway SDK 實現(xiàn)Fabric的編程模型,提供了一系列簡單的API給應(yīng)用程序與Fabric區(qū)塊鏈網(wǎng)絡(luò)進行交互;

網(wǎng)絡(luò)拓撲圖:

file

應(yīng)用程序?qū)⒏髯缘木W(wǎng)絡(luò)交互委托給其網(wǎng)關(guān),每個網(wǎng)關(guān)都了解網(wǎng)絡(luò)信道拓撲,包括組織的多個Peer節(jié)點和排序節(jié)點,使應(yīng)用程序?qū)W⒂跇I(yè)務(wù)邏輯;Peer節(jié)點可以使用gossip協(xié)議在組織內(nèi)部和組織之間相互通信。

二、Mavn依賴

添加網(wǎng)關(guān)sdk的依賴:


org.hyperledger.fabric
fabric-gateway-java
2.2.3

三、準備配置文件

工程的目錄結(jié)構(gòu)如下圖所示:

3.1. 準備網(wǎng)絡(luò)證書

創(chuàng)建目錄 crypto-config 把 orderer 和 peer 節(jié)點的證書文件復(fù)制進來。

證書文件從 fabric-samples 的 test-network 目錄中復(fù)制 ordererOrganizations 與 peerOrganizations 文件夾:

3.2. 創(chuàng)建網(wǎng)絡(luò)配置

創(chuàng)建文件 connection.json 內(nèi)容如下:

{
"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/Admin@org1.example.com/msp/keystore/priv_sk"
},
"signedCertPEM": {
"path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
}
},
"Org2": {
"mspid": "Org2MSP",
"peers": [
"peer0.org2.example.com"
],
"certificateAuthorities": [
"ca-org2"
],
"adminPrivateKeyPEM": {
"path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/keystore/priv_sk"
},
"signedCertPEM": {
"path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/signcerts/Admin@org2.example.com-cert.pem"
}
}
},
"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/Admin@example.com/msp/keystore/priv_sk"
},
"signedCertPEM": {
"path": "src/main/resources/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/Admin@example.com-cert.pem"
}
}
},
"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中的地址,內(nèi)容中分別包含了channels、organizations、orderers、peers、ca 的配置

3.3. SpringBoot配置

在 application.yml 中添加以下內(nèi)容,用于訪問網(wǎng)關(guān)的相關(guān)配置:

fabric:
# wallet文件夾路徑(自動創(chuàng)建)
walletDirectory: wallet
# 網(wǎng)絡(luò)配置文件路徑
networkConfigPath: connection.json
# 用戶證書路徑
certificatePath: crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem
# 用戶私鑰路徑
privateKeyPath: crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk
# 訪問的組織名
mspid: Org1MSP
# 用戶名
username: user1
# 通道名字
channelName: mychannel
# 鏈碼名字
contractName: mycc

四、連接合約

分別構(gòu)建網(wǎng)關(guān)、通道和合約的Bean對象,代碼如下:

/**
* 連接網(wǎng)關(guān)
*/
@Bean
public Gateway connectGateway() throws IOException, InvalidKeyException, CertificateException {
//使用org1中的user1初始化一個網(wǎng)關(guān)wallet賬戶用于連接網(wǎng)絡(luò)
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));

//根據(jù)connection.json 獲取Fabric網(wǎng)絡(luò)連接對象
Gateway.Builder builder = Gateway.createBuilder()
.identity(wallet, username)
.networkConfig(Paths.get(this.networkConfigPath));

//連接網(wǎng)關(guān)
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);
}

五、合約調(diào)用

創(chuàng)建controller類,注入Contract對象調(diào)用合約方法:

@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;
}

六、測試接口

調(diào)用接口 getUser:

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

返回:

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

調(diào)用接口 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


網(wǎng)站欄目:HyperledgerFabric2.xJava區(qū)塊鏈應(yīng)用
文章位置:http://www.5511xx.com/article/coedohd.html