使用 Hyperledger Fabric SDK 開發應用程式
Hyperledger Fabric SDK 現在已不再使用。不過,您可以安裝並使用 Hyperledger Fabric SDK 來開發 Oracle Blockchain Platform 的應用程式。
Oracle Blockchain Platform 提供的 REST API 在建立時具有最大彈性;您可以呼叫交易、呼叫查詢或檢視交易狀態。請參閱 Oracle Blockchain Platform 的 REST API 。
不過,此彈性表示您可能會想要包裝應用程式中的現有 API 端點,以提供物件層級控制。應用程式可以包含更多微點的作業。
SDK 版本
提供多個版本的 Hyperledger Fabric SDK。使用與您執行處理所依據之 Hyperledger Fabric 版本相符的 SDK 版本。
安裝 Node.js 版的 Hyperledger Fabric SDK
如需如何使用 Fabric SDK for Node.js 的相關資訊,請參閱下列網址:Hyperledger Fabric SDK for Node.js 文件
在開發人員工具頁籤上,開啟應用程式開發窗格。您可以使用此頁籤上的連結來安裝 Hyperledger Fabric Node.js SDK。
安裝 Java 適用的 Hyperledger Fabric SDK
如需如何使用 Fabric SDK for Java 的相關資訊,請參閱下列網址:Hyperledger Fabric SDK for Java 文件
- 您可以使用此頁籤上的連結來安裝 Hyperledger Fabric Java SDK。
- (Hyperledger Fabric v2.x) 您必須依照更新 Hyperledger Fabric Java SDK 以使用 Oracle Blockchain Platform 的指示來修改 SDK 以與 Oracle Blockchain Platform 搭配使用。
安裝建置工具,例如 Apache Maven。
建構應用程式
建構您的 Java 應用程式,類似下列範例:
/Application
/artifacts
/cypto
/orderer
Contains the certificates required for the application to act on the orderer node
In participant instances only contains TLS certificates
/peer
Contains the certificates required for the application to act on the peer node
/src
chaincode.go if installing and deploying chaincode to the blockchain
/java
pom.xml or other build configuration files
/resources
Any resources used by the Java code, including artifacts such as the endorsement policy yaml file and blockchain configuration properties
/src
Java source files建構類似下列範例的 Node.js 應用程式:
/Application
/artifacts
/cypto
/orderer
Contains the certificates required for the application to act on the orderer node
In participant instances only contains TLS certificates
/peer
Contains the certificates required for the application to act on the peer node
/src
chaincode.go if installing and deploying chaincode to the blockchain
/node
package.json file
application.js
/app
Any javascript files called by the application
/tools
執行應用程式
您現在可以執行並測試應用程式。除了應用程式傳回的任何狀態訊息之外,您還可以在 Oracle Blockchain Platform 主控台中查看分類帳,以查看您的變更:
- 移至主控台的通道頁籤,然後尋找並按一下執行鏈碼的通道名稱。
- 在通路的分類帳窗格中,檢視鏈碼的分類帳摘要。
更新 Hyperledger Fabric Go SDK 以與 Oracle Blockchain Platform 搭配運作
您必須更新 Hyperledger Fabric Go SDK 中的 NormalizeURL 函數,才能將它與 Kubernetes 版本的 Oracle Blockchain Platform 搭配使用。
- 開啟 Hyperledger Fabric Go SDK 中的
client.go檔案進行編輯:vendor/github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib/client.go - 更新
NormalizeURL函數,如下列程式碼所示。編輯前的NormalizeURL函數:// NormalizeURL normalizes a URL (from cfssl) func NormalizeURL(addr string) (*url.URL, error) { addr = strings.TrimSpace(addr) u, err := url.Parse(addr) if err != nil { return nil, err } if u.Opaque != "" { u.Host = net.JoinHostPort(u.Scheme, u.Opaque) u.Opaque = "" } else if u.Path != "" && !strings.Contains(u.Path, ":") { u.Host = net.JoinHostPort(u.Path, "") u.Path = "" } else if u.Scheme == "" { u.Host = u.Path u.Path = "" } if u.Scheme != "https" { u.Scheme = "http" } . . .編輯後的NormalizeURL函數:// NormalizeURL normalizes a URL (from cfssl) func NormalizeURL(addr string) (*url.URL, error) { addr = strings.TrimSpace(addr) u, err := url.Parse(addr) if err != nil { return nil, err } if u.Opaque != "" { u.Host = net.JoinHostPort(u.Scheme, u.Opaque) u.Opaque = "" } else if u.Host != "" && !strings.Contains(u.Host, ":") { u.Host = net.JoinHostPort(u.Host, "") u.Path = "" } else if u.Scheme == "" { u.Host = u.Path u.Path = "" } . . . - 儲存
client.go檔案。
更新 Hyperledger Fabric Java SDK 以與 Oracle Blockchain Platform 搭配運作
OCI 基礎架構元件與 Hyperledger Fabric v2.x 提供的 Java SDK 之間不相容。您必須更新 SDK,才能與 Oracle Blockchain Platform 搭配使用。
更新 Hyperledger Fabric SDK 的方法
有兩種更新 SDK 的方式:
- 從主控台下載修改過的套裝軟體。我們已建立更新的
grpc-netty-shaded-1.38.0.jar檔案,這是 Java SDK 所參照的模組,需要修改。 - 手動建置套裝軟體,如下列步驟所述。
若要下載 grpc-netty-shaded-1.38.0.jar 檔案,請按一下主控台的開發人員工具頁籤,然後選取應用程式開發窗格。
手動建立套裝軟體
對於 fabric-sdk-java 專案,請完成下列步驟來重建 grpc-netty-shaded 套件,以將對等與訂購者與 grpcs 用戶端連線 (透過 TLS)。grpc-netty-shaded 套裝軟體是 grpc-java 的子專案。
-
安裝專案相依性 :
mvn install -
下載
grpc-java原始碼:git clone https://github.com/grpc/grpc-java.git - 尋找您的
fabric-sdk-java專案使用的grpc版本,並查看程式碼。在grpc-java目錄中,取出fabric-sdk-java專案使用的grpc版本:cd grpc-java && git checkout v1.38.0 -
變更程式碼以避免伺服器端發生
alpn錯誤。使用下列內容建立grpc-java-patch修補檔案:diff --git a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java index 19d3e01b7..ebc4786a3 100644 — a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java +++ b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java @@ -611,7 +611,8 @@ final class ProtocolNegotiators { SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt; if (handshakeEvent.isSuccess()) { SslHandler handler = ctx.pipeline().get(SslHandler.class); if (sslContext.applicationProtocolNegotiator().protocols() + if (handler.applicationProtocol() == null + || sslContext.applicationProtocolNegotiator().protocols() .contains(handler.applicationProtocol())) { // Successfully negotiated the protocol. logSslEngineDetails(Level.FINER, ctx, "TLS negotiation succeeded.", null); -
套用修正程式:
git apply grpc-java.patch -
建置專案以產生目標修正的套裝程式。使用 Gradle 建立
grpc-java-shaded專案:cd netty/shaded gradle build -PskipAndroid=true -PskipCodegen=true組建完成之後,目標修正的
.jar套裝程式可從grpc-java/netty/build/libs/grpc-netty-shaded-1.38.0.jar取得。 -
將修正過的套裝程式新增至您的 Maven 本機儲存區域。
以已修補的套裝軟體取代原始的grpc-netty-shaded.jar套裝軟體,方法如下:-
使用 Maven 依本機檔案安裝套裝程式:
您必須將目標mvn install:install-file -Dfile=grpc-netty-shaded-build/grpc-netty-shaded-1.38.0.jar -DgroupId=io.grpc -DartifactId=grpc-netty-shaded -Dversion=1.38.0 -Dpackaging=jargroupid、artifactid和version與您要取代的套裝軟體保持相同。 -
手動取代您的套裝軟體。移至本機 Maven 儲存區域,尋找目標套裝程式所在的目錄,然後以修正的套裝程式取代套裝程式。
-
-
執行專案。