方案:运行链代码即服务

以下示例说明如何将 FabCar 链代码作为外部服务(Kubernetes 集群中的 pod)运行,然后您可以将其部署到 Oracle Blockchain Platform

在完成以下步骤之前,请在 Oracle Blockchain Platform 中将链代码部署为外部程序包。

  1. 在包含 fabcar/externalhyperledger/fabric-samples 系统信息库中克隆 GitHub 目录。该目录包含 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 的 Go 映像的基本映像,创建目录并将该目录设置为工作目录,将本地内容复制到映像中,提取并安装 Go 相关项,编译 Go 代码,将容器配置为侦听端口 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 云池和服务。
    kubectl apply -f deployment.yaml

现在,您可以从 Oracle Blockchain Platform 运行使用外部链代码的事务处理。