The Weather Microservice with OpenLiberty

In this chapter, a simple temperature Microservice already known from previous chapters is going to be implemented, demonstrating the simplicity of creating a Microservice with IBM's OpenLiberty. The Microservice built is available in this book's source code for a review and trial.

To create the Weather MicroService with OpenLiberty, you only need Java EE's JAX-RS and OpenLiberty configuration, which is described in this chapter. The code for the temperature Microservice created in the Chapter 2Creating your first Microservice  may be reused without any modifications whatsoever. First, you need to create the configuration:

package com.packtpub.Microservices.openliberty;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/weather")
public class WeatherMicroservice extends Application {

}

This configures the basic path for a weather service. The next step is to implement a RESTful endpoint, introduced in the second chapter, returning artificial average temperature whenever HTTP GET is issued upon the endpoint. Only a Java class decorated with annotations from the javax.ws.rs package is required:

package com.packtpub.Microservices.openliberty;

import com.packtpub.Microservices.domain.weather.Temperature;
import com.packtpub.Microservices.domain.weather.TemperatureScale;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
* RESTful resource providing information about city's temperature
*/
@Path("/temperature")
public class TemperatureResource {

/**
* Provides average temperature from all the city's sensors. The temperature
* is artificial.
*
* @return {@link Response} with constant temperature
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAverageTemperature() {
Temperature temperature = new Temperature();
temperature.setTemperature(35D);
temperature.setTemperatureScale(TemperatureScale.CELSIUS);

return Response.ok(temperature).build();
}
}

Both classes (the configuration and the endpoint itself) are located in the com.packtpub.Microservices.openliberty package. The package name is completely arbitrary and does not influence the functionality of the resulting OpenLiberty Microservice. 

In order to run the Microservice in a rightsized OpenLiberty runtime environment, two Maven commands must be issued:

mvn package
mvn liberty:run-server

Or simply execute the command mvn package liberty:run-server. The Maven package builds and packages the Java EE Microservice with the temperature endpoint inside and produces a  WAR. Such a WAR can be deployed to any existing application server and is not modified in any way. However, as the OpenLiberty Maven plugin is attached to the package phase of the build process, a self-contained zip file with both Microservice and OpenLiberty inside is generated.

During development, running a self-contained application over and over again is repetitive and may affect the developer's performance. The role of liberty:run-server is to create an instance of OpenLiberty and keep it running during the whole development process. During startup, OpenLiberty scans for archives created with the mvn package. Even when the Microservice is not yet packaged, once OpenLiberty is running, it scans the target/ folder in Maven's hierarchy for new applications to deploy. Once the application is deployed, it scans for changes in classes and resources. Once a class is recompiled, OpenLiberty detects the changes and instantly applies the new classes only. No complete redeployment happens. This way, code changes are visible almost instantly. It usually takes only dozens of milliseconds for OpenLiberty to apply the changes. From a developer's perspective, the process is instant.

After OpenLiberty is started and the temperature Microservice is packaged, simply invoking the http://localhost:9080/10-OpenLiberty.io/weather/temperature endpoint results in an immediate response. By default, OpenLiberty binds to localhost on port 9080. The port can be explicitly configured in the OpenLiberty Maven plugin configuration, as demonstrated in this chapter. The application context is by default the name of the application/ Microservice artifact being deployed, without any suffix. It can be controlled both in the server.xml configuration or by means of the Maven plugin configuration. The expected output is demonstrated in the following code block:

{
"temperature":35.0,
"temperatureScale":"CELSIUS"
}

OpenLiberty represents a way of rightsizing the application runtime. It may no longer be considered a pure application server only. If required, OpenLiberty can act as an application server, hosting a number of applications at once, providing many features up to the point where it is a full-blown Java EE 8 Application Server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.237.79