您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Hyperledger Fabric中如何部署并運(yùn)行Java鏈代碼的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
您已經(jīng)定義并啟動(dòng)了本地區(qū)塊鏈網(wǎng)絡(luò),而且已構(gòu)建 Java shim 客戶端 JAR 并安裝到本地 Maven 存儲(chǔ)庫(kù)中,現(xiàn)在已準(zhǔn)備好在之前下載的 Hyperledger Fabric 附帶的一個(gè) Java 鏈代碼示例上構(gòu)建、注冊(cè)和調(diào)用交易。
部署并運(yùn)行鏈代碼
您將執(zhí)行以下步驟:
使用 Gradle 構(gòu)建示例。
通過(guò)運(yùn)行 Gradle 構(gòu)建軟件為您創(chuàng)建的腳本,向驗(yàn)證對(duì)等網(wǎng)絡(luò)注冊(cè)該示例。
使用 SoapUI 將示例部署到本地區(qū)塊鏈網(wǎng)絡(luò)。
使用 SoapUI 在示例鏈代碼上調(diào)用交易。
導(dǎo)航到 $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/java/Example 目錄。
接下來(lái),通過(guò)命令行,使用此命令啟動(dòng) Gradle 構(gòu)建軟件:
gradle -b build.gradle build
您會(huì)看到以下輸出:
$ cd GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/java/Example $ gradle -b build.gradle build Starting a Gradle Daemon (subsequent builds will be faster) :examples:chaincode:java:Example:compileJava :examples:chaincode:java:Example:processResources UP-TO-DATE :examples:chaincode:java:Example:classes :examples:chaincode:java:Example:jar :examples:chaincode:java:Example:startScripts :examples:chaincode:java:Example:distTar :examples:chaincode:java:Example:distZip :examples:chaincode:java:Example:assemble :examples:chaincode:java:Example:compileTestJava UP-TO-DATE :examples:chaincode:java:Example:processTestResources UP-TO-DATE :examples:chaincode:java:Example:testClasses UP-TO-DATE :examples:chaincode:java:Example:test UP-TO-DATE :examples:chaincode:java:Example:check UP-TO-DATE :examples:chaincode:java:Example:build :examples:chaincode:java:Example:copyToLib BUILD SUCCESSFUL Total time: 6.935 secs
該構(gòu)建過(guò)程通過(guò)兩種形式創(chuàng)建了一個(gè)位于目錄 build/distributions 中的獨(dú)立發(fā)行版:TAR 文件和 ZIP 文件,每個(gè)文件都包含運(yùn)行鏈代碼所需的所有資源,其中包括一個(gè)用于驅(qū)動(dòng)鏈代碼的名為 Example 的腳本。
Example 鏈代碼現(xiàn)在已準(zhǔn)備好向本地區(qū)塊鏈網(wǎng)絡(luò)注冊(cè)。
確保本地區(qū)塊鏈網(wǎng)絡(luò)正在運(yùn)行。如果未運(yùn)行,則需要啟動(dòng)它。如果需要溫習(xí)一下相關(guān)內(nèi)容,請(qǐng)參閱“啟動(dòng)區(qū)塊鏈網(wǎng)絡(luò)”部分。
如果您未在 $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/java/Example 目錄下,請(qǐng)導(dǎo)航到這里。
接下來(lái),將 Example.zip(或 Example.tar)解壓到 build/distributions 目錄中:
$ cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/java/Example $ cd build/distributions/ $ unzip Example.zip Archive: Example.zip inflating: Example/lib/chaincode.jar inflating: Example/lib/grpc-all-0.13.2.jar inflating: Example/lib/commons-cli-1.3.1.jar inflating: Example/lib/shim-client-1.0.jar inflating: Example/lib/grpc-netty-0.13.2.jar inflating: Example/lib/grpc-auth-0.13.2.jar inflating: Example/lib/grpc-protobuf-nano-0.13.2.jar inflating: Example/lib/grpc-core-0.13.2.jar inflating: Example/lib/grpc-protobuf-0.13.2.jar inflating: Example/lib/grpc-okhttp-0.13.2.jar inflating: Example/lib/grpc-stub-0.13.2.jar inflating: Example/lib/protobuf-java-3.0.0.jar inflating: Example/lib/netty-tcnative-boringssl-static-1.1.33.Fork21-osx-x86_64.jar inflating: Example/lib/netty-codec-http2-4.1.0.CR3.jar inflating: Example/lib/google-auth-library-oauth3-http-0.3.0.jar inflating: Example/lib/guava-18.0.jar inflating: Example/lib/protobuf-javanano-3.0.0-alpha-5.jar inflating: Example/lib/jsr305-3.0.0.jar inflating: Example/lib/okio-1.6.0.jar inflating: Example/lib/okhttp-2.5.0.jar inflating: Example/lib/netty-codec-http-4.1.0.CR3.jar inflating: Example/lib/netty-handler-4.1.0.CR3.jar inflating: Example/lib/google-auth-library-credentials-0.3.0.jar inflating: Example/lib/google-http-client-1.19.0.jar inflating: Example/lib/google-http-client-jackson2-1.19.0.jar inflating: Example/lib/netty-codec-4.1.0.CR3.jar inflating: Example/lib/netty-buffer-4.1.0.CR3.jar inflating: Example/lib/netty-transport-4.1.0.CR3.jar inflating: Example/lib/httpclient-4.0.1.jar inflating: Example/lib/jackson-core-2.1.3.jar inflating: Example/lib/netty-common-4.1.0.CR3.jar inflating: Example/lib/netty-resolver-4.1.0.CR3.jar inflating: Example/lib/httpcore-4.0.1.jar inflating: Example/lib/commons-logging-1.1.1.jar inflating: Example/lib/commons-codec-1.3.jar inflating: Example/bin/Example inflating: Example/bin/Example.bat
您可能想知道 “為何有如此多的文件?”該發(fā)行版包含(在獨(dú)立進(jìn)程中)單獨(dú)運(yùn)行鏈代碼所需的一切資源,以及所有依賴 JAR 文件。
要注冊(cè)鏈代碼示例,可在 build/distributions 文件夾中執(zhí)行以下腳本:
./Example/bin/Example
這會(huì)運(yùn)行一個(gè)獨(dú)立進(jìn)程來(lái)向本地區(qū)塊鏈網(wǎng)絡(luò)注冊(cè)鏈代碼示例。您會(huì)看到以下終端窗口輸出:
$ ./Example/bin/Example Hello world! starting [Ljava.lang.String;@7ef20235 Feb 22, 2017 10:05:08 AM example.Example main INFO: starting Feb 22, 2017 10:05:08 AM org.hyperledger.java.shim.ChaincodeBase newPeerClientConnection INFO: Inside newPeerCLientConnection Feb 22, 2017 10:05:08 AM io.grpc.internal.TransportSet$1 call INFO: Created transport io.grpc.netty.NettyClientTransport@3dd7b80b(/127.0.0.1:7051) for /127.0.0.1:7051 Feb 22, 2017 10:05:14 AM io.grpc.internal.TransportSet$TransportListener transportReady INFO: Transport io.grpc.netty.NettyClientTransport@3dd7b80b(/127.0.0.1:7051) for /127.0.0.1:7051 is ready
查看本地區(qū)塊鏈網(wǎng)絡(luò)的控制臺(tái),您會(huì)看到以下輸出行:
. . vp0_1 | 16:05:14.048 [chaincode] HandleChaincodeStream -> DEBU 06d Current context deadline = 0001-01-01 00:00:00 +0000 UTC, ok = false vp0_1 | 16:05:14.065 [chaincode] processStream -> DEBU 06e []Received message REGISTER from shim vp0_1 | 16:05:14.065 [chaincode] HandleMessage -> DEBU 06f []Handling ChaincodeMessage of type: REGISTER in state created vp0_1 | 16:05:14.065 [chaincode] beforeRegisterEvent -> DEBU 070 Received REGISTER in state created vp0_1 | 16:05:14.065 [chaincode] registerHandler -> DEBU 071 registered handler complete for chaincode hello vp0_1 | 16:05:14.065 [chaincode] beforeRegisterEvent -> DEBU 072 Got REGISTER for chaincodeID = name:"hello" , sending back REGISTERED . .
記下注冊(cè)日志輸出中的 chaincodeID name(示例中為 hello;如上面 第 8 行 所示)。以后在通過(guò)結(jié)構(gòu)的 REST 接口部署 Example 鏈代碼時(shí),JSON 消息中需要使用此信息。
上面的輸出表明 Example 鏈代碼正在運(yùn)行,而且已向本地區(qū)塊鏈驗(yàn)證對(duì)等網(wǎng)絡(luò)注冊(cè),并做好了部署準(zhǔn)備。
Hyperledger Fabric 提供了一個(gè)用于與該結(jié)構(gòu)交互的 REST Web 服務(wù)接口。與 fabric 的第一次交互是部署鏈代碼。確保本地區(qū)塊鏈網(wǎng)絡(luò)正在運(yùn)行,然后啟動(dòng) SoapUI,單擊 REST 按鈕創(chuàng)建一個(gè)新的 REST 項(xiàng)目。您會(huì)看到一個(gè)類似圖 3 的對(duì)話框,在其中輸入用于所有 REST 請(qǐng)求的基礎(chǔ) URL:
輸入 http://localhost:7050 作為 URL,然后單擊 OK。端口 7050 是 fabric 使用的默認(rèn) REST 端口,而且因?yàn)閰^(qū)塊鏈網(wǎng)絡(luò)是在本地計(jì)算機(jī)上運(yùn)行的,所以將使用 localhost 作為主機(jī)名。
在 SoapUI 啟動(dòng)后,可以執(zhí)行一次快速冒煙測(cè)試,以確保它能與本地區(qū)塊鏈網(wǎng)絡(luò)進(jìn)行通信。展開(kāi)剛創(chuàng)建的新的 REST 資源,直到看到 Request 1,然后在 Editor 窗口中打開(kāi)它。使用 GET 方法,在 resource 下輸入 /chain。確保單擊了 output 選項(xiàng)卡上的 JSON 選項(xiàng),然后運(yùn)行請(qǐng)求(通過(guò)單擊 arrow 圖標(biāo))。執(zhí)行此請(qǐng)求時(shí),會(huì)在 Editor 窗口右側(cè)的輸出選項(xiàng)卡中返回當(dāng)前區(qū)塊的哈希值,如圖 4 所示:
如果看到一條類似圖 4 的 JSON 消息(當(dāng)然您的網(wǎng)絡(luò)的 currentBlockHash 值會(huì)有所不同),那么您已準(zhǔn)備好部署 Example 鏈代碼。
右鍵單擊 REST Project 1 (http://localhost:7050) 下的端點(diǎn)并選擇 New Resource;您會(huì)看到一個(gè)包含 Resource Path 字段的 “New REST Resource” 對(duì)話框(參見(jiàn)圖 5): 輸入 /chaincode 作為 resource path,然后單擊 OK,您會(huì)看到 SoapUI Projects 面板中顯示了新資源。打開(kāi)對(duì)此資源的請(qǐng)求(默認(rèn)情況下該請(qǐng)求名為 Request 1),將方法更改為 POST,并將此 JSON 粘貼到請(qǐng)求編輯器窗口左下角的請(qǐng)求區(qū)域:
{ "jsonrpc": "2.0", "method": "deploy", "params": { "type": 1, "chaincodeID":{ "name": "hello" }, "CtorMsg": { "args": [""] } }, "id": 1 }
有 3 點(diǎn)需要注意:
第 3 行:method 值必須為 deploy。 第 6-7 行:JSON 消息中的 chaincodeID.name 必須與您在上一節(jié)中注冊(cè) Example 鏈代碼時(shí)所用的 chaincodeID 匹配(在 Example 鏈代碼中,該值為 hello)。 第 13 行:id 值用于協(xié)調(diào)請(qǐng)求。本教程不需要過(guò)多地考慮它,但要注意的是,在響應(yīng)中始終會(huì)發(fā)送回該值(參見(jiàn)下一個(gè)清單)。 提交此請(qǐng)求時(shí),JSON 輸出應(yīng)如下所示:
{ "jsonrpc": "2.0", "result": { "status": "OK", "message": "hello" }, "id": 1 }
圖 6 給出了 SoapUI 中的輸出的屏幕截圖。JSON 輸出消息會(huì)顯示在輸出選項(xiàng)卡中,該選項(xiàng)卡位于請(qǐng)求編輯器的右側(cè)。
終端窗口中的網(wǎng)絡(luò)日志輸出應(yīng)包含以下行:
. . vp0_1 | 20:48:39.482 [rest] ProcessChaincode -> INFO 0c4 REST processing chaincode request... vp0_1 | 20:48:39.482 [rest] processChaincodeDeploy -> INFO 0c5 REST deploying chaincode... vp0_1 | 20:48:39.483 [devops] Deploy -> DEBU 0c6 Creating deployment transaction (hello) vp0_1 | 20:48:39.483 [devops] Deploy -> DEBU 0c7 Sending deploy transaction (hello) to validator vp0_1 | 20:48:39.483 [peer] sendTransactionsToLocalEngine -> DEBU 0c8 Marshalling transaction CHAINCODE_DEPLOY to send to local engine vp0_1 | 20:48:39.483 [peer] sendTransactionsToLocalEngine -> DEBU 0c9 Sending message CHAIN_TRANSACTION with timestamp seconds:1487796519 nanos:483661510 to local engine vp0_1 | 20:48:39.483 [consensus/noops] RecvMsg -> DEBU 0ca Handling Message of type: CHAIN_TRANSACTION vp0_1 | 20:48:39.483 [consensus/noops] broadcastConsensusMsg -> DEBU 0cb Broadcasting CONSENSUS vp0_1 | 20:48:39.483 [peer] Broadcast -> DEBU 0cc Broadcast took 1.135s vp0_1 | 20:48:39.483 [consensus/noops] RecvMsg -> DEBU 0cd Sending to channel tx uuid: hello vp0_1 | 20:48:39.483 [rest] processChaincodeDeploy -> INFO 0ce Successfully deployed chainCode: hello vp0_1 | 20:48:39.484 [rest] ProcessChaincode -> INFO 0cf REST successfully deploy chaincode: {"jsonrpc":"2.0","result":{"status":"OK","message":"hello"},"id":1} . .
第 3-4 行顯示了輸出,表明網(wǎng)絡(luò)已收到部署消息,并且該結(jié)構(gòu)正在部署鏈代碼。第 13-14 行表明鏈代碼已成功部署。
在運(yùn)行鏈代碼的終端窗口中,可以注意到以下輸出:
$ ./build/distributions/Example/bin/Example Hello world! starting [Ljava.lang.String;@7ef20235 Feb 22, 2017 2:44:43 PM example.Example main INFO: starting Feb 22, 2017 2:44:43 PM org.hyperledger.java.shim.ChaincodeBase newPeerClientConnection INFO: Inside newPeerCLientConnection Feb 22, 2017 2:44:43 PM io.grpc.internal.TransportSet$1 call INFO: Created transport io.grpc.netty.NettyClientTransport@46adccd3(/127.0.0.1:7051) for /127.0.0.1:7051 Feb 22, 2017 2:44:48 PM io.grpc.internal.TransportSet$TransportListener transportReady INFO: Transport io.grpc.netty.NettyClientTransport@46adccd3(/127.0.0.1:7051) for /127.0.0.1:7051 is ready Feb 22, 2017 2:48:40 PM example.Example run INFO: In run, function: Feb 22, 2017 2:48:40 PM example.Example run
我包含了所有上下文輸出,在向區(qū)塊鏈網(wǎng)絡(luò)發(fā)送部署消息時(shí),您會(huì)看到類似第 11-13 行的消息。
最后,將會(huì)調(diào)用 hello 方法,可以看到它會(huì)在運(yùn)行鏈代碼的終端窗口的日志消息中顯示出來(lái)。
在 SoapUI 中的 chaincode 資源下,右鍵單擊 Method 1 并選擇 Clone Method。將該方法命名為 Invoke,然后單擊 OK。打開(kāi)新的 Invoke 方法下的 Request 1,并粘貼到以下 JSON 請(qǐng)求中:
{ "jsonrpc": "2.0", "method": "invoke", "params": { "type": 1, "chaincodeID":{ "name": "hello" }, "CtorMsg": { "args": ["hello"] } }, "id": 2 }
運(yùn)行該請(qǐng)求時(shí),會(huì)看到以下 JSON 響應(yīng):
{ "jsonrpc":"2.0", "result": { "status":"OK", "message":"1c1811d0-a958-4c58-ab1d-e1df550c18a3" }, "id":2 }
圖 7 給出了 SoapUI 中的輸出的屏幕截圖
網(wǎng)絡(luò)日志輸出應(yīng)包含以下行:
. . vp0_1 | 21:44:35.143 [rest] ProcessChaincode -> INFO 555 REST processing chaincode request... vp0_1 | 21:44:35.143 [rest] processChaincodeInvokeOrQuery -> INFO 556 REST invoke chaincode... vp0_1 | 21:44:35.143 [devops] invokeOrQuery -> INFO 557 Transaction ID: 1c1811d0-a958-4c58-ab1d-e1df550c18a3 vp0_1 | 21:44:35.143 [devops] createExecTx -> DEBU 558 Creating invocation transaction (1c1811d0-a958-4c58-ab1d-e1df550c18a3) vp0_1 | 21:44:35.143 [devops] invokeOrQuery -> DEBU 559 Sending invocation transaction (1c1811d0-a958-4c58-ab1d-e1df550c18a3) to validator vp0_1 | 21:44:35.143 [peer] sendTransactionsToLocalEngine -> DEBU 55a Marshalling transaction CHAINCODE_INVOKE to send to local engine vp0_1 | 21:44:35.143 [peer] sendTransactionsToLocalEngine -> DEBU 55b Sending message CHAIN_TRANSACTION with timestamp seconds:1487799875 nanos:143438691 to local engine vp0_1 | 21:44:35.143 [consensus/noops] RecvMsg -> DEBU 55c Handling Message of type: CHAIN_TRANSACTION vp0_1 | 21:44:35.143 [consensus/noops] broadcastConsensusMsg -> DEBU 55d Broadcasting CONSENSUS vp0_1 | 21:44:35.143 [peer] Broadcast -> DEBU 55e Broadcast took 1.249s vp0_1 | 21:44:35.143 [consensus/noops] RecvMsg -> DEBU 55f Sending to channel tx uuid: 1c1811d0-a958-4c58-ab1d-e1df550c18a3 vp0_1 | 21:44:35.143 [rest] processChaincodeInvokeOrQuery -> INFO 560 Successfully submitted invoke transaction with txid (1c1811d0-a958-4c58-ab1d-e1df550c18a3) vp0_1 | 21:44:35.143 [rest] ProcessChaincode -> INFO 561 REST successfully submitted invoke transaction: {"jsonrpc":"2.0","result":{"status":"OK","message":"1c1811d0-a958-4c58-ab1d-e1df550c18a3"},"id":2} . .
鏈代碼日志輸出如下所示:
$ ./build/distributions/Example/bin/Example Hello world! starting [Ljava.lang.String;@7ef20235 Feb 22, 2017 3:26:57 PM example.Example main INFO: starting Feb 22, 2017 3:26:57 PM org.hyperledger.java.shim.ChaincodeBase newPeerClientConnection INFO: Inside newPeerCLientConnection Feb 22, 2017 3:26:57 PM io.grpc.internal.TransportSet$1 call INFO: Created transport io.grpc.netty.NettyClientTransport@765e4953(/127.0.0.1:7051) for /127.0.0.1:7051 Feb 22, 2017 3:27:02 PM io.grpc.internal.TransportSet$TransportListener transportReady INFO: Transport io.grpc.netty.NettyClientTransport@765e4953(/127.0.0.1:7051) for /127.0.0.1:7051 is ready Feb 22, 2017 3:27:24 PM example.Example run INFO: In run, function: Feb 22, 2017 3:27:24 PM example.Example run SEVERE: No matching case for function: Feb 22, 2017 3:30:55 PM example.Example run INFO: In run, function:hello hello invoked
感謝各位的閱讀!關(guān)于“Hyperledger Fabric中如何部署并運(yùn)行Java鏈代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。