Spring Cloud Zuul
Spring Cloud Zuul 路由是微服务架构的不可或缺的一部分, 提供动态路由, 监控, 弹性, 安全等的边缘服务. Zuul 是 Netflix 出品的一个基于 JVM 路由和服务端的负载均衡器.
下面我们通过代码来了解 Zuul 是如何工作的
简单使用
1, 添加依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-zuul</artifactId>
- </dependency>
引入 spring-cloud-starter-zuul 包
2, 配置文件
- spring.application.name=gateway-service-zuul
- server.port=8888
- # 这里的配置表示, 访问 / it/** 直接重定向到 http://www.ityouknow.com/**
- zuul.routes.baidu.path=/it/**
- zuul.routes.baidu.url=http://www.ityouknow.com/
3, 启动类
- @SpringBootApplication
- @EnableZuulProxy
- public class GatewayServiceZuulApplication {
- public static void main(String[] args) {
- SpringApplication.run(GatewayServiceZuulApplication.class, args);
- }
- }
启动类添加 @EnableZuulProxy, 支持网关路由.
史上最简单的 zuul 案例就配置完了
4, 测试
启动 gateway-service-zuul-simple 项目, 在浏览器中访问: http://localhost:8888/it/spring-cloud , 看到页面返回了: http://www.ityouknow.com/spring-cloud 页面的信息.
我们以前面文章的示例代码 spring-cloud-producer 为例来测试请求的重定向, 在配置文件中添加:
- zuul.routes.hello.path=/hello/**
- zuul.routes.hello.url=http://localhost:9000/
启动 spring-cloud-producer, 重新启动 gateway-service-zuul-simple, 访问:, 返回: hello 小明, this is first messge
说明访问 gateway-service-zuul-simple 的请求自动转发到了 spring-cloud-producer, 并且将结果返回.
服务化
通过 url 映射的方式来实现 zull 的转发有局限性, 比如每增加一个服务就需要配置一条内容, 另外后端的服务如果是动态来提供, 就不能采用这种方案来配置了. 实际上在实现微服务架构时, 服务名与服务实例地址的关系在 eureka server 中已经存在了, 所以只需要将 Zuul 注册到 eureka server 上去发现其他服务, 就可以实现对 serviceId 的映射.
我们结合示例来说明, 在上面示例项目 gateway-service-zuul-simple 的基础上来改造.
1, 添加依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
增加 spring-cloud-starter-eureka 包, 添加对 eureka 的支持.
2, 配置文件
配置修改为:
- spring.application.name=gateway-service-zuul
- server.port=8888
- zuul.routes.API-a.path=/producer/**
- zuul.routes.API-a.serviceId=spring-cloud-producer
- eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3, 测试
依次启动 spring-cloud-eureka, spring-cloud-producer,gateway-service-zuul-eureka, 访问:, 返回: hello 小明, this is first messge
说明访问 gateway-service-zuul-eureka 的请求自动转发到了 spring-cloud-producer, 并且将结果返回.
为了更好的模拟服务集群, 我们复制 spring-cloud-producer 项目改为 spring-cloud-producer-2, 修改 spring-cloud-producer-2 项目端口为 9001,controller 代码修改如下:
- @RestController
- public class HelloController {
- @RequestMapping("/hello")
- public String index(@RequestParam String name) {
- return "hello"+name+",this is two messge";
- }
- }
修改完成后启动 spring-cloud-producer-2, 重启 gateway-service-zuul-eureka. 测试多次访问, 依次返回:
hello 小明, this is first messge
hello 小明, this is two messge
hello 小明, this is first messge
hello 小明, this is two messge
说明通过 zuul 成功调用了 producer 服务并且做了均衡负载.
网关的默认路由规则
但是如果后端服务多达十几个的时候, 每一个都这样配置也挺麻烦的, spring cloud zuul 已经帮我们做了默认配置. 默认情况下, Zuul 会代理所有注册到 Eureka Server 的微服务, 并且 Zuul 的路由规则如下: http://ZUUL_HOST:ZUUL_PORT/ http://zuul_host/ 微服务在 Eureka 上的 serviceId/** 会被转发到 serviceId 对应的微服务.
我们注销掉 gateway-service-zuul-eureka 项目中关于路由的配置:
- #zuul.routes.API-a.path=/producer/**
- // 社交电商平台源码请加企鹅求求: 一零三八七七四六二六.
- #zuul.routes.API-a.serviceId=spring-cloud-producer
重新启动后, 访问, 测试返回结果和上述示例相同, 说明 Spring cloud zuul 默认已经提供了转发功能.
到此 zuul 的基本使用我们就介绍完了. 关于 zuul 更高级使用, 我们下篇再来介绍.
来源: https://www.2cto.com/kf/201905/807544.html