Configuring service consumer to use Zuul API gateway

The following code shows the existing configuration of RandomServiceProxy, which is used to call random service on Microservice A. The name attribute in the @FeignClient annotation is configured to use the application name of Microservice A. The request mapping uses the /random URI:

    @FeignClient(name ="microservice-a")
@RibbonClient(name="microservice-a")
public interface RandomServiceProxy {
@RequestMapping(value = "/random", method = RequestMethod.GET)
public List<Integer> getRandomNumbers();
}

Now, we want the call to go through the API Gateway. We would need to use the application name of the API Gateway and the new URI of random service in the request mapping. The following snippet shows the updated RandomServiceProxy class:

    @FeignClient(name="zuul-api-gateway")
//@FeignClient(name ="microservice-a")
@RibbonClient(name="microservice-a")
public interface RandomServiceProxy {
@RequestMapping(value = "/microservice-a/random",
method = RequestMethod.GET)
//@RequestMapping(value = "/random", method = RequestMethod.GET)
public List<Integer> getRandomNumbers();
}

When we invoke the add service at http://localhost:8765/service-consumer/add, we will see the typical response:

    2254

However, now we will see more things happen on Zuul API gateway. The following is an extract from the Zuul API gateway log. You can see that the initial add service call on the service consumer, as well as the random service call on Microservice A, are now being routed through the API Gateway:

2017-03-28 14:10:16.093 INFO 83147 --- [nio-8765-exec-4] c.m.s.z.filters.pre.SimpleLoggingFilter : Request Method : GET
URL: http://localhost:8765/service-consumer/add
2017-03-28 14:10:16.685 INFO 83147 --- [nio-8765-exec-5] c.m.s.z.filters.pre.SimpleLoggingFilter : Request Method : GET
URL: http://192.168.1.5:8765/microservice-a/random

We saw a basic implementation of a simple logging filter on Zuul API Gateway. A similar approach can be used to implement filters for other cross-cutting concerns.

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

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