spring cloud 路由网关zuul基本使用

在微服务架构中,需要几个关键的组件,服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个组件可以组建一个简单的微服务架构。客户端的请求首先经过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),然后再到具体的服务,服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理(之前文章讲述过),配置服务的配置文件放在Git仓库,方便开发人员随时改配置。

1. Zuul介绍

Zuul的主要功能是路由和过滤器。路由功能是微服务的一部分,比如/api/user映射到user服务,/api/shop映射到shop服务。zuul实现了负载均衡。以下是微服务结构中,Zuul的基本流程。在接下来的步骤中,我们来创建一个zuul服务

2. 创建Zuul的Maven工程springcloud-gateway-zuul,其中关于zuul的依赖是

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

3. 创建启动类: 使用@EnableZuulProxy注解

package com.pupeiyuan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

4. 编写zuul服务配置:

简单配置两个路由, 一个路由到MULTIPLE,一个路由到MOVESERVER; 由于都注册到eureka服务中心,这里有两种配置方式

(1)

spring:
  application:
    name: springcloud-gateway-zuul
server:
  port: 8050
eureka:
  client:
    service-url:
      defaultZone: http://root:123456@localhost:8000/eureka
  instance:
    prefer-ip-address: true
zuul:
  ignoredServices: microservice-consumer-movie-ribbon-with-hystrix
  prefix: /api
  strip-prefix: true
  routes:
    MULTIPLE: /user/**
    MOVESERVER: /movie/**
    

 

(2)都用通过serviceId来发现服务具体地址, path是路由的地址映射关系

 eureka:
   client:
     serviceUrl:
       defaultZone: http://localhost:8761/eureka/
 server:
   port: 8904
 spring:
   application:
     name: service-zuul
 zuul:
   routes:
     ribbo:
       path: /multiple/**
       serviceId: multiple
     feign:
       path: /movie/**
       serviceId: moveserver

这时启动zuul服务, 然后访问http://localhost:8050/movie/movie2/1可直接路由到springcloud-moveServer服务

http://localhost:8050/user/movie2/1可路由到multiple服务

路由前缀

设置 zuul.prefix 可以为所有的匹配增加前缀, 例如 /api,代理前缀默认会从请求路径中移除(通过zuul.stripPrefix=false可以关闭这个功能),zuul.stripPrefix默认为true.

如:配置全局的,与prefix一起使用

 
zuul:
  prefix: /api
  strip-prefix: true

当strip-prefix=true的时候 (http://localhost:8050/api/movie/movie2/1)

测试如下

  

使用正则表达式指定Zuul的路由匹配规则

借助PatternServiceRouteMapper,实现从微服务到映射路由的正则配置。

 
@SpringBootApplication
@EnableZuulProxy
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    /*正则表达式指定Zuul的路由匹配规则**/
    @Bean
    public PatternServiceRouteMapper serviceRouteMapper() {
        return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${version}/${name}");
    }
}

 

说明:上面将如provide-user-v2这个微服务,映射到/v2/provide-user/**这个路径上

 例:我们队微服务的serviceId命名为provide-user-v2,那么我们可以这么来访问http://localhost:5017/v2/provide-user/1

posted @ 2018-12-24 15:22  十月围城小童鞋  阅读(407)  评论(0编辑  收藏  举报