Building an API gateway using Spring Cloud's Netflix Zuul proxy

Spring Cloud provides the Zuul proxy to build an API gateway application. The following example demonstrates how to create an API gateway application using that proxy. So, let's create a Spring Boot project with the following code:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

In the preceding Maven configuration file, you can see that we have included the Web, Eureka Discovery, and Zuul starters. Let's now create the main entry-point class with @EnableZuulProxy as follows:

package com.dineshonjava.bookshop.apizuulproxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

As you can see in the preceding code, we have annotated this main class with three annotations: @EnableZuulProxy, @EnableEurekaClient, and @SpringBootApplication. The @SpringBootApplication annotation is responsible for auto-configuration, which enables a boot application. The @EnableEurekaClient annotation allows an API gateway to register with the service discovery, and the @EnableZuulProxy annotation enables the Zuul proxy. 

The following YAML configuration file illustrates how to configure the Zuul proxy for microservices:

spring:
application:
name: API-GATEWAY

server:
port: 8080

eureka:
client:
service-url:
default-zone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
prefer-ip-address: true

zuul:
ignoredServices: '*'
prefix: /api
routes:
account-service:
path: /account/**
serviceId: ACCOUNT-SERVICE
book-service:
path: /book/**
serviceId: BOOK-SERVICE
order-service:
path: /order/**
serviceId: ORDER-SERVICE
shipping-service:
path: /shipping/**
serviceId: SHIPPING-SERVICE
host:
socket-timeout-millis: 30000

As you can see in the preceding YAML configuration file, we have exposed services such as ACCOUNT-SERVICE, BOOK-SERVICE, ORDER-SERVICE, and SHIPPING-SERVICE through the Zuul proxy. By default, all the services registered with Eureka Server will be exposed. For this reason, we have set the zuul.ignoredServices property to override this default behavior and have only routed some services explicitly, as you can see with the configured services in the YAML configuration. 

We have also set a common prefix for all URLs, such as /api, by setting the zuul.prefix property.

In the preceding configuration, we used the URL http://localhost:8080/api/order/<orderId>, which is forwarded to the service with the service ID, ORDER-SERVICE.

As you have seen in the preceding examples, the Zuul proxy aggregates all configured services as a single application and requests a route from the Zuul proxy to the microservices. The Zuul service can also be used to implement other functionalities for cross-cutting concerns, such as authentication and authorization, API rate limiting, and so on.

There are many external APIs available on the market that provide API management functionalities, including API rate limiting, filtering, and so on. MuleSoft is one of them.

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

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