Example of API Gateway

In this example, we will try to show only sample product pages that will fetch the data from the service product detail to give information about the product. This example can be increased in many aspects. Our focus of this example is to only show how the API Gateway pattern works; so we will try to keep this example simple and small.

This example will be using Zuul from Netflix as an API Gateway. Spring also had an implementation of Zuul in it, so we are creating this example with Spring Boot. For a sample API Gateway implementation, we will be using http://start.spring.io/ to generate an initial template of our code. Spring initializer is the project from Spring to help beginners generate basic Spring Boot code. A user has to set a minimum configuration and can hit the Generate Project button. If any user wants to set more specific details regarding the project, then they can see all the configuration settings by clicking on the Switch to the full version button, as shown in the following screenshot:

For Zuul, user have to click on Advance and check the zuul check box and hit generate project. Project should be starting to download by now.

If you open the pom.xml of the project downloaded you can see the zuul dependency in it as follows:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

Now let's create a controller in the same package of the main application class and put the following code in the file:

@SpringBootApplication  
@RestController  
public class ProductDetailConrtoller 
 {  
    @Resource 
    ProductDetailService pdService; 
 
    @RequestMapping(value = "/product/{id}")  
    public ProductDetail getAllProduct( @PathParam("id") String id)  
    {  
        return pdService.getProductDetailById(id); 
       }  
} 

In the preceding code, there is an assumption that the pdService bean will interact with the Spring data repository for the product detail and get the result for the required product ID. Another assumption is that this service is running on port 10000. Just to make sure everything is running, a hit on a URL such as http://localhost:10000/product/1 should give some JSON as a response.

For the API Gateway, we will create another Spring Boot application with Zuul support. Zuul can be activated by just adding a simple @EnableZuulProxy annotation.

The following is a simple code to start the Zuul proxy:

@SpringBootApplication  
@EnableZuulProxy  
public class ApiGatewayExampleInSpring  
{  
    public static void main(String[] args)      
    { 
     SpringApplication.run(ApiGatewayExampleInSpring.class, args); 
     } 
} 

The rest of the things are managed in configuration. In the application.properties file of the API Gateway, the content will be something as follows:

zuul.routes.product.path=/product/**  
zuul.routes.produc.url=http://localhost:10000  
ribbon.eureka.enabled=false  
server.port=8080   

With this configuration, we are defining rules such as this: for any request for a URL such as /product/xxx, pass this request to http://localhost:10000. For the outer world, it will be like http://localhost:8080/product/1, which will internally be transferred to the 10000 port. If we defined a spring.application.name variable as a product in the product detail microservice, then we don't need to define the URL path property here (zuul.routes.product.path=/product/** ), as Zuul, by default, will make it a URL/product.

The example taken here for an API Gateway is not very intelligent, but this is a very capable API Gateway. Depending on the routes, filter, and caching defined in the Zuul's property, one can make a very powerful API Gateway.

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

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