在IBM Bluemix上部署Hyperledger應用


簡介

IBM Bluemix (http://www.ibm.com/bluemix‎)是一個基於cloud的應用開發和部署平台,提供包括多種服務和運行環境的支持。對Hyperledger應用開發者而言,IBM Bluemix提供免費的入門級區塊運行環境。本文介紹了在IBM Bluemix上部署Hyperledger應用的基本過程。

第一步: 在 Bluemix中選擇區塊服務

登錄Bluemix后,在Catalog頁面中選擇Application Services下的Blochain服務。

確認區塊服務的名稱,然后點擊Create按鈕以創建區塊服務。

在新建的區塊服務頁面上點擊LAUNCH按鈕以啟動區塊服務,並顯示其具體信息。

第二步: 在 Bluemix區塊服務中注冊用戶

首先在區塊服務頁面中選擇 APIs, 然后展開Network's Enroll IDs列表以獲取預置的用戶名和密碼列表 。

然后向選定的區塊節點發送如下請求以注冊用戶

    POST https://e1bb641577f249a88acca04da0272249-vp3.us.blockchain.ibm.com:444/registrar
    Content-Type: application/json

    {
        "enrollId": "admin",
        "enrollSecret": "19725f3d88"
    }

注冊成功的響應為

  {
    "OK": "Login successful for user 'admin'."
  }    

第三步: 在 Bluemix區塊服務中發布應用代碼, 前提是應用的GoLang代碼必須在github.com上。

    POST https://e1bb641577f249a88acca04da0272249-vp3.us.blockchain.ibm.com:444/chaincode
    Content-Type: application/json

    {
        "jsonrpc": "2.0",
        "method": "deploy",
        "params": {
            "type": 1,
            "chaincodeID": {
                "path": "https://github.com/huyouhengbc/Hyperledger/HelloWorld"
            },
            "ctorMsg": {
                "function": "init",
                "args": [
                ]
            },
            "secureContext": "admin"
        },
        "id": 1
    }

發布成功的響應為

    {
        "jsonrpc": "2.0",
        "result": {
            "status": "OK",
            "message": "e2623e76371a16ca86d97bfb90bdab95885939bf4f2fa40133d65bca4aace27eaf94d8cd10438b80de097442916f6bfef87fb45a6cd678e3cb6ec0bd8dd008c2"
        },
        "id": 1
    }

第四步: 測試應用代碼,

Query請求

    POST https://e1bb641577f249a88acca04da0272249-vp3.us.blockchain.ibm.com:444/chaincode
    Content-Type: application/json
    {
        "jsonrpc": "2.0",
        "method": "query",
        "params": {
            "type": 1,
            "chaincodeID": {
                "name": "e2623e76371a16ca86d97bfb90bdab95885939bf4f2fa40133d65bca4aace27eaf94d8cd10438b80de097442916f6bfef87fb45a6cd678e3cb6ec0bd8dd008c2"
            },
            "ctorMsg": {
                "function": "hello",
                "args": [
                ]
            },
            "secureContext": "admin"
        },
        "id": 3
    }

Query響應

    200 OK
    access-control-allow-headers:  accept, content-type
    Access-Control-Allow-Origin:  *
    Content-Type:  application/json
    Date:  Sun, 30 Oct 2016 05:13:58 GMT
    Content-Length:  73
    Server:  nghttpx nghttp2/1.14.0-DEV
    Via:  1.1 nghttpx
    X-Firefox-Spdy:  h2

    {"jsonrpc":"2.0","result":{"status":"OK","message":"Hello World"},"id":3}

Invoke請求

    POST https://e1bb641577f249a88acca04da0272249-vp3.us.blockchain.ibm.com:444/chaincode
    Content-Type: application/json
    {
        "jsonrpc": "2.0",
        "method": "invoke",
        "params": {
            "type": 1,
            "chaincodeID": {
                "name": "e2623e76371a16ca86d97bfb90bdab95885939bf4f2fa40133d65bca4aace27eaf94d8cd10438b80de097442916f6bfef87fb45a6cd678e3cb6ec0bd8dd008c2"
            },
            "ctorMsg": {
                "function": "hello",
                "args": [
                ]
            },
            "secureContext": "admin"
        },
        "id": 3
    }

Invoke響應

    200 OK
    access-control-allow-headers:  accept, content-type
    Access-Control-Allow-Origin:  *
    Content-Type:  application/json
    Date:  Sun, 30 Oct 2016 05:13:22 GMT
    Content-Length:  98
    Server:  nghttpx nghttp2/1.14.0-DEV
    Via:  1.1 nghttpx
    X-Firefox-Spdy:  h2

    {"jsonrpc":"2.0","result":{"status":"OK","message":"d74495ff-f3d3-44ba-acc6-5fbc767bbf21"},"id":3}

后記

  • 發布應用代碼中的path指向的是GoLang代碼所在的目錄,而不是GoLang源碼文件, 錯誤響應如下:
    POST  https://e1bb641577f249a88acca04da0272249-vp0.us.blockchain.ibm.com:444/chaincode
    Content-Type: application/json
        {
            "jsonrpc": "2.0",
            "method": "deploy",
            "params": {
                "type": 1,
                "chaincodeID": {
                    "path": "https://github.com/huyouhengbc/Hyperledger/blob/master/HelloWorld/HelloWorld.go"
                },
                "ctorMsg": {
                    "function": "init",
                    "args": [
                    ]
                },
                "secureContext": "user_type8_0"
            },
            "id": 1
        }
    -- response --
    200 OK
    access-control-allow-headers:  accept, content-type
    Access-Control-Allow-Origin:  *
    Content-Type:  application/json
    Date:  Sun, 30 Oct 2016 04:00:14 GMT
    Content-Length:  721
    Server:  nghttpx nghttp2/1.14.0-DEV
    Via:  1.1 nghttpx
    X-Firefox-Spdy:  h2

    {"jsonrpc":"2.0","error":{"code":-32001,"message":"Deployment failure","data":"Error when deploying chaincode: Error getting chaincode package bytes: Error getting code 'go get' failed with error: 'exit status 1'\npackage github.com/huyouhengbc/Hyperledger/blob/master/HelloWorld/HelloWorld.go: cannot find package 'github.com/huyouhengbc/Hyperledger/blob/master/HelloWorld/HelloWorld.go' in any of:\n\t/usr/local/go/src/github.com/huyouhengbc/Hyperledger/blob/master/HelloWorld/HelloWorld.go (from $GOROOT)\n\t/go/_usercode_/234208637/src/github.com/huyouhengbc/Hyperledger/blob/master/HelloWorld/HelloWorld.go (from $GOPATH)\n\t/go/src/github.com/huyouhengbc/Hyperledger/blob/master/HelloWorld/HelloWorld.go\n"},"id":1}
  • Bluemix提供基於基於Hyperledger Fabric v0.5的服務,如果使用最新的Hyperledger Fabric接口, 發布應用時錯誤響應如下:
    {
        "jsonrpc": "2.0",
        "error": {
            "code": -32001,
            "message": "Deployment failure",
            "data": "Error when deploying chaincode: Error getting chaincode package bytes: Error getting code 'go get' failed with error: 'exit status 2'\n# github.com/huyouhengbc/Hyperledger/HelloWorld\n/go/_usercode_/730749822/src/github.com/huyouhengbc/Hyperledger/HelloWorld/HelloWorld.go:15: undefined: shim.ChaincodeStubInterface\n/go/_usercode_/730749822/src/github.com/huyouhengbc/Hyperledger/HelloWorld/HelloWorld.go:21: undefined: shim.ChaincodeStubInterface\n/go/_usercode_/730749822/src/github.com/huyouhengbc/Hyperledger/HelloWorld/HelloWorld.go:27: undefined: shim.ChaincodeStubInterface\n"
        },
        "id": 1
    }
  • 發布應用報錯
    {
        "jsonrpc": "2.0",
        "error": {
            "code": -32001,
            "message": "Deployment failure",
            "data": "Error when deploying chaincode: Failed loading TCerts from TCA"
        },
        "id": 1
    }

解決方法: 在 Demo Chaincoded下發布示例應用,選擇Show API details輸出中使用的節點。

總結

本文介紹了在IBM Bluemix上部署Hyperledger應用的基本過程。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM