SpringCloud Zuul的基本使用

2,318 阅读3分钟

Zuul基本使用

Zuul基本认识

Zuul是从设备和网站到后端应用程序所有请求的前门,为内部的服务提供可配置的对外URL到服务的映射关系,基于JVM的后端路由器,它具备以下的功能:

  1. 认证与鉴权
  2. 压力控制
  3. 金丝雀测试
  4. 动态路由
  5. 负载消减
  6. 静态响应
  7. 主动流量

Zuul具备上面的功能,它底层是基于Servlet,本质是一系列Filter所构成的责任链。

Zuul的入门例子

ZuulServer的主要依赖如下:

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

配置如下:

    pring:
      application:
        name: zuul-server-1
    server:
      port: 8093
      undertow:
        worker-threads: 4000
        io-threads: 200
    eureka:
      client:
        serviceUrl:
          defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8092}/eureka/
      instance:
        prefer-ip-address: true
    zuul:
      routes:
        zuul-gateway:
          path: /client/**
          serviceId: zuul-client-1

启动类的代码如下:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class,args);
    }
}

Client的代码如下:

依赖如下:

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

配置如下:

server:
  port: 8094
spring:
  application:
    name: zuul-client-1
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8092}/eureka/
  instance:
    prefer-ip-address: true

启动类如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
public class ZuulClientApplication {

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

测试Controller

@RestController
public class TestController {

	@GetMapping("/add")
	public Integer add(Integer a, Integer b){
		return a + b;
	}
}

测试结果:

到此一个简单的Zuul网关的例子已经完成了。

Zuul的配置

  1. 路由配置如下:

    zuul: routes: zuul-client-1: path: /client/** serviceId: zuul-client-1

上面的这段路由配置也可以写成这样:

zuul:
  routes:
    zuul-gateway:
      path: /client/**
      serviceId: zuul-client-1

上面的配置还可以简化成这个样子:

zuul:
  routes:
    zuul-client-1:
      path: /client/**
  1. 单实例url映射 上面的配置主要是针对服务进行路由的,除此之外我们还可以针对serviceId替换成为真实的物理地址。

zuul: routes: zuul-client-1: path: /client/** url: http://localhost:9093 #就是client的物理地址

  1. 多实例的又有配置

在默认的情况下Zuul会使用Eureka中集成的基本负载均衡功能,如果要是有Ribbon的负载均衡的功能,就需要指定一个serviceId,此操作需要禁止Ribbon是Eureka,在E版之后,新增了负载均衡的策略的配置,配置如下:

ribbon-route: ribbon: NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #Ribbon LB Strategy listOfServers: localhost:8092,localhost:8093 #client services for Ribbon LB

  1. forward本地跳转

如果需要在访问某个接口的时候跳转到这个方法上来处理,就需要用到Zuul的本地跳转

zuul: routes: zuul-client-1: path: /client/** url: forward:/client

  1. 相同路径的加载规则

zuul: routes: zuul-client-1: path: /client/** serviceId: zuul-client-1 zuul-client-2: path: /client/** serviceId: zuul-client-2

经过多次的实验后面的会将前面的覆盖掉。

功能配置

  1. 路由前缀的配置 在配置路由规则的时候,我么不可以配置一个统一的代理前缀。

    zuul: prefix: /pre routes: zuul-client-1: /client/**

我们在通过网关访问后端的接口的时候就需要加上这个前缀,请求的路径就编程了/pre/client/add,但是实际起作用的是/client/add,也就是Zuul会把代理路径从请求路径中移除。可以使用stripPrefix=false来关闭这个功能。

zuul:
  routes:
    zuul-client-1:
      path: /client/**
      serviceId: zuul-client-1
      stripPrefix: false

此时请求的路径是/pre/client/add,实际起作用的还是/pre/client/add,但是一般的情况下是选择无视这个配置。

  1. 服务屏蔽与路由屏蔽

zuul: ignored-services: zuul-client-2 ignored-patterns: /div/ prefix: /pre routes: zuul-client-1: /client/**

上面的配置在Zuul在拉去服务列表,创建映射规则的时候,就会忽略掉zuul-client-2服务和/**/div/**接口

  1. 敏感头信息 我们可以很容易的在头部存放信息,进行传递,这些在我们的系统内部传递是没有什么问题的,但是如果系统要和外部打交道就需要考虑防止这些数据的泄露。

zuul: ignored-services: zuul-client-2 ignored-patterns: /div/ prefix: /pre routes: zuul-client-1: /client/** sentiveHeaders: Cookie,Set-Cookie,Authorization