Running the Spring Boot Weather Microservice

Once the Spring Boot Microservice is developed and assembled, it is ready to run. There are several ways of running a Spring Boot Microservice:

  • Executing a Spring Boot Java Archive in a JVM
  • Using Maven to run the Spring Boot application
  • Using the Spring Boot CLI

Since the Spring Boot Maven Plugin produces a JAR with all the dependencies found in the {project-root}/target/ folder, an obvious choice is to run it with java -jar weather-service-spring-1.0-SNAPSHOT.jar. As a necessity, Java must be available on the PATH, otherwise, a path to the java binary folder must be provided explicitly:

> java -jar weather-service-spring-1.0-SNAPSHOT.jar
...several output omitted...
Tomcat started on port(s): 8080 (http)
Started WeatherServiceApplication in 2.305 seconds (JVM running for 2.601)

As a default behavior, Spring Boot starts a Tomcat container included in the Java Archive, registers Spring-Framework-specific servlets, and constructs application context. The default interface Apache Tomcat listens to is 127.0.0.1 (localhost) using port 8080. If the Spring Boot application is successfully deployed into the bundled servlet container, an information message about the successful start of an application appears.

The initial ASCII art logo of Spring Boot can be disabled in many ways. One simple way is to construct the Spring Boot application using the SpringApplicationBuilder class from the org.springframework.boot.builder package. The showBanner(false) method of this builder saves console space.

After a successful start, the Weather Microservice can be invoked. The final URL of a RESTful resource is assembled as {protocol}://{host}:{port}/{base-context-path}/{resource-path}. By default, a plain HTTP protocol without any SSL/TLS capabilities is used when Tomcat is started. As already mentioned, the default interface Tomcat, or any other servlet container available with Spring Boot, binds to is localhost, using port 8080. The base context path is empty by default; however, in the Weather Microservice, a domain-descriptive prefix is bound using 'server.contextPath=/weather' in the application.properties file. The Temperature resource is available under the /temperature context path, which is appended to {base-context-path}. As the average temperature endpoint resides on the /temperature address, there's nothing more to append to the final URL.

To invoke WeatherMicroService, specifically the average temperature endpoint, any tool with support for an HTTP protocol can be used. One such tool is cURL. The Average temperature endpoint is reachable from the preceding URL by making an HTTP request using the GET method. If there is no cURL tool available for your operating system, for this simple request, any web browser can be utilized:

> curl http://localhost:8080/weather/temperature
{"temperature":35.0,"temperatureScale":"CELSIUS"}

The operation is successful if Spring Framework replies with a Temperature object serialized as JSON, as demonstrated in the preceding code. There is also an option for Maven to start any Spring Boot Microservice. With the Spring Boot Maven plugin present in the project, Maven goals prefixed with Spring Boot can be used to start, stop, and repackage the application:

> mvn spring-boot:run
...several output omitted...
Tomcat started on port(s): 8080 (http)
Started WeatherServiceApplication in 2.305 seconds (JVM running for 2.601)

The Spring Boot Maven plugin executes the very same java -jar command already demonstrated, only the process is more automated.

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

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