微服务之API Gateway

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

在微服务架构中,后端的服务会存在多个。如果没有API Gateway就会存在以下的问题:

  • 客户端需要知道每个每个微服务的存在
  • 一次业务场景的交互需要发多次请求到多个微服务
  • 不同的微服务调用协议有可能是不同的
  • 每个微服务都需要进行权限校验

针对以上的需求,我们引入API Gateway。这里介绍基于Zuul的APIGateway的配置。

在gradle中添加依赖

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 1.8

ext {
    springCloudVersion = 'Finchley.SR1' //注意cloud的版本
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'
    compile('org.springframework.boot:spring-boot-starter')
}

在Application中添加@EnableZuulProxy

@SpringBootApplication
@EnableZuulProxy
public class ApiGatewayApplication {

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

添加Zuul路由配置

application.yml文件的配置:

server:
  port: 8900
zuul:
  strip-prefix: true //是否在转发时去掉路由前缀
  prefix: /api //全局到为路由规则增加前缀
  sensitive-headers: Cookie, Set-Cookie //默认情况下Zuul在路由请求时会将Cookie、Set-Cookie和Authorization给过滤调。由于下游服务可能需要Authorization信息,所以在这里进行自定义不过滤掉Authorization。
  ignored-headers: g1 //转发时忽略的hearder
  ignored-patterns: /**/goods/user //配置不希望API网关进行路由的路径
  routes:
    goods:
      path: /goods/** //路径为goods的都会路由到goods服务中
      serviceId: mst-goods-service //goods服务在Consul注册的service name
      stripPrefix: false //不移除路径前缀,即https://localhost:8900/goods/会被路由到goods服务到/goods路径,如果为true,则会被路由到goods服务到/(根路径)路径中

stripPrex介绍

在这里再次介绍下stripPrefix,stripPrex为true时表示是否在转发时将匹配的前缀移除。比如:

zuul:
  routes:
    goods:
      path: /goods/**
      serviceId: mst-goods-service
      stripPrefix: false
  prefix: /api
  strip-prefix: true

如果请求:https://localhost:8900/api/goods/user 路由到goods服务的goods/user

zuul:
  routes:
    goods:
      path: /goods/**
      serviceId: mst-goods-service
      stripPrefix: true
  prefix: /api
  strip-prefix: true

如果请求:https://localhost:8900/api/goods/user 路由到goods服务的/user

zuul:
  routes:
    goods:
      path: /goods/**
      serviceId: mst-goods-service
      stripPrefix: true
  prefix: /api
  strip-prefix: false

如果请求:https://localhost:8900/api/goods/user 路由到goods服务的/api/user

zuul:
  routes:
    goods:
      path: /goods/**
      serviceId: mst-goods-service
      stripPrefix: false
  prefix: /api
  strip-prefix: false

如果请求:https://localhost:8900/api/goods/user 路由到goods服务的/api/goods/user

代码

猜你喜欢

转载自blog.csdn.net/zhaoruda/article/details/82533336