天天看点

SpringCloud Zuul路由网关的使用介绍

Zuul概述

Zuul

包含的对请求的路由和过滤两个主要功能。

其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。

Zuul

Eureka

整合,将

zuul

自身注册为

Eureka

服务治理下的应用,同时从

Eureka

中获取其他微服务的消息,也就i是说以后访问微服务都要通过

Zuul

跳转后获得。

Zuul

最终也是要注册进

Eureka

的。

Zuul

主要提供:代理、路由、过滤三大功能。

Zuul的实现

通过上面的概述,我们可以了解到Zuul最终还是会注册到Eureka中,所以,zuul的本质也可以看作一个eureka的服务

新建一个项目

1)引入依赖

<dependencies>
        <!-- 引入公用实体类依赖,引入api -->
        <dependency>
            <groupId>com.sc</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- spring boot web启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Eureka依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!-- Ribbon依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- Hystrix依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- Hystrix Dashboard依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- 监控信息 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Zuul依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- 热部署依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
           

2)编写配置文件

server:
  port: 6527

spring:
  application:
    name: zuul

#Eureka 配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7003/eureka/
  instance:
    instance-id: zuul6527
    prefer-ip-address: false #false 隐藏服务的地址信息,true显示IP地址

#路由网关配置
zuul:
  routes: #配置微服务的访问别名
    mydept.serviceId: peo_user
    mydept.path: /mydept/**
  ignored-services: "*" # peo_user忽略微服务的真实名称,不能以名称访问,"*"隐藏全部服务
  prefix: /fyy #统一访问前缀


info:
 app.name: fyy
 company.name: www.fyy.com
           

3)配置启动类

@SpringBootApplication
@EnableZuulProxy
public class ZuuApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuuApplication.class,args);
    }

}
           

简单三步,我们就实现了zuul统一网关的实现集成

Zuul配置详解

  • 查看 zuul 中配置好的路由和过滤器信息

    当我们启动了zuul 的网关之后,想知道当前zuul代理了那些路由,访问的路径又是那些或者我们自己写了一个zuul的filter,想知道它当前位于zuul的filter的那个位置时,该如何查询

    访问路径:

    http://网关地址:端口/routes 访问路由的简单信息

    http://网关地址:端口/routes?format=details 访问路由的详细信息

    http://网关地址:端口/filters 访问zuul中使用了那些过滤器

    yml配置

management:  
  security:  
    enabled: false  # 默认值是 true, 为true的话那么页面上可能会报没有权限访问  
           
  • 忽略所有微服务或某些微服务
zuul:  
  ignored-services: "*" #忽略所有微服务
  ignored-services: product-provider,product-consumer-8201  #忽略某些微服务:(直接写微服务的名字=>可以理解为spring.application.name的值,多个以都好分隔)
           
  • 路由指定的微服务
zuul:
  routes: #配置微服务的访问别名,一个map的key-value
    mydept.serviceId: peo_user
    mydept.path: /mydept/**
    ...
    ...
           
  • 通过path和url访问到具体的某台机器上
zuul:  
  routes:  
    product-provider:  
      path: /product/**  
      url: http://localhost:8202/ #url 里面不可以写多个url,访问不会作为 HystrixCommand 来进行访问
           
  • 脱离eureka进行访问,并使之具有负载均衡和隔离的机制等
ribbon:
	eureka:
		enabled: false #修改为false

zuul:
  routes: 
    product-provider: 
      path:
      serviceId:

baidu-oschina:
	ribbon:
		listOfServers: http://www.baidu.com/,https://gitee.com:443/
           
  • 转发前是否去掉路由前缀&添加统一路由前缀
zuul:
  prefix: /fyy #统一访问前缀
  strip-prefix: true #转发前是否去掉路由前缀 true:去掉;false:不去
           
  • 忽略某些路径不进行路由
zuul:  
  ignored-patterns: /**/selectOne/**
           

@EnableZuulServer、@EnableZuulProxy两个注解的区别

简单的说

@EnableZuulProxy

@EnableZuulServer

的增强版,当Zuul与Eureka、Ribbon等组件配合使用时,我们使用

@EnableZuulProxy

@EnableZuulServer过滤器:

1)pre类型过滤器:

  • ServletDetectionFilter

    :该过滤器用于检查请求是否通过Spring Dispatcher。检查后,通过

    isDispatcherServletRequest

    设置布尔值
  • FormBodyWrapperFilter

    :解析表单数据,并为请求重新编码
  • DebugFilter

    :顾名思义,调试用的过滤器,可以通过

    zuul.debug.request=true

    开启该过滤器

    2)route类型过滤器

  • SendForwardFilter

    :该过滤器使用Servlet RequestDispatcher转发请求,转发位置存储在

    RequestContext.getCurrentContext().get("forward.to")

    中,可以设置为

    zuul、routes、abc、path: /abc/**、url: forward:/abc

    ,然后访问

    $ZUUL_HOST:ZUUL_PORT/abc

    ,观察该过滤器的执行过程。

    3)post类型过滤器

    SendResponseFilter

    :将Zuul所代理的微服务的响应写入当前响应

    4)error类型过滤器

    SendErrorFilter

    :如果

    RequestContext.getThrowable()

    不为null,那么默认就会转发到/error,也可以设置

    error.path

    属性修改默认的转发路径

@EnableZuulProxy过滤器:

如果使用注解

@EnableZuulProxy

,那么除上述过滤器之外,Spring Cloud还会增加以下过滤器

1)pre类型过滤器

  • PreDecorationFilter

    :该过滤器根据提供的RouteLocator确定路由到的地址,以及怎样去路由。该路由器也可为后端请求设置各种代理相关的header

    2)route类型过滤器

  • RibbonRoutingFilter

    :该过滤器使用Ribbon,Hystrix和可插拔的HTTP客户端发送请求,该过滤器可使用不同的HTTP客户端

    (1) Apache HttpClient:默认的HTTP客户端

    (2)SquareupOkHttpClient v3:如需使用该客户端,需保证

    com.squareup.okhttp3

    的依赖在classpath中,并设置

    ribbon.okhttp.enabled = true

    (3)Netflix Ribbon HTTP client:设置

    ribbon.restclient.enabled = true

    即可启用该HTTP客户端。需要注意的是,该客户端有一定限制,例如不支持PATCH方法,另外,它有内置的重试机制
  • SimpleHostRoutingFilter

    :该过滤器通过Apache HttpClient向指定的URL发送请求。URL在

    RequestContext.getRouteHost()

源码地址:下载地址

继续阅读