springcloud(6):服务网关zuul基础篇

转自:微信公众号--纯洁的微笑(示例代码:https://github.com/ityouknow/spring-cloud-starter

(本人仅学习记录)


为什么需要API Gateway

1、简化客户端调用复杂度

在微服务架构模式下后端服务的实例数一般是动态的,对于客户端而言很难发现动态改变的服务实例的访问地址信息。因此在基于微服务的项目中为了简化前端的调用逻辑,通常会引入API Gateway作为轻量级网关,同时API Gateway中也会实现相关的认证逻辑从而简化内部服务之间相互调用的复杂度。


2、数据裁剪以及聚合

通常而言不同的客户端对于显示时对于数据的需求是不一致的,比如手机端或者Web端又或者在低延迟的网络环境或者高延迟的网络环境。

因此为了优化客户端的使用体验,API Gateway可以对通用性的响应数据进行裁剪以适应不同客户端的使用需求。同时还可以将多个API调用逻辑进行聚合,从而减少客户端的请求数,优化客户端用户体验

3、多渠道支持

当然我们还可以针对不同的渠道和客户端提供不同的API Gateway,对于该模式的使用由另外一个大家熟知的方式叫Backend for front-end, 在Backend for front-end模式当中,我们可以针对不同的客户端分别创建其BFF,进一步了解BFF可以参考这篇文章:Pattern: Backends For Frontends


4、遗留系统的微服务化改造

对于系统系统而言进行微服务改造通常是由于原有的系统存在或多或少的问题,比如技术债务,代码质量,可维护性,可扩展性等等。API Gateway的模式同样适用于这一类遗留系统的改造,通过微服务化的改造逐步实现对原有系统中的问题的修复,从而提升对于原有业务响应力的提升。通过引入抽象层,逐步使用新的实现替换旧的实现。


在Spring Cloud体系中, Spring Cloud Zuul就是提供负载均衡、反向代理、权限认证的一个API gateway。

Spring Cloud Zuul

Spring Cloud Zuul路由是微服务架构的不可或缺的一部分,提供动态路由,监控,弹性,安全等的边缘服务。Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器。

下面我们通过代码来了解Zuul是如何工作的

简单使用

1、添加依赖

 
  1. <dependency>

  2.    <groupId>org.springframework.cloud</groupId>

  3.    <artifactId>spring-cloud-starter-zuul</artifactId>

  4. </dependency>

引入 spring-cloud-starter-zuul

2、配置文件

 
  1. spring.application.name=gateway-service-zuul

  2. server.port=8888

  3. #这里的配置表示,访问/it/** 直接重定向到http://www.ityouknow.com/**

  4. zuul.routes.baidu.path=/it/**

  5. zuul.routes.baidu.url=http://www.ityouknow.com/

3、启动类

 
  1. @SpringBootApplication

  2. @EnableZuulProxy

  3. public class GatewayServiceZuulApplication {

  4.    public static void main(String[] args) {

  5.        SpringApplication.run(GatewayServiceZuulApplication.class, args);

  6.    }

  7. }

启动类添加 @EnableZuulProxy,支持网关路由。

史上最简单的zuul案例就配置完了

4、测试

启动 gateway-service-zuul-simple项目,在浏览器中访问: http://localhost:8888/it/spring-cloud,看到页面返回了: http://www.ityouknow.com/spring-cloud 页面的信息,如下:


我们以前面文章的示例代码 spring-cloud-producer为例来测试请求的重定向,在配置文件中添加:

 
  1. zuul.routes.hello.path=/hello/**

  2. zuul.routes.hello.url=http://localhost:9000/

启动 spring-cloud-producer,重新启动 gateway-service-zuul-simple,访问: http://localhost:8888/hello/hello?name=%E5%B0%8F%E6%98%8E,返回: hello小明,thisisfirst messge

说明访问 gateway-service-zuul-simple的请求自动转发到了 spring-cloud-producer,并且将结果返回。

服务化

通过url映射的方式来实现zull的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了。实际上在实现微服务架构时,服务名与服务实例地址的关系在eureka server中已经存在了,所以只需要将Zuul注册到eureka server上去发现其他服务,就可以实现对serviceId的映射。

我们结合示例来说明,在上面示例项目 gateway-service-zuul-simple的基础上来改造。

1、添加依赖

 
  1. <dependency>

  2.    <groupId>org.springframework.cloud</groupId>

  3.    <artifactId>spring-cloud-starter-eureka</artifactId>

  4. </dependency>

增加 spring-cloud-starter-eureka包,添加对eureka的支持。

2、配置文件

配置修改为:

 
  1. spring.application.name=gateway-service-zuul

  2. server.port=8888

  3. zuul.routes.api-a.path=/producer/**

  4. zuul.routes.api-a.serviceId=spring-cloud-producer

  5. eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、测试

依次启动 spring-cloud-eureka、 spring-cloud-producer、 gateway-service-zuul-eureka,访问: http://localhost:8888/producer/hello?name=%E5%B0%8F%E6%98%8E,返回: hello小明,thisisfirst messge

说明访问 gateway-service-zuul-eureka的请求自动转发到了 spring-cloud-producer,并且将结果返回。

为了更好的模拟服务集群,我们复制 spring-cloud-producer项目改为 spring-cloud-producer-2,修改 spring-cloud-producer-2项目端口为9001,controller代码修改如下:

 
  1. @RestController

  2. public class HelloController {

  3.    @RequestMapping("/hello")

  4.    public String index(@RequestParam String name) {

  5.        return "hello "+name+",this is two messge";

  6.    }

  7. }

修改完成后启动 spring-cloud-producer-2,重启 gateway-service-zuul-eureka。测试多次访问 http://localhost:8888/producer/hello?name=%E5%B0%8F%E6%98%8E,依次返回:

 
  1. hello 小明,this is first messge

  2. hello 小明,this is two messge

  3. hello 小明,this is first messge

  4. hello 小明,this is two messge

  5. ...

说明通过zuul成功调用了producer服务并且做了均衡负载。

网关的默认路由规则

但是如果后端服务多达十几个的时候,每一个都这样配置也挺麻烦的,spring cloud zuul已经帮我们做了默认配置。默认情况下,Zuul会代理所有注册到Eureka Server的微服务,并且Zuul的路由规则如下: http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的serviceId/**会被转发到serviceId对应的微服务。

我们注销掉 gateway-service-zuul-eureka项目中关于路由的配置:

 
  1. #zuul.routes.api-a.path=/producer/**

  2. #zuul.routes.api-a.serviceId=spring-cloud-producer

重新启动后,访问 http://localhost:8888/spring-cloud-producer/hello?name=%E5%B0%8F%E6%98%8E,测试返回结果和上述示例相同,说明spirng cloud zuul默认已经提供了转发功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值