Getting Started with Microservice Dropwizard <9>

Getting Started

Dropwizard doesn't have any fancy project initialization helpers or Maven plugins. When getting started with Dropwizard, follow a similar pattern to any normal Java project: use a Maven archetype, or add it to an existing application using whatever tool you are currently using. You can also use JBoss Forge, a technology-agnostic Java project coordination and management tool that allows you to quickly create projects, add dependencies, add classes, and more. In this section, we will use Maven archetypes.

Select the directory in which to create the new Dropwizard project. Also verify that Maven is installed. You can run commands from your operating system's command prompt, or you can populate your favorite IDE's dialogs or wizards with the following information from the command:

Navigate to the directory that the Maven archetype generator gave us in hola-dropwizard and run the following command to build our project:

You will successfully build!

This uses the Dropwizard prototype java-Simple to create our microservice. If you go into the hola-dropwizard directory, you should see the following structure:

Note that Dropwizard creates a package structure for you that follows its conventions:

api

POJOs that define objects used in REST resources (some people refer to these objects as Domain Objects or DTOs).

cli

That's where the Dropwizard command comes in (other commands hopefully added to the app startup process).

client

Client helper class is here

db

Any DB related code or configuration is put here.

health

Microservice-specific health checks that can be exposed in the admin interface at runtime occur here.

resources

Our REST resource class goes here.

We also have the files HolaDropwizardApplication.java and Hola-DropwizardConfiguration.java, which are where our configuration and bootstrap code goes. For example, take a look at the HolaDropwizardApplication class, in Example 3-1 .

This class contains our public static void main() method, which doesn't do much other than call our microservice's run() method. It also has a getName() method, which is displayed at startup. The initialize() and run() methods are key places where we can bootstrap the application, which we will show in the next section

The generated configuration class HolaDropwizardConfigu is now empty (Example 3-2).

Although Dropwizard itself doesn't have any special Maven plugins, take a look at the generated pom.xml. We see that Dropwizard depends on the classpath, and we will use the maven-shade-plugin plugin to package our JAR into a jar. This means that all of our project's dependencies will be unpacked (ie, all dependency JARs will be unpacked) and combined into a single JAR that our build will create. For this JAR, we use maven-jar-plugin to make it executable.

One plugin we want to add is the exec-maven-plugin. With SpringBoot, we can use MVN's Spring Boot:Run to run our microservices. We want to be able to do the same in our Dropwizard application, so let's add the following plugin to the <Build> section of apor.xml, as shown in Example 3-3.

Now, we can execute the application from the command line as follows:

We can see something in Example 3-4


If you see the application start, you can try navigating to the default location of the RESTful endpoint in your browser: http://localhost:8080. You probably won't see much:

If you try to access the admin endpoint http://localhost:8081 you should see a simple page with some links. Try clicking around and see what value is already provided for managing your microservices!

Hello World

Now that we have our Dropwizard microservice template ready, let's add a REST endpoint. We want to expose an HTTP/REST endpoint on /api/hola that will return "HolaDrop Wizard from X", where X is the IP address where the service is running. To do this, go to the src/main/java/com/redhat/examples/dropwizard/resources directory (remember, this is the convention Dropwizard follows when placing REST resources and creating a new Java class called HolaRestResource). We'll add a method called hola() that returns a string along with the IP address of where the service is running, as shown in Example 3-5.

Add the HTTP Endpoints

There's a reason for this if we're familiar with SpringBoot. We want to be able to create REST endpoints and services using POJO code where possible, and the HelloWorld application is the best place to do that. To expose it as a REST service, we'll leverage good old JAX-RS annotations (see Example 3-6):

@Path
tells JAX-RS what the context path for this service should be.

@GET

Add a GET HTTP service.

Now, in the HolaDropwizardApplication class, let's provide an implementation for the run() method to add new REST resources to the microservice (Example 3-7).

Now, we should be able to build and run the Dropwizard microservice:

When we reach the endpoint at http://localhost:8080/api/hola, we should see the following:

 

The next section is even more exciting! ! ! Stay tuned

original:

Author source: https://github.com/redhat-developer/microservices-by-example-source

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325864716&siteId=291194637
Recommended