This tutorial introduces Open Liberty, a light weight open source application server making REST requests to a blockchain network. You’ll discover exactly what a blockchain is by implementing a local blockchain network from scratch using the VS Code Blockchain extension, as well as starting the Open Liberty server all from VS code.
You’ll be able to hit endpoints for different functions from the Open Liberty server, and the blockchain network will return a response to the web browser. Experiencing how easy it is to start up a Blockchain Network as well as see how fast Open Liberty starts up as an application server and see some of the features included in Open Liberty for free!
- Java
- Git
- Maven
- Docker
- VS Code
Blockchain is a way of storing digital data. The data can literally be anything. For Bitcoin, it’s the transactions (logs of transfers of Bitcoin from one account to another), but it can even be files; it doesn’t matter. The data is stored in the form of blocks, which are linked (or chained) together using cryptographic hashes — hence the name “blockchain.”
In our instance, we are using cars, and on our ledger we are going to make transactions to the ledger in the form of cars.
Open Liberty is a Java application server. Put it simply, it hosts Java applications which you can access on a web browser. This is lightweight, free and allows you to make your own Java applications that you want to put on the web. The benefit of Open Liberty is you can have as much or as little as you want in terms of additional features on your web server. In terms of popularity its predecessor WebSphere Application Server, 20071 companies use it according to enlyfit.
-
Test that the server is running
-
Query all items from the ledger
-
Query specific items from the ledger
-
Add Cars to the ledger.
- If you dont already, Install Visual Studio Code.
- Go to the extensions Marketplace and search for IBM Blockchain
- Install the IBM Blockchain Platform extension
- After installation, if you need any additional pre-reqs, the extension will guide you through installing them. Make sure you pick up the Docker pre-reqs, as they'll be used to create your Fabric network
- Go to the extensions Marketplace on VS code and search for "Open Liberty Dev Dashboard"
- Install the Open Liberty Dev Dashboard plugin
- Go to the "IBM Blockchain Platform" view in VS Code by clicking the IBP icon
- Hover over the "Fabric Environments" section title and click the + icon
- Choose "Add a new local network (from template)"
- Choose the 2 Org template
- Wait for your network to spin up!
- Make your way back to the IBM Blockchain Platform homepage on VS Code. This can be done by clicking the IBP icon
-
Pick FabCar from the
explore sample code
section. -
Click the clone button to git clone the sample code for the FabCar sample
- You can pick whichever language you prefer to open the smart contract in however, because Open Liberty is a Java application server choose Java
This is a pre-configured smart contract already for you. Clone the Github repository for the Java
Fabcar
smart contract sample
- Connect to the Fabric Environment you created earlier
- Under Smart Contracts > Instantiated, click
+Instantiate
- Choose the folder you cloned from GitHub
- If asked which peers to install on, pick them all
- Accept the defaults on any other prompts (most things can be left blank and just hit Enter for a simple deployment like this)
For Open Liberty to communicate to the Blockchain Network, Hyperledger Fabric has security features, which stop applications attempting to make transactions unless you have the specific Profiles.
Export the Local Fabric Gateways
to do this right click on 1 org local fabric - org 1
and export and click Export connection profile. The finder
window will open and save the json
file as 1-Org-Local-Fabric-Org1_connection.json
in the target/liberty/wlp/usr/servers/defaultServer
directory.
Export the Fabric Wallets
by clicking on the 1 Org Local Fabric - Orderer Wallet
and right clicking the Export Wallet. Save the file as Local-Fabric-Org1_connection.json
in the target/liberty/wlp/usr/servers/defaultServer
directory.
As we installed the Dev Tool
for Open Liberty click on the Liberty Dev Dashboard
icon and the extension will display the project. As the artifact-Id
specified in the pom.xml
where our server retrieves the dependincies for the server to run, it is called application-server
Right click on application-server
and hit Start
. This will start the application server up very quickly. Usually within 2 - 5 seconds!
The server has started in Development mode
meaning it will show the command line and if you hit enter
it will run tests onto the Server on demand.
Open up a browser of your choice, Chrome is usually best and test out a basic Hello World
end point and see if it returns anything. The server is running on your machine which means that it is hosted locally: http://localhost:9080/LibertyProject/System/helloworld
When you spun up the The blockchain network from VS Code, it had cars already on the Blockchain Ledger and you can view them by hitting the QueryCar/AllCars
endpoint:
http://localhost:9080/LibertyProject/System/QueryCar/AllCars
If you are keen you can see the output on the terminal window in VS Code, where its the same output. This is useful to see if it hasn't worked and I have printed the stack trace
out for you.
If it hasn't worked it will return back with Failed.
onto the web browser.
Successful response should look like:
Queried all Cars Successfully.
Cars:
[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}}]
As you can see, there is an ID for each item on the Ledger. This is very useful if you want to query specific items on the ledger. To Query specific cars on the ledger. You can query by CarByKeyID
.
For example query CAR10
and see the details of it:
http://localhost:9080/LibertyProject/System/QueryCar/CarByKeyID?Key=CAR10
Queried car Successfully.
Key = CAR5
Details = {"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}
You can Query any car on the ledger by changing the ID for the Key:
http://localhost:9080/LibertyProject/System/QueryCar/CarByKeyID?Key=<ID>
Open Liberty uses Microprofile and one of the features we are showing off is MicroProfile Open API. This is a feature where you can make POST requests to the Ledger, to add cars. Providing a GUI and buttons, means there is no need for the command line anymore!
Navigate to http://localhost:9080/openapi/ui/#/default/addCar
and click on Try it out
. You will be able to add data to the field. Feel free to put anything into your blockchain network.
Once you have added a car, you can easily query that car by the ID you were given.
Once you have finished, go back to VS Code, Liberty Dev Dashboard, and press Stop
. This will stop the Open Liberty Server. Now the server is not on, the application is not running anymore, meaning if you tried to go hit one of the end points, it wouldn't find it.
To stop the blockchain network, click on the Blockchain Icon on the left hand side. On Fabric environments click on ...
and click stop fabric environment
this will stop the environment from running.
To remove the Docker images where it was running, on Fabric Environments click on ...
and choose Teardown Fabric Environment
.
You have experienced using two IBM Open Source contributed products. You have learnt what Blockchain is, an application server is and experienced making transactions to a ledger and adding to a ledger.
Congratulations you have completed the task of using two IBM Products concurrently, as well as experiencing how easy it is to create a Blockchain Network and the concpets of Blockchain.