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

太过爱你忘了你带给我的痛 2021-12-15 05:41 524阅读 0赞

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

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

  1. 新建一个moudle 名为gateway

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

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-gateway</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-test</artifactId>
  13. <scope>test</scope>
  14. </dependency>
  15. </dependencies>
  1. 启动类添加注解 @EnableEurekaClient
  2. 配置文件,具体各个配置是什么意思,我在配置文件已经写的很清楚了,就不再赘述了,如下:

    server:
    port: 8888
    spring:
    profiles:

    1. active: path-route #使用哪个配置文件

    application:

    1. name: HELLO-GATEWAY #服务名

    —- #三个横线表示再创建一个配置文件
    spring:
    profiles: path-route #配置文件名 和 spring.profiles.active 相对应
    cloud:

    1. #设置路由规则
    2. gateway:
    3. routes:
    4. - id: gateway
    5. uri: lb://MICROSERVICE01 #代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
    6. predicates: #断言
    7. - Path=/service01/** #表示将以/service01/**开头的请求转发到uri为lb://MICROSERVICE01的地址上
    8. - 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

    1. discovery:
    2. locator:
    3. #表示gateway开启服务注册和发现功能,
    4. #并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
    5. enabled: true
    6. #表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
    7. lower-case-service-id: true

    spring:
    profiles: after-route
    cloud:

    1. gateway:
    2. routes:
    3. - id: gateway
    4. uri: lb://MICROSERVICE01
    5. predicates:
    6. #表示在该时间点之后的时间,发出的请求会被路由到uri
    7. - After=2019-06-20T00:00:00.789-07:00[America/Denver]

    - After=1561098916602 也可以用long类型的时间戳格式


    spring:
    profiles: before-route
    cloud:

    1. gateway:
    2. routes:
    3. - id: gateway
    4. uri: lb://MICROSERVICE01
    5. predicates:
    6. #表示在该时间点之前的时间,发出的请求会被路由到uri
    7. - Before=2019-12-20T00:00:00.789-07:00[America/Denver]

    spring:
    profiles: between-route
    cloud:

    1. gateway:
    2. routes:
    3. - id: gateway
    4. uri: lb://MICROSERVICE01
    5. predicates:
    6. #表示在该时间点之间的时间,发出的请求会被路由到uri
    7. - 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:

    1. gateway:
    2. routes:
    3. - id: gateway
    4. uri: lb://MICROSERVICE01
    5. predicates:
    6. #表示当请求的请求头中有 key=Hello,value=World,发出的请求会被路由到uri
    7. - Header=Hello, World
    8. #可以是正则表达式 例如 - Header=Hello, \d+

    spring:
    profiles: cookie-route
    cloud:

    1. gateway:
    2. routes:
    3. - id: gateway
    4. uri: lb://MICROSERVICE01
    5. predicates:
    6. #表示当请求带有名为Hello,值为World的Cookie时,发出的请求会被路由到uri
    7. - Cookie=Hello, World

    spring:
    profiles: host-route
    cloud:

    1. gateway:
    2. routes:
    3. - id: gateway
    4. uri: lb://MICROSERVICE01
    5. predicates:
    6. #表示当请求带有host为**.host.test时,发出的请求会被路由到uri
    7. - Host=**.host.test

    spring:
    profiles: method-route
    cloud:

    1. gateway:
    2. routes:
    3. - id: gateway
    4. uri: lb://MICROSERVICE01
    5. predicates:
    6. #表示GET请求,都会被路由到uri
    7. - Method=GET

    spring:
    profiles: query-route
    cloud:

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

本篇源码 -> 点我查看

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

发表评论

表情:
评论列表 (有 0 条评论,524人围观)

还没有评论,来说两句吧...

相关阅读