Step-by-Step Guide to Integrating Spring Cloud Zuul for Service Gateway Routing
This article walks through the background, architecture, and practical implementation of a Spring Cloud Zuul service gateway, showing how to create the gateway project, enable Zuul, configure Eureka registration, build a sample business microservice, and test request routing and load balancing.
Background
When the number of microservices grows, configuring many reverse‑proxy instances and managing interface permissions becomes cumbersome, motivating a dedicated service gateway.
Zuul Overview
Zuul is Netflix’s open‑source API Gateway and a core module of Spring Cloud Netflix. It acts as a unified entry point, forwards external requests to business microservices, and provides routing and load‑balancing. It can be extended for authentication, rate limiting, and other cross‑cutting concerns.
Solution Practice
3.1 Build the Service Gateway
3.1.1 Create the gateway project
Create a Spring Boot project named eureka-zuul and add the Zuul starter and Eureka starter dependencies in 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>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>3.1.2 Enable Zuul proxy
Add @EnableZuulProxy to the Spring Boot main class:
@EnableZuulProxy
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}3.1.3 Configure Eureka client
In application.properties set the service name, port, and Eureka server address:
spring.application.name=eureka-zuul
server.port=9030
# Multiple addresses can be comma‑separated
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/3.2 Build a Business Microservice
3.2.1 Create the microservice project
Create a Spring Boot project named eureka-consumer-order and add Eureka and Web starters in 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-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>3.2.2 Write the web interface
Add @EnableDiscoveryClient to the main class and define a simple controller that returns a greeting:
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
public class HelloController {
@GetMapping("/hello")
public String index() {
return "hello,这是一个订单微服务";
}
}3.2.3 Configure Eureka client
In application.properties set the service name, port, and Eureka address:
spring.application.name=eureka-consumer-order
server.port=9022
# Multiple addresses can be comma‑separated
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/3.3 Service Testing
Start the Eureka server, then the gateway ( eureka-zuul) and the order service ( eureka-consumer-order). Access:
http://localhost:9030/eureka-consumer-order/helloThe response is: hello,这是一个订单微服务 Zuul forwards the request using the default routing rule http://zuul_host:zuul_port/serviceId/**, where serviceId corresponds to the spring.application.name of the target service. The request is internally routed to http://localhost:9022/hello. If multiple instances exist, Zuul performs load balancing automatically.
To hide the service ID, define a custom route in the gateway configuration:
# Custom route prefix; if omitted the default is the service name
zuul.routes.api-order.path=/order/**
zuul.routes.api-order.serviceId=eureka-consumer-orderConclusion
Zuul provides routing, load balancing, and can be extended for authentication and rate limiting.
References
http://www.ityouknow.com/springcloud/2017/06/01/gateway-service-zuul.html
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.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
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.
