案例:執行鏈碼即服務

下列範例說明以外部服務 (Kubernetes 叢集中的 Pod) 方式執行 FabCar 鏈碼,然後部署至 Oracle Blockchain Platform

完成下列步驟之前,請先在 Oracle Blockchain Platform 中將鏈碼部署為外部套件。

  1. 複製 hyperledger/fabric-samples 儲存庫中包含 fabcar/externalGitHub 目錄。目錄包含 Go、fabcar.goDockerfile 檔案中 FabCar 鏈碼的外部版本。忽略 metadata.jsonchaincode.env.example 檔案。
  2. 如下列範例所示,修改 Dockerfile 檔案:
    # Use the Go image from Docker Hub with specified versions for Go and Alpine Linux
    FROM golang:1.13.8-alpine3.10
    
    # Create a directory named "chaincode" in the image
    RUN mkdir /chaincode
    
    # Set "/chaincode" as the working directory within the image
    WORKDIR /chaincode
    
    # Copy the contents of the local directory into "/chaincode" in the image
    COPY . .
    
    # Fetch and install Go dependencies for the project
    RUN go get -d -v ./...
    
    # Install Go dependencies
    RUN go install -v ./...
    
    # Compile the Go code in the current directory, creating an executable binary named "fabcar"
    RUN go build -o fabcar .
    
    # Expose port 9999, indicating that the application within the container will listen on this port
    EXPOSE 9999
    
    # Set the default command to run the "fabcar" executable when the container starts
    CMD ["/chaincode/fabcar"]

    此命令順序設定使用 Go 和 Alpine Linux 特定版本建立新映像檔的基礎映像檔,建立一個目錄,將該目錄設為工作目錄,將本機內容複製到影像、擷取並安裝「執行」相依性、編譯「執行」程式碼、設定容器以監聽連接埠 9999,以及設定預設命令以執行 fabcar 執行檔。

  3. 執行下列命令以建立 Docker 映像檔,此映像檔會建立名為 fabcar-sample 的 Docker 映像檔,然後將其植入儲存區域。
    docker build -t fabcar-sample .
  4. 建立 Kubernetes 部署 YAML 檔案,以執行包含鏈碼的 Docker 映像檔。下列範例顯示 deployment.yaml 檔案。提取 Docker 映像檔的儲存區域為 repository.example.org。用於提取映像檔的 Kubernetes 加密密碼名稱為 ocirsecretCHAINCODE_SERVER_ADDRESS 值為 0.0.0.0:9999,表示伺服器設定為接受來自與電腦相關之任何 IP 位址的連接埠 9999 上的內送連線。選取外部鏈碼類型並上傳 .zip 檔案,從您在 Oracle Blockchain Platform 的對等節點上部署時所顯示的鏈碼套裝程式 ID 取得 CHAINCODE_ID 值。在 Oracle Blockchain Platform UI 中,鏈碼 ID 顯示為 chaincode_namepackage_ID。對於 Java 鏈碼,鏈碼 ID 變數稱為 CORE_CHAINCODE_ID_NAME
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: external-fabcar-node
    namespace: chaincode
    labels:
    chaincode-type: external
    chaincode-name: external-fabcar-node
    spec:
    replicas: 1
    selector:
    matchLabels:
    chaincode-type: external
    chaincode-name: external-fabcar-node
    template:
    metadata:
    labels:
    chaincode-type: external
    chaincode-name: external-fabcar-node
    spec:
    imagePullSecrets:
    - name: ocirsecret
    containers:
    - env:
    - name: CHAINCODE_SERVER_ADDRESS
    value: "0.0.0.0:9999"
    - name: CHAINCODE_ID
    value: "ext-fabcar-node:7f953544f26124914c29467d5079f601ddefa53375fffec830ecd70c13547877"
    image: repository.example.org/blockchain/fabcar-sample:latest
    imagePullPolicy: Always
    name: assettransfer
    ports:
    - containerPort: 9999
    name: grpc
    protocol: TCP
    
    
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
    labels:
    chaincode-name: external-fabcar-node
    chaincode-type: external
    name: external-fabcar-node
    namespace: chaincode
    spec:
    ports:
    - name: grpc
    port: 9999
    protocol: TCP
    targetPort: 9999
    selector:
    chaincode-name: external-fabcar-node
    chaincode-type: external
    type: ClusterIP
  5. 請執行下列命令,在 chaincode 命名空間中產生 Kubernetes 密碼。若為 name_of_secret,請指定 deployment.yaml 檔案的加密密碼。在此範例中,加密密碼的名稱是 ocirsecret
    kubectl create secret docker-registry name_of_secret -n chaincode --docker-server='repository.example.org/blockchain' --docker-username='your_username' --docker-password='auth_token'
  6. 執行下列命令以部署 Kubernetes Pod 和服務。
    kubectl apply -f deployment.yaml

您現在可以從 Oracle Blockchain Platform 執行使用外部鏈碼的交易。