SpringCloud实战六:服务网关Zuul(一)

1,522 阅读3分钟
Hello大家好,我是初晨,本章我们学习SpringCloud 服务网关Zuul的使用。大家有问题和意见可以发邮箱mr_beany@163.com

一:什么是Zuul

Zuul是Spring Cloud服务系列中的微服务API网关。

Zuul的核心是一系列的filters, 其作用可以类比Servlet框架的Filter,或者AOP。

所有的请求都会经过Zuul的验证之后到达其他各个服务。作为一个边界性质的应用程序,Zuul提供了动态路由、监控、弹性负载和安全功能。Zuul底层利用各种filter实现如下功能:

  • 认证和安全 识别每个需要认证的资源,拒绝不符合要求的请求。
  • 性能监测 在服务边界追踪并统计数据,提供精确的生产视图。
  • 动态路由: 以动态方式根据需要将请求路由至不同后端集群处。
  • 压力测试: 逐渐增加指向集群的负载流量,从而计算性能水平。
  • 负载卸载 预先为每种类型的请求分配容量,当请求超过容量时自动丢弃。
  • 静态资源处理 直接在边界返回某些响应。

二:创建服务网关

1:创建过程与 SpringCloud 实战二:Client的创建和高可用 一样


2:打开pom文件,添加依赖

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

3:修改启动类,添加@EnableZuulProxy


4:修改配置文件名称为bootstrap.yml

spring:
  application:
    name: api-gateway
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG
      profile: dev

5:打开git仓库,创建配置文件


6:启动服务,访问http://localhost:8761/


可以看到网关服务已经注册成功。

7:通过网关服务的路由来访问其他服务接口

我们来访问前面为创建组件间通信创建的http://localhost:8081/getServerResult


可以访问,再通过网关访问http://localhost:8085/client/getServerResult


其中8085是服务网关的ip,client代表着client服务,getServerResult代表访问路径

8:自定义路由

通过Zuul每次访问client服务时都需要带上client,那么怎么才能不使用client而使用自定义的名称呢?

修改api-gateway服务配置文件,添加如下配置

zuul:
  routes:
    myClient:
      path: /myClient/**
      serviceId: client

这时地址栏中输入http://localhost:8085/myClient/getServerResult


仍然可以获取到返回结果。

那么我们怎么查看所有路由的规则呢?

地址栏中输入http://localhost:8085/actuator/routes


9:禁用路由

修改api-gateway配置文件,添加如下

zuul:
  ignored-patterns:
      - /**/getServerResult

此时我们git上的配置文件应该为

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:
        include: '*'
zuul:
  routes:
    myClient:
      path: /myClient/**
      serviceId: client
      # 设置可以传递请求头
      sensitiveHeaders:
  ignored-patterns:
      - /**/getServerResult

这里我们把这个url给禁用掉,再次访问该地址


10:动态配置路由

利用我们上篇文章讲的统一配置中心,来实现动态配置路由功能,大家可以先回想一下配置的步骤

  • 在api-gateway服务上,添加依赖

    <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>

  • 修改git上api-gateway服务的配置文件,添加rabbitmq配置信息

    spring:
      rabbitmq:
        host: 192.168.99.100
        username: user
        password: password
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    zuul:
      routes:
        myClient:
          path: /myClient/**
          serviceId: client
          # 设置可以传递请求头
          sensitiveHeaders:
      ignored-patterns:
          - /**/getServerResult

  • 创建ZuulConfig.java

    package com.example.apigateway;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
    
    @Component
    public class ZuulConfig {
    
        @ConfigurationProperties("zuul")
        @RefreshScope
        public ZuulProperties zuulProperties(){
            return new ZuulProperties();
        }
    }

注意这时我们访问http://localhost:8085/myClient/getServerResult是404的,因为我们已经在配置文件中把url禁用了


修改git上api-gateway的配置文件,把禁用http://localhost:8085/myClient/getServerResult

的配置注释掉


然后postman访问http://localhost:8084/actuator/bus-refresh来刷新配置文件

再次访问