SpringCloud的微服务网关:zuul(实践)

时间:2023-03-09 04:11:18
SpringCloud的微服务网关:zuul(实践)

Zuul的主要功能是路由和过滤器。路由功能是微服务的一部分,比如/api/user映射到user服务,/api/shop映射到shop服务。zuul实现了负载均衡。

zuul有以下功能:

  • Authentication
  • Insights
  • Stress Testing
  • Canary Testing
  • Dynamic Routing
  • Service Migration
  • Load Shedding
  • Security
  • Static Response handling
  • Active/Active traffic management

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:

  • pre:路由之前
  • routing:路由之时
  • post: 路由之后
  • error:发送错误调用

其他概念:

  • filterOrder:过滤的顺序
  • shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
  • run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。

1、pom.xml

SpringCloud的微服务网关:zuul(实践)

2、application.yml

SpringCloud的微服务网关:zuul(实践)

3、app

SpringCloud的微服务网关:zuul(实践)

4、filter

SpringCloud的微服务网关:zuul(实践)

5、运行验证

SpringCloud的微服务网关:zuul(实践)

http://localhost:8401/app-a/HelloA?name=liangshengqi&token=xxxx

SpringCloud的微服务网关:zuul(实践)

发现:已经使用了网关的ip,使用了spring.application.name使用了映射的app-a,  访问路径HelloA?name=liangshengqi&token=xxxx和原来一样