Books I read in 2019

Books I read in 2019. I wanted to talk about the books I have read in 2019. I feel like I have learnt some good things from books and I feel like I should share….

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Gateway Service using Spring Cloud Netflix Zuul

In this story we will learn how to map our MicroServices in the Cloud using Intelligent Routing via Netflix Api. So what is meant by Intelligent Routing?

Each of the individual services would be running on different port, different addresses or a combination of both. And they also likely have different paths and different API’s to interact with. As a user or a client of those services such as mobile app or web app, interacting with each of the individual services which could easily be in double or triple digits would be a nightmare. Instead we can use Intelligent Routing to make our application appear as it was a single system.

Routing is implemented via Gateway service or API Gateway. It is defined as single point of entry for all clients. Gateway Service is lot like a front door to our system. Each of the individual services is then located behind this door and all the requests must inter-through it.

Gateway Service provides the following functionalities.

Dynamic Routing and Delivery: It means at runtime it can decide where it should route a request and if it should even route a request at all.

Security: It provides the ability to authenticate all the incoming requests and filter out bad requests.

Auditing and Logging: It also provide auditing and logging request since all the requests must inter-through the gateway.

Request enhancement: It allows you to add some additional information to the request. A concrete example of this is the way Netflix uses their gateway. They use gateway service to do geo-location lookup for all incoming requests and add that additional information as additional request header so that it is available for all downstream services.

Load Balancer: The gateway service can also acts as load balancer for individual services.

Different APIs for different clients: Another feature of gateway service is ability to provide different APIs for different clients. Different clients such as mobile or web clients have different needs in the way they call API’s . For instance, it is Okay for web client to interact with several endpoints to accomplish certain functionality. But the same may not be true for mobile client which may be better served with a single endpoint.

1) gateway-service

Group: com.malathiboggavarapu, Artifact: gateway-service Dependencies: Zuul, Eureka Discovery

Go ahead and click Generate Project and that would create a gateway-service.zip file for you.

2) hello-service

Group: com.malathiboggavarapu, Artifact: hello-service Dependencies: Web, Eureka Discovery

Go ahead and click Generate Project and that would create a hello-service.zip file for you.

3) goodbye-service

Group: com.malathiboggavarapu Artifact: goodbye-service Dependencies: Web, Eureka Discovery

Go ahead and click Generate Project and that would create a goodbye-service.zip file for you.

Unzip all of those ZIP files and head over to your IDE and import all the projects. You also need the ServiceDiscovery server. So if you don’t already have that, import it into IDE from my github account. Please follow the link

Inorder to import from github, right click on empty space of Project Explorer, go to New -> Other. Choose the option Maven -> “Checkout Maven Projects from SCM” and click Next. Copy the above github url and paste it there and click Next and click Finish. We now have everything that we need to build MicroServices application using Gateway Service. Expand the gateway-service project and locate the main application class GatewayServiceApplication.java.

Next open up the application.properties and configure the gateway-service. Add the following properties

spring.application.name=gateway-service

eureka.client.service-url.defaultZone=http://localhost:8761/eureka

Now expand the hello-service application and locate the main application class HelloServiceApplication. Add the following annotations to the class.

1) @EnableDiscoveryClient 2) @RestController

Normally it is a bad idea to make this class annotated as Restcontroller. We need to actually create a seperate class for this. But to make it simple i am doing it. So it is OK for now. Add a handler method hello() as shown above which returns a simple string “Hello!” and annotate it with @RequestMapping.

Now it’s time to configure hello-service application using application.properties. Go ahead and add the following properties. The last property is the location of the ServiceDiscovery server.

spring.application.name=hello

server.port=1111

eureka.client.service-url.defaultZone=http://localhost:8761/eureka

Now expand the goodbye-service application and locate the main application class GoodbyeServiceApplication. Add the following annotations to the class.

1) @EnableDiscoveryClient 2) @RestController

Now it’s time to configure goodbye-service application using application.properties. Go ahead and add the following properties. The last property is the location of the ServiceDiscovery server.

spring.application.name=goodbye

server.port=2222

eureka.client.service-url.defaultZone=http://localhost:8761/eureka

Now it’s time to start all the services. We need to start ServiceDiscovery Server, gateway-service, hello-service, goodbye-service.

These examples are very simplistic but a lot is going on in the background. When we visit the above urls, the gateway-service looked up the service with the name goodbye from the Servicediscovery server and proxied the traffic to that service and proxied the response back to the browser.

One of the key features in Netflix zuul is the ability to define Filters. Filters allows you to intercept and control the requests and responses that pass through the gateway. Zuul has support for several different kind of Filters.

In our previous sections we have created 4 different services. One is ServiceDiscovery server to discover services, gateway-service and two application services goodbye service and hello service. Since we are building a filter we start with gateway-service.

So go ahead to gateway-service application and create a new package com.malathiboggavarapu.filters. Create a class called AddRequestHeaderFilter that extends ZuulFilter. See below.

Inorder to add request header to the request, we need access to RequestContext. Imagine that our usecase is to add location of the incoming request. So set header x-location and set the value as USA.

Now go to our main application class GatewayServiceApplication, add @Configuration annotation at class level. Create a new method addRequestHeaderFilter. See below.

Now it’s time to do changes in hello-service. Open the main application class HelloServiceApplication and do changes to the handler method hello(). See below.

If you notice the above code, we added a new parameter location to the method and annotated it with @RequestHeader and pass x-location parameter which gateway-service adds to the request header when it receives the request. Print the location as shown in the above example.

So that’s all about Mapping Services using Intelligent Routing via Gateway service. Hope it is helpful. Please post your comments below for any questions.

Happy learning!

Add a comment

Related posts:

Why You Need Personal Quarterly Goals And How To Plan Them

The places we work and the business where we love to shop have quarterly goals, and we are a big part of their goals equation. This is how they measure success or failure, iterate, benchmark mark…

5 useful terminal commands and how to launch them from your ruby program

Here are some terminal commands I recently learnt. They’ve proven quite useful (or just entertaining) on my Mac. Displays the content of your file. You can pass it flags (extra specifications…

Beyond hot reloading

I was subscribing to updates on this new vscode extension “Visual Studio IntelliCode” from Microsoft (AI-assisted development!) and made some interesting points we may have forgotten about. They…