第十章:SpringCloud Zuul路由器和过滤器

Netflix uses Zuul for the following: Netflix使用Zuul进行以下操作:
Authentication 认证
Insights洞察
Stress Testing 压力测试
Canary Testing 金丝雀测试
Dynamic Routing 动态路由
Service Migration 服务迁移
Load Shedding 加载脱落
Security 安全
Static Response handling 静态响应处理
Active/Active traffic management 主动/主动流量管理

1.添加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

zuul也需要注册到eureka

2.修改启动类

添加注解@EnableZuulProxy
我们来看看@EnableZuulProxy里面都包含了什么注解

img_3ac8e6782e4a7c8cb0cb967d3bbfa55d.png
image.png

@EnableCircuitBreaker实现断路器
@EnableDiscoveryClient实现eureka用户端注册

3.application.yml

和普通eureka客户端配置一样

server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9000/eureka
#加入密码验证
security:
  basic:
    enabled: true
  user:
    name: laojiao
    password: laojiao
spring:
  application:
    name: gateway-zuul

4.启动 查看zuul组件是否添加成功

  • 启动eureka
  • 启动user3服务
  • 启动zuul服务


    img_2336329717aca71bcdb21c8ebaf84c23.png
    image.png

    访问user3服务


    img_63827274004e5c3a7caf8ae99e8aedd7.png
    image.png

    通过zuul访问user3服务
    img_eaee1018feaa2687533639ed0fe328ae.png
    image.png
5. 自定义访问前缀
zuul:
  ignoredPatterns: /**/admin/**    #忽略某个服务的访问
  routes:
    provider-user3: /user/**

或者  serviceId模式
zuul:
  routes:
    users:
      path: /user/**
      serviceId: provider-user3

或者 url 模式(不能实现ribbon的负载均衡和hystrix断路器)
 zuul:
  routes:
    users:
      path: /user/**
      url: http://127.0.0.1:7904

img_584242f7c4b7b59467d48f16b2880ecb.png
image.png

但是要注意一点:

These simple url-routes don’t get executed as a HystrixCommand nor can you loadbalance multiple URLs with Ribbon.
To achieve this, specify a service-route and configure a Ribbon client for the serviceId (this currently requires disabling Eureka support in Ribbon: see above for more information), e.g.
这些简单的url路由不会作为HystrixCommand执行,也不能使用Ribbon来负载多个URL
为此,请指定服务路由并为serviceId配置功能区客户端(目前需要在功能区中禁用Eureka支持:有关详细信息,请参阅上文)。

zuul:
  routes:
    users:
      path: /user/**
      serviceId: provider-user3
ribbon:
  eureka:
    enabled: false

provider-user3:
  ribbon:
    listOfServers: http://127.0.0.1:7904,http://127.0.0.1:7902

这样配置后。我们启动7902,然后访问


img_774a3fc60c349ac2c665d125a1446bf3.png
image.png

然后看看两个user3服务 日志打印 是否做到负载均衡:


img_10d84256ad65e02a706fea0e0dc46fd9.png
image.png
img_25b422d8768fe0b090e2db04e4a33949.png
image.png

6. /routes 使用

访问zuul服务下的/routes,可以查看代理的服务路径列表。

img_544f7bd36c2b5935a7c3f5b37a6633a7.png
image.png

7.Strangulation Patterns and Local Forwards(请求转发)

Zuul代理是一个有用的工具,因为您可以使用它来处理来自旧端点的客户端的所有流量,但将一些请求重定向到新端点。
application.yml

 zuul:
  routes:
    first:
      path: /first/**
      url: http://first.example.com
    second:
      path: /second/**
      url: forward:/second
    third:
      path: /third/**
      url: forward:/3rd
    legacy:
      path: /**
      url: http://legacy.example.com

解释:

在这个例子中,我们正在扼杀映射到不匹配其他模式的所有请求的“遗留”应用程序。
/ first / **中的路径已被提取到具有外部URL的新服务中。
并且/ second / **中的路径被禁止,所以它们可以在本地处理,例如, 与一个正常的Spring @RequestMapping。
在/ third / **中的路径也被转发,但是具有不同的前缀(即/ third / foo被转发到/ 3rd / foo)。
注意:被忽略的模式并不完全被忽略,它们只是不被代理处理(所以它们也被有效地本地转发)。

7.Uploading Files through Zuul(文件上传)

如果您使用@EnableZuulProxy,则可以使用代理路径上传文件,只要文件很小,就可以使用。
对于大文件,在“/ zuul / ”中有一条绕过*Spring DispatcherServlet(避免多部分处理)的替代路径。
例子:我想通过upload服务上传文件,我在upload的配置文件设置允许的最大文件为2500Mb,但是如果通过zuul来上传http://super-john:8040/file-upload/upload的话,如果文件超过10MB,zuul会直接挡掉(因为用的是zuul的默认值);所以我可以在请求前缀上加个zuul即:http://super-john:8040/zuul/file-upload/upload
servlet路径通过zuul.servletPath进行外部化。
因为zuul默认通过hystrix和ribbon,所以极大的文件也需要提升超时设置 。

  • hystrix和ribbon的超时设置
    application.yml
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000

8.禁用Zuul过滤器

For example to disable org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter set zuul.SendResponseFilter.post.disable=true.

9.Providing Hystrix Fallbacks For Routes(提供Hystrix路由回退)

当Zuul中给定路由的电路被跳闸时,可以通过创建一个类型为ZuulFallbackProvider的bean来提供回退响应。 在这个bean中,你需要指定fallback的路由ID,并提供一个ClientHttpResponse作为回退。 这是一个非常简单的ZuulFallbackProvider实现

class MyFallbackProvider implements ZuulFallbackProvider {
    @Override
    public String getRoute() {
        return "customers";
    }

    @Override
    public ClientHttpResponse fallbackResponse() {
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return 200;
            }

            @Override
            public String getStatusText() throws IOException {
                return "OK";
            }

            @Override
            public void close() {

            }

            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream("fallback".getBytes());
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

其中

public String getRoute() {
        return "customers";
    }

 public InputStream getBody() throws IOException {
                return new ByteArrayInputStream("fallback".getBytes());
            }

customers改为自己 微服务的idname provider-user3
new ByteArrayInputStream("fallback".getBytes());里自定义返回内容

  • And here is what the route configuration would look like.
zuul:
  routes:
    provider-user3: /user/**
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值