How to Build a Spring Cloud Zuul Service Gateway Step‑by‑Step
This guide walks you through constructing a Spring Cloud Zuul gateway, covering the shortcomings of exposing services directly, the required preparations, Maven dependencies, application code, YAML configuration, automatic route creation, and how to verify the gateway’s behavior.
Why a Service Gateway?
Directly exposing internal services breaks the stateless nature of REST APIs because permission checks must be embedded in each service, and existing interfaces cannot be reused without adding proxy logic. To keep services clean and reusable, these concerns should be moved to a front‑door component.
Solution: Spring Cloud Zuul
Zuul acts as a service gateway that provides routing, load balancing, and centralized permission control. By placing Zuul in front of the microservice cluster, non‑business logic such as authentication is handled at the routing layer, improving testability and reusability.
Preparation
Ensure the existing Eureka client and consumer services ( eureka-client and eureka-consumer) are running.
Use the public Eureka server at http://eureka.didispace.com/ for service registration.
Build the Zuul Gateway Project
Create a new Spring Boot project named api-gateway and add the following dependencies to pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Next, create the main application class and enable Zuul:
@EnableZuulProxy
@SpringCloudApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}Add an application.yml configuration file with service name, port, and Eureka address:
spring:
application:
name: api-gateway
server:
port: 1101
eureka:
client:
serviceUrl:
defaultZone: http://eureka.didispace.com/eureka/Automatic Routing
When the gateway starts, it registers with Eureka and discovers the eureka-client and eureka-consumer services. Zuul automatically creates two route rules:
Requests matching /eureka-client/** are forwarded to the eureka-client service.
Requests matching /eureka-consumer/** are forwarded to the eureka-consumer service.
Verification
Access the gateway at http://localhost:1101/eureka-client/dc. The request is routed to the eureka-client service’s /dc endpoint, confirming that the routing works as expected.
Summary
This tutorial demonstrates how to set up a Spring Cloud Zuul gateway, providing a unified external entry point for internal services, automatically generating routes, and reducing the need for manual configuration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
