Block chain code __hyperledger for Java developers

Source: Internet
Author: User
Tags define local docker hub docker compose docker run soapui hyperledger fabric

a brief introduction to the chain code for Java developers

Click to view the video demo to view the transcript

You may have heard of the chunk chain, but you may not be sure what it does for Java™ developers. This tutorial will help you to dispel the confusion. I'll show you how to use the hyperledger Fabric v0.6 to build, run, and execute a smart contract or chain code written in the Java language. You will install tools, define local block chain networks, and build and run a chain code intelligence contract.

For an overview of block chains, see "What is the chunk chain" in the DeveloperWorks blog. The introduction of distributed ledger technology.

Get the best free tools, training and monthly collection of community resources to help you get started with block chains.
Latest Issue | Subscribe to Prerequisite Conditions

This tutorial assumes that you have the following prerequisites: I heard that the block chain or Hyperledger Fabric framework has a medium level of Java programming knowledge, as well as familiarity with or (preferably) proficient in the use of the language and platform: Eclipse IDE Docker and Docker Com Pose gradle Linux command line soapui or another HTTP client software, such as Postman

You should also be able to install the software on your computer with minimal guidance. Due to space limitations, I will not introduce the detailed installation instructions for all the necessary software, and the website providing you with the software should provide installation instructions.

Before I go into this tutorial, I want to say a little about the block chain. Block Chain Foundation

Although there is a lot of hype about the block chain, it's worth it. The technology is not only interesting in itself, but it is disruptive and has the potential to revolutionize the way business is implemented on the Internet.

block chain developers recommend reading content

Improve your skills with the help of DeveloperWorks tutorials, courses, blogs, and community support within the block chain Developer Center .

How to change. Let's think about the basic attributes of a successful business transaction: Trust: We have reached an agreement, but can I really believe that you will follow it (or respect me)? Transparency: allows you to view "behind-the-scenes situations" (which can both build trust and reduce the need for trust). Accountability: used to define conditions that determine whether all parties consider the agreement to be complied with.

The health of any business relationship between two or more parties corresponds to the different levels of the above 3 attributes (for example, more trust means less transparency and vice versa), but some of these properties must exist, or there will be problems.

"Block-chain technology is being quickly applied to software development projects around you." Are you ready for this? ”

How the block chain helps. First, by using a common framework, business partners can build a trust network ahead of time. The block chain then provides transparency by using a ledger that is visible to all counterparties. Finally, accountability is established by using the consensus of all Parties (in the form of smart contracts or chain codes).

What this means for Java developers.

The rapid development of the Hyperledger community and Hyperledger Fabric means that block-chain technology is rapidly being applied to software development projects around you. Are you ready for this? Development situation of block chain technology

Sometimes, development techniques can hinder the resolution of business problems. The main purpose of this tutorial is to show how to write Java chain code, so I chose the simplest development technology portfolio to do this.

That is, there are other options for the components in the group. In this tutorial, I will use Docker as the network container environment, and the other option is the vagrant with VirtualBox. If you have never used vagrant, you should at least try it.

Docker is a container environment, and vagrant uses virtualization. When used in conjunction with VIRTUALBOX, the virtualized environment has varying degrees of control over the computing environment, which is favored by some developers (making it ideal for fabric developers).

If you want to learn more about the pros and cons of container and virtualization, see the "Docker" on DeveloperWorks blog. Introduction to the benefits of the container for the application.

If a developer only wants to write code without having to worry about containers, virtualization, or any infrastructure, then you can choose Ibm®bluemix®. Although Bluemix supports running a complete IBM block chain network, it does not currently support the use of the Java language development chain code. This situation is expected to change soon, so keep your eye on it.

an ice sculpture formed by an avalanche.

Hyperledger Fabric is very fluent (euphemistically). You may often notice that some parts of the project stop working. Don't panic-this is a natural feature that will emerge in the early stages of technology development.

For example, if you notice that a link to a document in this tutorial is not valid, the content is still there-it may just be moved to a different place.

A similar problem may be encountered with evolving technologies. Being an early adopters means that you sometimes have to adapt to the situation, so just let it be.

If in your impression, block chain technology at that time the development situation is very unstable, you are right. However, this means that you start contacting the block chain and chain code at the right time (from the outset). As the technology matures, your investment in early-stage learning of the technology will continue to be rewarded handsomely.

Block chains are one of the disruptive technologies that can radically change the way everyone performs their business. Such technologies include not only business-to-business, but also consumer, and even c2c. This is really a very exciting moment.

Let's get started. setting up the development environment

To run the chain code, you first need to set up the development environment.

By the end of this section, you can run a Hyperledger Java chain code example, in which you will deploy and invoke transactions on the real-world chain code. Then I'll show you how (almost) to write a new chain code program from scratch.

setting up the development environment

Click to view the video demo to view the transcript

In this section, you will: set up a network environment--for running your local block chain network. Install build software-for building your chain code. Install an HTTP client-for invoking transactions on your chain code. Start the block chain network. Build Java Shim Client JAR.

