spring-cloud-Zuul学习(一)【基础篇】--入门案例【重新定义spring cloud实践】
-- 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”错误,已补充贴上)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>lb.study</groupId> 8 <artifactId>spring-cloud-Zuul</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>2.0.3.RELEASE</version> 16 </parent> 17 18 <properties> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 23 </properties> 24 25 <dependencyManagement> 26 <dependencies> 27 <dependency> 28 <groupId>org.springframework.cloud</groupId> 29 <artifactId>spring-cloud-dependencies</artifactId> 30 <version>${spring-cloud.version}</version> 31 <type>pom</type> 32 <scope>import</scope> 33 </dependency> 34 </dependencies> 35 </dependencyManagement> 36 37 <dependencies> 38 <!--spring-boo引包开始--> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-web</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-tomcat</artifactId> 46 <scope>provided</scope> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-starter-test</artifactId> 51 <scope>test</scope> 52 </dependency> 53 <!--spring-boo引包结束--> 54 55 <dependency> 56 <groupId>org.projectlombok</groupId> 57 <artifactId>lombok</artifactId> 58 </dependency> 59 </dependencies> 60 61 <build> 62 <plugins> 63 <plugin> 64 <groupId>org.springframework.boot</groupId> 65 <artifactId>spring-boot-maven-plugin</artifactId> 66 </plugin> 67 </plugins> 68 </build> 69 70 <modules> 71 <module>zuul-introduction</module> 72 </modules> 73 </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,结果如下: