一、Zuul简介
Zuul是Netflix开源的微服务网关,包含对请求的路由和过滤两个主要功能。
1)路由功能:负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。
2)过滤功能:负责对请求的过程进行干预,可以实现请求校验、服务聚合等功能。
二、Zuul代码实现
1)在pom.xml中引入依赖
注意:系统中所有的微服务须连接至Eureka注册中心。Eureka搭建详见另一篇博文:微服务1:搭建微服务注册中心Eureka(命令行简易版,不使用IDE)-CSDN博客
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完整的pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>microservice-zuul</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>microservice-zuul</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)配置application.yml
server:
port: 9000
spring:
application:
name: microservice-zuul
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
3)在App.java中添加注解
@EnableDiscoveryClient
@EnableZuulProxy
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class App
{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
三、Zuul测试
在浏览器中输入:
http://localhost:9000/microservice-consumer/users/4
其中microservice-consumer是实际要访问的微服务名称,如果不加Zuul,则该服务的访问URL为:
http://localhost:8010/users/3
使用Zuul网关之后,客户端无需知道每个微服务的端口号,即可通过网关端口号9000来统一访问到系统中所有的微服务。