To be honest, to write chain code, there are a lot of setup work to do. However, if you follow these instructions and work a little harder, your effort will be worthwhile. 1 setting up the network environment

This tutorial runs a local block chain network using Docker and a pre-built block chain network component image from the Docker Hub. If you want, you can build fabric from scratch (after all, it's open source), but at this stage it's easier to use the pre-built Hyperledger fabric mirrors provided in the Docker Hub.

As I mentioned in my introduction, another option (which you may have seen in the Hyperledger document) is to use vagrant and VirtualBox. Vagrant is a good choice for fabric developers, but as a chain-code developer, we are more concerned with the construction, running, and testing of the chain code than with the fabric itself.

If you have already installed Docker version 1.12 or later, you can skip to the next section ("Install build Software"). In the following instructions, assume that you have not installed Docker (that is, you are not upgrading from a previous Docker version). Docker Compose is also installed during the installation of Docker, which is used to define and run applications that require multiple containers, such as the local Hyperledger block chain network that will run in this tutorial. Install Docker

Here you can find installation instructions for MAC, Windows, and Linux:

Install Docker on Mac, Windows, and Linux to verify Docker installation

To test the Docker installation, open a terminal window (or a command prompt on Windows) and type the following command:

Docker-v docker-compose-v

You will get the following output:

$ docker-v Docker version 1.13.1, build 092cba3 $ docker-compose-v docker-compose version 1.11.1, build 7c5d5e4

If you want to see how the Docker actually works, you can run the Hello-world mirror as follows:

$ docker Run Hello-world unable to find image ' hello-world:latest ' locally latest:pulling from Library/hello-world 78445 Dd45222:pull Complete Digest:sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7 Status: Downloaded newer image for hello-world:latest   Hello from docker! This message shows the your installation appears to be working correctly.   to generate this message, Docker took the following steps:   1. The Docker client contacted the Docker daemon.   2. The Docker daemon pulled the "Hello-world" image from the Docker Hub.   3. The Docker daemon created a new container from this image which runs the      executable that produces The output you are currently reading.   4. The Docker daemon streamed that output to the Docker client, which sent it      to your.   to try something more ambitious, you can run a Ubuntu container with:   $ docker run-it Ubuntu Bash &nbsP Share images, automate workflows, and more with a free Docker ID:   https://cloud.docker.com/  For more example s and ideas, visit:   https://docs.docker.com/engine/userguide/
2 Installing the Build software

For the build system, Hyperledger Fabric uses gradle, and this tutorial will also use it. Gradle is a building automation system that combines the simple syntax of a specified build component with the best features of Apache Ant and Apache Maven, creating a powerful build system that is easy to use. It's not surprising that so many developers are switching their projects to Gradle. Please note that this article is using the Gradle 3.3 version. Using a different version of Gradle may cause the project to build up and fail.

You can learn more about gradle (and some of its high-profile users) on the Gradle home page. Install Gradle

To install Gradle, follow the instructions below:

Install Gradle on Mac, Windows, and Linux to verify Gradle installation

To verify the Gradle installation, open a terminal window and execute this command:

Gradle-v

You will see the following output:

$ gradle-v------------------------------------------------------------gradle 3.3-------------------------------- ----------------------------build time:2017-01-03 15:31:04 UTC revision:075893a3d0798c0c1f322899b41ceca82e4e134 b groovy:2.4.7 Ant:apache Ant (TM) version 1.9.6 compiled on June-2015 jvm:1.8.0_102 (oracl E Corporation 25.102-b14) os:mac OS X 10.12.3 x86_64
3 Installing HTTP clients

Next, install the HTTP client software, which allows the chain code to communicate with the REST interface of the Hyperledger block chain structure. Your browser can emit HTTP get, but to interact with fabric, you need to be able to send messages via POST. This means that you need an HTTP client.

The HTTP client I chose for this tutorial is SOAPUI, which provides a powerful, easy-to-use, free community version that contains many features. Install Soapui

To install SOAPUI, follow the instructions below:

Installing SOAPUI authentication soapui for Mac OS, Windows, and Linux

To confirm that SOAPUI is installed, you can start the application on your computer. On Mac OS, the Soapui Starter Page appears when you open Soapui, as shown in Figure 1. Figure 1. SOAPUI 4 boot block chain network on Mac OS X

Gopath

Hyperledger Fabric is written in go, and you often see the term gopath in the Hyperledger document, so you should be familiar with it if you plan to use Hyperledger to perform many chain code development efforts.

Gopath is the root directory of the GO environment. The source code, binaries, and other Golang packages are referenced by a relative path to this path.

Now that you have installed the software needed to develop and test the chain code, it is time to start the local block chain network. The first step is to define the configuration of the network.

First, create a directory that uses it as the root directory for all source code used in the chain code development process. In this tutorial, I'll use ~/home/mychaincode (or C:\home\chaincode on Windows).

Next, set the GOPATH environment variable to this path. We don't compile any go code, we don't build golang packages or other binaries, but Golang terminology is fused into hyperledger, so it's a good idea to be familiar with the go and Gopath way of thinking.

On Linux, execute the following command:

Export Gopath=~/home/mychaincode

Or, on Windows, you can use the following command:

SET Gopath=c:\home\mychaincode

Next, you must tell Docker Compose how to create and run a chunk-chain Peer-to-peer network. The network is defined using YAML and should be named Docker-compose.yml. You can name a file as a different name, but you must specify the-F flag when you start Docker Compose. It is recommended that you adhere to the default name, which is docker-compose.yml.

Create the Docker-compose.yml file in the Gopath root directory. Paste the following content:

MEMBERSRVC:    image:hyperledger/fabric-membersrvc    ports:     -" 7054:7054 "   command:membersrvc vp0:    image:hyperledger/fabric-peer:x86_64-0.6.0-preview    ports:     -"7050:7050"     -"7051:7051"    & nbsp -"7053:7053"    Environment:     -Core_peer_addressautodetect=true      -Core_vm_endpoint=unix:///var/run/docker.sock     -Core_logging_level=debug     -core_peer_id=vp0     -core_peer_pki_eca_paddr=membersrvc:7054     -core_peer_pki_tca_paddr=membersrvc:7054     -Core_peer_pki_tlsca_ paddr=membersrvc:7054     -Core_security_enabled=false     -core_ Security_enrollid=test_vp0     -CORE_SECURITY_ENROLLSECRET=MWYPMSRJUPBT   Links:     -membersrvc    command:sh-c "Sleep 5"; Peer node start--peer-chaincodedev "

There's a lot of content, and most of it's not part of the scope of this tutorial, but I'd like to explain a little bit about it. This file tells Docker Compose defines two services: MEMBERSRVC: A member Service node that provides member services, specifically a certificate issuing authority (CA) that handles all cryptographic decryption work (such as issuing and revoking certificates). Name the pre-build Docker mirror for this purpose as HYPERLEDGER/FABRIC-MEMBERSRVC. VP0: Individual authentication peer node in the network. For development purposes, we do not need to perform peer-to-peer network validation in a luxurious way, only one peer node is required. Name the pre-build Docker mirror for this purpose as Hyperledger/fabric-peer:x86_64-0.6.0-preview. Note that because the Fabric version is upgraded, omitting the colon and subsequent characters can cause a build error. The Fabric 0.6 version is specified here. Hyperledger/fabric-peer equal to Hyperledger/fabric-peer:latest. Some environment variables are set by the VP0 peer node. Please note that the Core_logging_level variable is set to DEBUG. This generates a large amount of output, which is sometimes convenient. However, if you want less output, you can change the level to INFO. See "Log Control" in the Hyperledger settings document for more information about log levels.

You can find more information about the Docker Compose YML file definition on the Docker Web site.

Next, note that the value of core_security_enabled is false. This means that fabric does not require you to send any type of end user credentials. Security is outside the scope of this tutorial, but if you are interested in more information, you can consult this security feature description based on your chain code request.

The last caveat: Any changes to the default settings (especially port values) for any of these values may cause the example in this tutorial to not work. A block chain network is a set of distributed software components that require precise and coordinated communication. It is highly recommended that you do not change the default value of a port value until you understand how all the components of the fabric interact.

When you have finished defining the block chain, you can start the local block chain network. To do this, run Docker Compose. Navigate to your $GOPATH and execute this command:

Docker-compose up

You will get the following output in the terminal window:

$ docker-compose up. . Pulling MEMBERSRVC (hyperledger/fabric-membersrvc:latest) ... latest:pulling from HYPERLEDGER/FABRIC-MEMBERSRVC. . status:downloaded newer image for Hyperledger/fabric-membersrvc:latest pulling vp0 (hyperledger/fabric-peer:latest) ... latest:pulling from Hyperledger/fabric-peer. . status:downloaded newer image for hyperledger/fabric-peer:latest creating Mychaincode_membersrvc_1 Creating Mychaincode_vp0_1 attaching to Mychaincode_membersrvc_1, Mychaincode_vp0_1 vp0_1          | 19:30:03.773 [Logging] logginginit-> DEBU 001 Setting Default logging level to DEBUG for command ' node ' VP0_1 &NB sp;       | 19:30:03.773 [Nodecmd] serve-> INFO 002 Running in Chaincode development mode ... vp0_1    &nbsp ;    | 19:30:04.146 [Peer] chatwithsomepeers-> Debu 07c starting up the "a" a new peer network Bsp;     | 19:30:04.146 [Consensus/statetransfer] verifyandrecoverblockchain-> debu 07d Validating existing blockchain, Highest validated block is 0, valid through 0 vp0_1         | 19:30:04.146 [Consensus/statetransfer] blockthread-> INFO 0
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.