Microservice Dropwizard configuration <11> Service call

call another service

In a microservices environment, each service is responsible for providing functionality or services to other collaborators. If we wish to extend the "HelloWorld" microservice, we will need to create a service that can be called using the REST client functionality of the Drop wizard. Just as we did for the SpringBoot microservices, we'll leverage the backend services from the source code that accompanies the book. The interaction looks similar to:


If you look at the source code of the book, we will see a Maven module called Backend that contains a very simple HTTPservlet that can be invoked with a GET request and query parameters. The code for this backend is very simple and does not use any framework (SpringBoot, Swarm or WildFlySwarm).

To start the backend service on port 8080, go to the directory and run the following command:

 

The service is open at /api/backend and accepts a query parameter. For example, when we use the following path /API/backend? When greeting=Hello, the backend service will respond with a JSON object like the following:

$ curl -X GET http://localhost:8080/api/backend?greeting=Hello

Before we start, let's add the dropwizard-client dependency to our por.xml:

<dependency>

    <groupId>io.dropwizard</groupId>

    <artifactId>dropwizard-client</artifactId>

</dependency>

We'll create a new HTTP endpoint /api/greeting in the hola-dropwizard example and call this backend using Jersey! First, let's create two new classes in the src/main/java/com/redhat/examples/dropwizard/resources folder of the hola-dropwizard project named GreeterRestResources and GreeterSayingFactory. GreeterRestResources will implement our JAX-RS REST endpoint and the GreeterSayingFactory class will encapsulate the configuration options we want to use for the Greter service

The code implementation of the GreeterRestResource class is shown in Example 3-15

src/main/java/com/redhat/examples/dropwizard/resources/GreeterRestResource.java

The GreeterSayingFactory class encapsulates the configuration options we need, such as service host and service port, which can be changed in different environments (see Example 3-16). It also declares a JerseyClientConfiguration which provides a nice DSL for building our Jersey clients. Let's add it as a section to the HolaDropwizardConfiguration class, as shown in Example 3-17.

We can also update the conf/application.yml file to add the following configuration:

greeter:
      saying:${GREETER_SAYING:-Guten Tag Dropwizard}
      host:${GREETER_BACKEND_HOST:-localhost}
      port:${GREETER_BACKEND_PORT:-8080}

Note that if the OS environment variables do not provide various configuration options, we will specify default values ​​for them.

Now, let's connect the client to the greeter resource in the HolaDropwizardApplication class, where all our services and resources are connected to the environment object (Example 3-18).

Finally, let's implement the client, which will call the backend service, injecting all the appropriate host and port information, see Example 3-19. x

Dropwizard provides two convenience wrappers for making REST calls: the HttpComponents library directly (if you need low-level HTTP access) or the Jersey/JAX-RS REST client library, which has a high-level DSL for making HTTP calls. In the previous example, we used the Jersey client to make the call.

Now, let's build the microservice and verify that we can call this new greeting endpoint and that it calls the backend correctly. First, if the backend is not already running, we start it. Navigate to the backend directory of the source code that ships with this application and run it:

$ mvn clean install jetty:run

Next, let's build and run the Dropwizard microservice. Let's also configure this service to run on a different port than its default port (8080) so it doesn't conflict with a backend service already running on port 8080. Later in the book, we can see how to run these microservices in their own containers, removing the limitation of port switching at runtime.

First, let's be explicit about which port to use as the default port. Open the conf/application.yml file and add the server port information:

server:
      applicationConnectors:
        - type: http
          port: 8080

Now start the microservice by overriding the default port:

$ mvn clean install exec:java \ -Ddw.server.applicationConnectors[0].port=9090

Now, let's navigate our browser to http:/localhost:9090/api/greeting and see if our microservice is calling the backend correctly and displaying what we expect:

further study:

In this chapter, we learned about Dropwizard, saw some differences and similarities with Spring Boot, and how to expose REST endpoints, configuration and metrics, and how to call external servers. This is a quick introduction as a Drop wizard, by no means a comprehensive guide. For more information, see the following links:

  • Dropwizard Core

  • Dropwizard Getting Started

  • Client API

  • Dropwizard on GitHub

  • Dropwizard examples on GitHub

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

original:

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

If you have anything to discuss, you can add my WeChat public account:

Guess you like

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