八、Spring Colud Zuul详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/makyan/article/details/88727965

8.1. Zuul基本介绍
Zuul本身不具备服务发现的功能,如果要让服务发现Zuul,必须让Zuul依赖于eureka client,
所以,使用Zuul,最基本的需要依赖两个包:

spring-cloud-starter-netflix-zuul
spring-cloud-starter-netflix-eureka-client

1、创建一个项目 futurecloud-apigetway-zuul
引入依赖


<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--将此项目变成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-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <!--添加zuul依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>

2、Spring Boot main class with @EnableZuulProxy


package com.futurecloud.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient //声明为eureka 客户端
@EnableZuulProxy
public class FuturecloudZuulApplication {

    public static void main( String[] args )
    {

        SpringApplication.run(FuturecloudZuulApplication.class,args);
    }

    @Bean
    public PatternServiceRouteMapper serviceRouteMapper() {
        return new PatternServiceRouteMapper(
            "(?<name>^.+)-(?<version>v.+$)",
            "${version}/${name}");
    }
}

3、隐式声明路由配置,即使用zuul的默认路由规则
application.yml配置如下:


server:
  port: 9000 #程序启动端口,也就是tomcat的端口
spring:
  application:
    name: futurecloud-apigetway-zuul #应用名称,别名

#将此服务注册到eureka 服务上
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123@localhost:10000/eureka
  instance:
    prefer-ip-address: true  #将注册到eureka服务的实例使用ip地址

依次启动服务:

futurecloud-service
futurecloud-user
futurecloud-hystrix-cluster1
futurecloud-apigetway-zuul

访问 : http://localhost:9000/futurecloud-hystrix-cluster1/user/99 ,访问成功 url访问地址分析:

http://localhost:9000  :是网关zuul的地址;
futurecloud-hystrix-cluster2 :是项目futurecloud-hystrix-cluster1的微服务serverId;
/user/99 :是微服务futurecloud-hystrix-cluster1的一个接口。

通过以上我们知道,Zuul默认会将通过以服务名作为ContextPath的方式创建路由映射,
比如将path:/futurecloud-hystrix-cluster1/user/99的请求转发到service-id=futurecloud-hystrix-cluster1的服务上(根据ContextPath查找对应的服务,需要结合服务发现机制如Eureka等);


  1. 显式声明路由配置:配置Zuul的路由映射
    zuul各种路由映射如下配置:

server:
  port: 9000 #程序启动端口,也就是tomcat的端口
spring:
  application:
    name: futurecloud-apigetway-zuul #应用名称,别名

#将此服务注册到eureka 服务上
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:123@localhost:10000/eureka
  instance:
    prefer-ip-address: true  #将注册到eureka服务的实例使用ip地址

zuul:
  routes:
    #1、 serviceId(服务名) 方式映射
    futurecloud-user: /user/**
    #2、path + serviceId 方式映射
    feign:
      path: /feign/**
      serviceId: futurecloud-feign
    #3、path + url 方式映射
    cluster1:   #随便写,但要保证唯一
      path: /cluster1/**
      url: http://localhost:8909/cluster1/
    cluster2:
      path: /cluster2/**
      url: forward:/wap #forward跳转到本地url,wap是本地的一个conetxt-path
    legacy:
      path: /**
      url: http://localhost:8914/cluster6/
    #4、前缀方式映射-去掉前缀,请求"/api/**"将被跳转到"futurecloud-feign"服务的"/**"上.
    futurecloud-feign:
      path: /api/**
      stripPrefix: true
    #5、前缀方式映射-保留前缀,请求"/order/**"将被跳转到"futurecloud-order"服务的"/order/**"上.
    futurecloud-order:
      path: /order/**
      stripPrefix: false

zuul路由映射类型:


(1)服务名模式:用服务名来做映射


zuul:
  routes:
    futurecloud-user: /abc/**
  ignored-services: futurecloud-order #zuul会从eureka上找到所有的注册的服务,然后全部做代理,如果我们不想要它代理其中一些服务,只需要添加此配置就好,多个服务之间用逗号分隔。

(2)path+服务名模式:用path路径来做映射


zuul:
  routes:
    abcdef: #随便写,但要保证唯一
      path: /abcd/** #映射的路径,也就是代理后的地址
      serviceId: futurecloud-user #给哪个服务做映射,这几行配置相当于前面的那一行配置

(3)path + url模式:用url方式来做映射

扫描二维码关注公众号,回复: 5644293 查看本文章

 zuul:
  routes:
    users: #随便写,但要保证唯一
      path: /myusers/**
      url: http://example.com/users_service

These simple url-routes do not get executed as a HystrixCommand, nor do they load-balance multiple URLs with Ribbon.
使用url模式,就不能使用hystrix(熔断)以及ribbon(负载均衡)了。
如果使用url的方式,还要使用熔断和负责均衡,那怎么办呢?
手动设置hystrix和ribbon


zuul:
  routes:
    echo: #随便写,但要保证唯一
      path: /myusers/**
      serviceId: futurecloud-user
      stripPrefix: true

#设置myusers-service服务为线程池隔离
hystrix:
  command:
    futurecloud-user:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: ...

futurecloud-user: #给上面的serviceId对应的服务的名字指定一个ribbon的负载均衡,负载均衡是从listOfServers配置的地址中选择。
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    listOfServers: http://example1.com,http://example2.com
    ConnectTimeout: 1000
    ReadTimeout: 3000
    MaxTotalHttpConnections: 500
    MaxConnectionsPerHost: 100

(4)表达式方式映射
使用正则表达式方式来映射,用版本号version和服务名name来做正则表达式匹配
在Spring boot main class with add ,for example:


@Bean
public PatternServiceRouteMapper serviceRouteMapper() {
    return new PatternServiceRouteMapper(
        "(?<name>^.+)-(?<version>v.+$)",
        "${version}/${name}");
}

通过这个正则表达式,得到的routes是myusers-v1,然后访问/v1/myusers/**
myuser是你的服务名,v1是你定义的版本号


(5)前缀方式映射


zuul
  prefix: /api

*************************************************************
 zuul:
  routes:
    users:
      path: /myusers/**
      stripPrefix: false

(6)路径过滤


 zuul:
  ignoredPatterns: /**/admin/**   #带得有此路径的服务,都不允许访问
  routes:
    users: /myusers/**

(7)特殊处理映射


 zuul:
  routes:
    myusers:  # myusers服务,使用/myusers/** 来访问 ,
      path: /myusers/**
    legacy:   # 其他服务使用 /**来访问
      path: /**

猜你喜欢

转载自blog.csdn.net/makyan/article/details/88727965