前言
在上篇中介绍了 SpringCloud Config 的完美使用版本, 本篇则介绍基于 SpringCloud(基于 SpringBoot2.x,.SpringCloud Finchley 版)中的路由网关 (SpringCloud Zuul) 的使用教程.
SpringCloud Zuul
介绍
Spring Cloud Zuul 主要的功能是提供负载均衡, 反向代理, 权限认证, 动态路由, 监控, 弹性, 安全等的边缘服务. 其主要作用是为微服务架构提供了前门保护的作用, 同时将权限控制这些较重的非业务逻辑内容迁移到服务路由层面, 使得服务集群主体能够具备更高的可复用性和可测试性.
通俗一点来说, 就是对服务提供一层保护, 对外界的请求进行过滤转发到后端服务中.
这里我们可以通过几张简单的示例图来进行了解..
不使用路由网关的示例图:
使用路由网关的示例图:
使用路由网关并且加上注册中心的示例图:
从上述的示例图中, 我们发现加了路由网关之后, 实际上是将一对多的关系转变成了一对一的关系, 这样的好处是我们可以在网关层进行数据合法校验, 权限认证, 负载均衡等统一处理, 这样可以在很大的程度上节省的人力和物力, 但是这种方式也有一定的弊端, 就是以后新增了服务或者在服务中新增方法, 就会使得网关层可能需要进行改动. 幸好在 Spring Cloud 有 Zuul 这样一个组件, 通过 eureka 将网关和微服务之间相互关联起来, 都会在 eureka 上进行注册, 这样 Zuul 就能感知到哪些服务在线, 并且可以通过配置路由规则将请求自动转发到指定的后端微服务上, 这样即使后续新增了服务或者在服务中新增了某些方法, 那么只需在 Zuul 层进行简单配置即可.
开发准备
开发环境
- JDK:1.8
- SpringBoot:2.0.6.RELEASE
- SpringCloud:Finchley.SR2
注: 不一定非要用上述的版本, 可以根据情况进行相应的调整. 需要注意的是 SpringBoot2.x 以后, jdk 的版本必须是 1.8 以上!
服务端
由于我们这里是使用的第三种模式, 所以需要使用到 Eureka 注册中心, 因此这里我们也顺便弄一个注册中心服务.
注册中心这块配置和代码和之前配置基本一样即可. 注册中心新项目的的名称为 springcloud-zuul-eureka.
注册中心 pom 配置, application.properties 配置和代码如下:
- pom:
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- application.properties:
- spring.application.name=springcloud-zuul-eureka
- server.port=8006
- eureka.client.register-with-eureka=false
- eureka.client.fetch-registry=false
- eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
代码:
- @EnableEurekaServer
- @SpringBootApplication
- public class ZuulApplication {
- public static void main(String[] args) {
- SpringApplication.run(ZuulApplication.class, args);
- System.out.println("zuul 注册中心服务启动...");
- }
- }
注册中心服务配置完成之后, 我们在新增一个 Zuul 服务, 该服务的名称为 springcloud-zuul-gateway, 该服务的 pom 配置, application.properties 配置和代码如下:
- pom:
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter</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-zuul</artifactId>
- </dependency>
- </dependencies>
- application.properties:
- spring.application.name=springcloud-zuul-gateway
- server.port = 9009
- eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/
- zuul.routes.hello.path = /hello/**
- zuul.routes.hello.url = http://localhost:9010/hello
- zuul.routes.hi.path = /hi/**
- zuul.routes.hi.url = http://localhost:9011/hi
配置说明:
spring.application.name: 这个是指定服务名称.
server.port: 服务指定的端口.
eureka.client.serviceUrl.defaultZone: 这个是设置与 Eureka Server 交互的地址, 客户端的查询服务和注册服务都需要依赖这个地址.
zuul.routes.{route}.path: 自定义路由的规则, 通过 path 配置路径进行过滤;
zuul.routes.{route}.url: 自定义路由的规则, 访问上述的路径会转发到该配置的地址;
注: 上述的 zuul.routes.{route}.path 和 zuul.routes.{route}.url 一般来说是作为传统的方式进行配置, 是不依赖于 Eureka, 是属于一对一的配置. 例如, 访问: http://localhost:9009/hello/pancm 的话就会跳转到 http://localhost:9010/hello/pancm 地址上.
代码:
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableZuulProxy
- public class ZuulApplication {
- public static void main(String[] args) {
- SpringApplication.run(ZuulApplication.class, args);
- System.out.println("zuul 服务启动...");
- }
- }
客户端
这里我们也需要创建两个客户端服务, 来进行验证 Zuul 路由网关是否生效.
创建两个客户端服务, 名称分别为 springcloud-zuul-server1 和 springcloud-zuul-server2, 两个 pom 文件的配置如下:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- </dependencies>
两个 application.properties 配置文件也基本相同, 除了名称和端口不一致.
springcloud-zuul-server1 的 application.properties 配置:
- spring.application.name=springcloud-zuul-server1
- server.port=9010
- eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/
springcloud-zuul-server2 的 application.properties 配置:
- spring.application.name=springcloud-zuul-server2
- server.port=9011
- eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/
springcloud-zuul-server1 服务的代码:
主类
- @SpringBootApplication
- @EnableDiscoveryClient
- public class ZuulServerApplication1 {
- public static void main(String[] args) {
- SpringApplication.run(ZuulServerApplication1.class, args);
- System.out.println("zuul 第一个服务启动...");
- }
- }
控制层:
- @RestController
- public class ConsumerController {
- @RequestMapping("/hello/{name}")
- public String index(@PathVariable String name) {
- return name+",Hello World!";
- }
- }
springcloud-zuul-server2 服务的代码:
主类
- @SpringBootApplication
- @EnableDiscoveryClient
- public class ZuulServerApplication2 {
- public static void main(String[] args) {
- SpringApplication.run(ZuulServerApplication2.class, args);
- System.out.println("zuul 第二个服务启动...");
- }
- }
控制层:
- @RestController
- public class ConsumerController {
- @RequestMapping("/hi")
- public String index(@RequestParam String name) {
- return name+",hi!";
- }
- }
注: 这里故意将两个服务的接口参数请求和返回值弄成不一样, 以便对 Zull 的功能进行多方面测试.
测试
完成上述的代码开发后, 我们来进行测试 springcloud-zuul 是否可以地址过滤转发功能.
首先依次启动 springcloud-zuul-eureka,springcloud-zuul-gateway,springcloud-zuul-server1 和 springcloud-zuul-server2 这四个项目. 其中 9009 是服务 springcloud-zuul-gateway 的端口, 9010 是第一个客户端 springcloud-zuul-server1 的端口, 9011 是第二个客户端 springcloud-zuul-server2 的端口.
启动成功之后, 在浏览器输入:
http://localhost:9010/hello/pancm
界面返回:
pancm,hello world!!
在浏览器输入:
http://localhost:9011/hi?name=pancm
界面返回:
pancm,hi!
可以看出程序正常启动, 并且客户端的接口返回正确!
那么我们再来使用同样路径来访问 zuul, 因为是在本地进行测试, 因此只需要更改下端口就可以了, 将上述在浏览器访问的地址的端口自都改成 9009.
在浏览器输入:
http://localhost:9009/hello/pancm
界面返回:
pancm,Hello World!
在浏览器输入:
http://localhost:9009/hi?name=pancm
界面返回:
pancm,hi!
示例图:
从上述示例中, 我们可以得出 zuul 路由网关已经生效了, 成功的帮我们的请求进行了转发!
其他
总结
本篇文章主要介绍了关于 zuul 的基本使用, 使用的方式也是单例的, 一个路由规则对应一个地址, 按照上述的三幅示例图来说, 主要是介绍了第二种. 介于篇幅问题, 通过 Eureka 注册中心方式实现, zuul 的详细配置, 以及 zuul 的核心模块过滤器还未讲解, 这些留在下一篇再来讲解.
项目地址
基于 SpringBoot2.x,SpringCloud 的 Finchley 版本开发的地址 https://github.com/xuwujing/springcloud-study :https://github.com/xuwujing/springcloud-study
如果感觉项目不错, 希望能给个 star, 谢谢!
音乐推荐
这首纯音乐听起来有种似曾相识的感觉, 但仔细听下来, 又并非是自己熟悉的一首. 不过真是因为这样, 才有感觉吧.
原创不易, 如果感觉不错, 希望留言推荐! 您的支持是我写作的最大动力!
来源: https://www.cnblogs.com/xuwujing/p/10777782.html