Java ☕️ and a little cloud ☁️ dashboard 🎛 for a microservice developer 👩💻👨💻
If you are a Java Developer and in need to check if your cloud services are responsive — this blogpost is for you — just slide over the below talk to Introduction.
July was a great slow month to enjoy some of summer on the Nothern Hemisphere. I spent even more time with my son trying to bond as his partner in his Fortnite battle royal. I developed some survival skills, but my son is so much better in the game tactics — of course!
Coming back recharched to work after summer holidays helped me to focus again on containers and PaaS — the Platform as a Service deployments.
Introduction
This blog post is exactly about my experiences from building a small test app for my local customer to create a test dashboard with availability of the cloud services. My two previous posts showed on how to test Compose for MongoDB https://medium.com/@blumareks/mongodb-for-you-a-hardcore-java-cloud-developer-f67b909834f4 — and Kafka as a Service (IBM Message Hub) https://medium.com/@blumareks/how-to-kafka-and-java-%EF%B8%8F-kafka-as-a-service-on-the-cloud-%EF%B8%8F-for-java-hardcore-devs-59990b578510 . Both services generated the output like this:
MongoDB:
{service: ‘mongodb’, operations: [
{type: ‘create’, response_time: 30, response_code: 200, desc:
{‘visitor id’: ‘some id’}},
{type: ‘read’, response_time: 25, response_code: 200, desc:
{‘visitor id’: ‘some id2’}},
{type: ‘update’, response_time: 49, response_code: 200, desc:
{‘visitor id’: ‘some id3’}},
{type: ‘delete’, response_time: 28, response_code: 200, desc:
{ ‘deleted visitor id’: ‘some id4’}}
], response_code: 200, desc:’operations implemented CRUD/CRUD’
}
IBM Message Hub:
{service: ‘message-hub’, operations: [
{type: ‘create topic’, response_time: 121, response_code: 200, desc: {‘Admin REST response’: ‘no response — the topic has been already created’}},
{type: ‘produce message’, response_time: 1, response_code: 200, desc: {‘Message created’: ‘Message produced, offset: 523’}},
{type: ‘consume message’, response_time: 2064, response_code: 200, desc: {‘Message consumed’: ‘Message consumed: ConsumerRecord(topic = test-java-messagehub-topic, partition = 0, offset = 523, CreateTime = 1499458409907, serialized key size = 3, serialized value size = 25, headers = RecordHeaders(headers = [], isReadOnly = false), key = key, value = This is a test message #2)’}}
], response_code: 200, desc:’operations implemented CrPCo/CrPCoD’}
Now it would be great to see it on a dashboard, and test it every couple hours… One way is to use a Kibana for doing exactly that. The other way is to build a service with Web front and show the results of queries there. In this post I will describe on how to deploy it.
If you undertook the previous How-Tos in this series, you can go ahead to step 2. Otherwise please continue reading here.
You will be able to learn how to run Java Open Liberty 🗽 server in the cloud ☁️ , and how to connect to the exposed service.
Prerequisites
Before you start you will need the following software on your machine:
- Git,
- Cloud Foundry CLI,
- Maven,
- and a Dedicated Bluemix account or Public Bluemix account (it needs to be a Cloud regular account, since the services aren’t Cloud lite account enabled),
- I am also using Eclipse with Bluemix pluggin to deploy the application to the cloud — however you can easily use any IDE for Java, and run Maven with IBM Cloud or Cloudfoundry CLIs (command line interface) in the terminal to achieve the same results.
Step 1. Clone the sample app.
Let us startwith getting your test application. You can simply clone my github repository : https://github.com/blumareks/BluemixTestDashboard and change the directory to where the sample app for the test dashboard is located. In order to do it use the instructions below:
$ git clone https://github.com/blumareks/BluemixTestDashboard
$ cd BluemixTestDashboard/TestDashboard/
Step 2. Run the app locally using command line
Use Maven to install dependencies and build the .war file.
$ mvn clean install
Run the app locally on Liberty:
$ mvn install liberty:run-server
View your app at: http://localhost:9080/
If everything is launched correctly you might see the init page asking for further configuration:
Step 3. Deploy to IBM Cloud using command line interface
To deploy an app to IBM Cloud using command line, it can be helpful to set up a manifest.yml file. The manifest.yml includes basic information about your app, such as the name, the location of your app, how much memory to allocate for each instance, and how many instances to create on startup. This is also where you’ll choose your app URL.
The manifest.yml is provided in the sample:
applications:
- path: target/TestDashboard.war
memory: 512M
instances: 1
name: your-appname-here
host: your-appname-here
Change both the name and host to a single unique name of your choice. Note that the host value will be used in your public url, for example, http://your-appname-here.mybluemix.net. If you already created an app from the IBM Cloud UI but haven’t pushed your code to it, you can use the same name value. Make sure the path points to the built application, for this example the location is target/JavaHelloWorldApp.war.
The next you need to choose your API endpoint (US South — which is a default for me, but you might use one out of two in Europe, Asia Pacific). Replace the API-endpoint in the command with an API endpoint from the following list.
https://api.ng.bluemix.net # US South
https://api.eu-gb.bluemix.net # United Kingdom
https://api.au-syd.bluemix.net # Sydney or your Bluemix Dedicated API
and then login to your IBM Cloud ⛅️ account
$ ibmcloud api https://api.ng.bluemix.net
$ ibmcloud login -u youremail-ID@email -o org_name -s space_name
alternatively you might want to use ibmcloud target --cf to setup the org and space.
You can now push the application to the cloud:
$ ibmcloud app push
This can take around two minutes to provision the service. If there is an error in the deployment process you can use the command ibmcloud cf logs <Your-App-Name> — recent to troubleshoot.
When application is running you could use the IBM Cloud UI to access the app:
Step 4. Add a database
Next, you will add a NoSQL database to this application and set up the application so that it can run locally or on IBM Cloud.
- Log in to IBM Cloud in your Browser. Select your application and click on Connect new under Connections. Select Cloudant NoSQL DB and Create the service.
(Alternatively select an existing Cloudant NoSQL DB service) - Select Restage when prompted.
- IBM Cloud will restage your application and provide the database credentials to your application using the VCAP_SERVICES environment variable. This environment variable is only available to the application when it is running on IBM Cloud.
Step 5. Developing and Deploying using Eclipse
IBM® Eclipse Tools for IBM Cloud provides plug-ins that can be installed into an existing Eclipse environment to assist in integrating the developer’s integrated development environment (IDE) with IBM Cloud.
Download and install IBM Eclipse Tools for IBM Cloud -> https://console.bluemix.net/docs/manageapps/eclipsetools/eclipsetools.html.
Import this sample into Eclipse using File -> Import -> Maven -> Existing Maven Projects option.
Create a local Open Liberty server definition:
- In the Servers view right-click -> New -> Server
- Select IBM -> WebSphere Application Server Liberty
- Choose Install from an archive or a repository
- Enter a destination path (/Users/username/liberty)
- Choose WAS Liberty with Java EE 7 Web Profile
- Continue the wizard with default options to Finish
- Run your application locally on Liberty:
- Right click on the TestDashboard sample and select Run As -> Run on Server option
- Find and select the localhost Liberty server and press Finish
In a few seconds, your application should be running at http://localhost:9080/TestDashboard/
In addition to above you can create the IBM Cloud based server definition:
- In the Servers view, right-click -> New -> Server
- Select IBM -> IBM Cloud and follow the steps in the wizard.
- Enter your credentials and click Next
- Select your org and space and click Finish
- Run your application on Bluemix:
- Right click on the TestDashboard sample and select Run As -> Run on Server option
- Find and select the IBM Bluemix and press Finish
- A wizard will guide you with the deployment options. Be sure to choose a unique Name for your application
In a few minutes, your application should be running at the URL you chose.
Now you have your code running locally and on the cloud!
The IBM Eclipse Tools for IBM Cloud provides many powerful features such as incremental updates, remote debugging, pushing packaged servers, etc. Learn more: https://console.bluemix.net/docs/manageapps/eclipsetools/eclipsetools.html
Step 6. Use the database in the local deployment with Eclipse
When running locally with or without Eclipse you can update your local code to point to the IBM Cloud database. You will store the credentials for the services in a properties file. This file will get used ONLY when the application is running locally. When you run the service in IBM Cloud, the credentials will be read automatically from the VCAP_SERVICES environment variable of the bound services to your IBM Cloud Java Open Liberty service.
In Eclipse or your favorite IDE, open the file src/main/resources/cloudant.properties — and add the properties:
cloudant_url=
In your browser open the IBM Cloud UI, select your App -> Connections -> Cloudant -> View Credentials
Copy and paste just the url from the credentials to the url field of the cloudant.properties file — it will look like this:
”url”: “https://<a user name>:<a password>@<an id of the Cloudant DB>.cloudant.com"
The property will look like this:
cloudant_url=https://<a user name>:<a password>@<an id of the Cloudant DB>.cloudant.com
Your Liberty server in Eclipse should automatically pick up the changes when you restart it.
View your app at: http://localhost:9080/TestDashboard/. Any names you enter into the app will now get added to the database.
Summary
Thank you for spending the time and working with Java Open Liberty service on IBM Cloud. Please feel free to make any changes you want to this application and re-deploy it to IBM Cloud!
Please subscribe and follow me on Twitter: http://twitter.com/blumareks
References
Further reading and references:
- Sample and tutorial to help you get started with a Java EE app, REST API and a database — https://github.com/IBM-Cloud/get-started-java