-- 2019-04-15 16:58:02
- Zuul是什么?
以下是官方解释:
Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security. It also has the ability to route requests to multiple Amazon Auto Scaling Groups as appropriate.
Zuul是从设备和网站到后端应用程序所有请求的前门,为内部服务提供可配置的对外URL到服务的映射关系,基于JVM的后端路由器。其具备以下功能
- 认证与鉴权
- 压力测试
- 金丝雀测试
- 动态路由
- 负载削减
- 静态响应处理
- 主动流量管理
其底层基于Servlet,本质组件是一系列的Filter所构成的责任链。这其实就很容易知道底层的大概方向了,毕竟zuul主要是网关拦截
- 入门案例:
- 整体结构图
我在zuul-introduction下的pom文件导入spring-boot的依赖包,就不说了。(防止出现“The bean 'proxyRequestHelper', defined in class path resource”错误,已补充贴上)
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>lb.study</groupId>
<artifactId>spring-cloud-Zuul</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!--spring-boo引包开始-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--spring-boo引包结束--> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <modules>
<module>zuul-introduction</module>
</modules>
</project>
- 创建eureka-server
添加eureka注册中心依赖,在启动类添加注解@EnableEurekaServer即可,端口为8761
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<!-- eureka服务端依赖开始 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- eureka服务端依赖结束 -->
</dependencies>
@SpringBootApplication
@EnableEurekaServer //服务
public class EurekaServerApplication { public static void main(String[] args) {//
SpringApplication.run(EurekaServerApplication.class, args);
} }
- 创建zuul-server组件工程
zuul-server工程的pom.xml只需要引入zuul的核心包就行,不过这里有可能会造成版本不一样导致错误。
<dependencies>
<!-- zuul服务导包开始 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- zuul服务导包结束 --> </dependencies>
在启动类加注解@EnableEurekaClient(加上上这个后会自动注册到eureka上,就可以不在yml上写注册配置了)跟@EnableZuulProxy
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulServerApplication { public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
} }
application.yml文件如下:
说明:设置端口为5555,我这里没有指明注册中心,因为加了@EnableEurekaClient会默认注册到本地的eureka上。zuul下面的(最后五行),将所有以/client开头的URL映射到client-a这个服务去,所有就需用请求实际的服务,而是请求这个zuul就可以到相应的服务上去
spring:
application:
name: zuul-server
server:
port: 5555
zuul:
routes:
client-a:
path: /client/**
serviceId: client-a
- 创建client-a工程
这个工程只是提供一个controller接口而已,所以直接看代码就行了,不过application-name需要跟zuul配置的相同。
spring:
application:
name: client-a
server:
port: 7070
/**
* @author libo@citycloud.com.cn
* @date 2019/4/15 14:48
*/
@RestController
@RequestMapping("/student")
public class StudentController { @RequestMapping("/getStudent")
public String getStudent(){
return "这是一个学生";
}
}
- 测试
依次启动Eureka、Zuul、client-a三个工程,使用postman分两次访问接口:一次直接访问client-a项目localhost:7070/student/getStudent,一次通过zuul访问localhost:5555/client/student/getStudent,结果如下: