Help Center > > Developer Guide> Demos> Java SDKs

Java SDKs

Updated at: Aug 30, 2019 GMT+08:00

This section provides a demo application that uses a Java SDK and supports the Chinese cryptographic algorithms.

Preparation

HUAWEI CLOUD BCS services are deployed on container clusters. To ensure access from external networks and prevent data from being lost, prepare the environment before deploying a BCS service. The preparation includes cluster creation, elastic IP address (EIP) binding, and Scalable File Service (SFS) file system creation for storage (in sequence).

Table 1 Preparation

Step

Action

Description

1

Create a cluster.

Create a CCE cluster of Kubernetes v1.9 or later, and name it javasdk-test.

NOTE:
  • For this demo application, you are advised to purchase a VM with 4 vCPUs and 8 GB memory when creating the cluster.
  • If you purchase and bind an EIP during cluster creation, skip step 2.

2

Bind an EIP to the ECS.

Apply for an elastic IP address (EIP) and bind it to an ECS so that the ECS can be accessed from the public network. The recommended bandwidth for the EIP is 5 Mbit/s.

3

Create an SFS file system.

After a container cluster is created, create a file storage system to store data. Use the default specifications when creating the system.

4

Install a development tool.

Install JDK 1.8 or a later version (64 bit), Maven, and Eclipse (Eclipse can be replaced by another IDE you prefer).

5

Download the project code and chaincode.

chaincode_example02.zip

javasdkdemo.zip

Procedure

  1. Buy a BCS service. The following table lists the service parameters.

    Table 2 Service parameters

    Parameter

    Setting

    Billing Mode

    Select Pay-per-use.

    Region

    Retain the default value.

    Service Name

    Enter java-sdk-demo.

    Edition

    Select Basic. (If Chinese cryptographic algorithms must be used, select Enterprise.)

    Blockchain Type

    Select either Private or Consortium.

    Cluster Type

    Select CCE cluster.

    Container Cluster

    Select an existing container cluster.

    Network Storage

    Select an existing SFS file system.

    Ledger Storage

    Select File database (goleveldb).

    Peer Organization

    Create a peer organization named organization with one peer.

    Consensus Mechanism

    Select Solo.

    Security Mechanism

    Select ECDSA.

    NOTICE:

    The Chinese cryptographic algorithms option is available. If this option is selected, certain configurations of the demo application must be modified. Pay attention to the following descriptions.

    Version

    Select the latest version.

    Blockchain Mgmt. Initial Password

    Set a password.

    Confirm Password

    Enter the password again.

    Enable Support for RESTful APIs

    Select No.

    Orderer Quantity

    Set it to 1.

    Channel Configuration

    Create a channel named channeldemo, and add the peer organization organization to the channel.

  2. Install and instantiate the chaincode.

    • The parameters for chaincode installation are as follows.
      Table 3 Parameters for installation

      Parameter

      Setting

      Chaincode Name

      Enter chaincode.

      Chaincode Version

      Enter 2.0.

      Organization & Peer

      Select peer-0.

      Language

      Select Golang.

      Chaincode File

      Add the downloaded chaincode package.

      Chaincode Description

      Fill in a description of the chaincode.

    • The parameters for chaincode instantiation are as follows.
      Table 4 Parameters for instantiation

      Parameter

      Setting

      Channel

      Enter channeldemo.

      Chaincode Version

      Enter 2.0.

      Initialization Function

      Enter init.

      Chaincode Parameters

      Enter a,400,b,1000.

      Endorsement Policy

      Select Endorsement from any of the following organizations.

      Endorsing Organizations

      Select organization.

  3. Configure the application.

    • Download certificates.

      Download the administrator certificates of the organizations java-sdk-demo-orderer and organizations on the Service Management page, and save them to the default local path.

    • Import the project.

      Right-click the Eclipse page, and choose Import from the shortcut menu to import the javasdkdemo file (Maven project) to the Eclipse.

    • Download the SDK configuration.

      Click More > Download SDK Configuration in the Operation column on the Service Management page.

      Set the SDK file parameters.

      Table 5 SDK file parameters

      Parameter

      Setting

      Chaincode Name

      Enter chaincode.

      NOTICE:

      The chaincode name and version must be the same as those specified for chaincode installation and instantiation.

      Chaincode Version

      Enter 2.0.

      Certificate Root Path

      Enter the path to the config folder of the demo project.

      NOTICE:

      Backslashes (\) in the path must be changed to slashes (/), for example, D:/javasdkdemo/config.

      Click Download. The downloaded file is java-sdk-demo-sdk-config.zip.

    • Copy and decompress the packages.

      Copy the downloaded organization-admin.zip, BCS service name-orderer-admin.zip, and BCS service name-Channel name-sdk-config.zip to the config directory of the demo project, and decompress the packages.

  4. Deploy the application.

    • Find the Main.java file in the /javasdkdemo/src/main/java/handler/ directory, and change the file path in the following code of the Main class to the absolute path of the java-sdk-demo-sdk-config.yaml file. Change the backslashes (\) to slashes (/).
      helper.setConfigCtx("E:/yourdir/huawei.yaml");

      If Chinese cryptographic algorithms is selected for the BCS service, add the dependency upon fabric-sdk-java-1.1.0-jar-with-dependencies.jar in the lib folder of the project to pom.xml because Chinese cryptographic algorithms need to refer to the fabric-sdk dependency package of Huawei. To add the dependency, remove the comments on the dependency as shown in the following figure.

    • Run the main function.

      Each execution of the main function represents a transaction in which user A transfers money (100) to user B, as shown in the following figure.

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel