SpringCloud入门学习(SpringCloud整合Gateway实现网关服务)

SpringCloud入门学习(SpringCloud整合Gateway实现网关服务)

本篇将在 上一篇 的基础上,介绍SpringCloud整合Gateway实现服务转发。

  1. 新建一个moudle 名为gateway

pom依赖如下,注意不要添加 spring-boot-starter-web 否则会启动失败

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>
  1. 启动类添加注解 @EnableEurekaClient
  2. 配置文件,具体各个配置是什么意思,我在配置文件已经写的很清楚了,就不再赘述了,如下:
server:
  port: 8888
spring:
  profiles:
    active: path-route      #使用哪个配置文件
  application:
    name: HELLO-GATEWAY     #服务名

---         #三个横线表示再创建一个配置文件
spring:
  profiles: path-route            #配置文件名 和 spring.profiles.active 相对应
  cloud:
    #设置路由规则
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01        #代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
        predicates:                     #断言
        - Path=/service01/**            #表示将以/service01/**开头的请求转发到uri为lb://MICROSERVICE01的地址上
        - After=2019-06-20T00:00:00.789-07:00[America/Denver] #表示在该时间点之后的时间,发出的请求会被路由到uri
#        filters:
#        - StripPrefix=1                #表示将Path的路径/service01在转发前去掉,如果设置StripPrefix=2,表示将/service01/*去掉 以此类推... 同时将spring.cloud.gateway.discovery.locator.enabled改为false,如果不改的话,之前的localhost:8799/client01/test01这样的请求地址也能正常访问,因为这时为每个服务创建了2个router

      discovery:
        locator:
          #表示gateway开启服务注册和发现功能,
          #并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
          enabled: true
          #表示将请求路径的服务名配置改成小写  因为服务注册的时候,向注册中心注册时将服务名转成大写的了
          lower-case-service-id: true
          
---
spring:
  profiles: after-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示在该时间点之后的时间,发出的请求会被路由到uri
        - After=2019-06-20T00:00:00.789-07:00[America/Denver]
#        - After=1561098916602  也可以用long类型的时间戳格式

---
spring:
  profiles: before-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示在该时间点之前的时间,发出的请求会被路由到uri
        - Before=2019-12-20T00:00:00.789-07:00[America/Denver]

---
spring:
  profiles: between-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示在该时间点之间的时间,发出的请求会被路由到uri
        - Between=2019-02-20T00:00:00.789-07:00[America/Denver],2019-12-20T00:00:00.789-07:00[America/Denver]

---
spring:
  profiles: header-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示当请求的请求头中有 key=Hello,value=World,发出的请求会被路由到uri
        - Header=Hello, World
        #可以是正则表达式 例如 - Header=Hello, \d+

---
spring:
  profiles: cookie-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示当请求带有名为Hello,值为World的Cookie时,发出的请求会被路由到uri
        - Cookie=Hello, World

---
spring:
  profiles: host-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示当请求带有host为**.host.test时,发出的请求会被路由到uri
        - Host=**.host.test

---
spring:
  profiles: method-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示GET请求,都会被路由到uri
        - Method=GET

---
spring:
  profiles: query-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示请求带有参数key=a, value=b时,该请求会被路由到uri
        - Query=a, b
  1. 根据配置文件的不同,请求路径也会不一样,由于本文只是涉及gateway的简单使用,我也没有做什么太深入的研究,就到这里啦。

本片源码 -> 点我查看

下一篇,将介绍 SpringCloud整合Zipkin实现服务调用的链路追踪

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章