SpringCloud 2020.0.4 系列之 Gateway入门

21 篇文章 0 订阅
16 篇文章 0 订阅

目录

1. 概述

2. Gateway 工程的搭建

3. 自定义动态路由

4. 综述

5. 个人公众号


1. 概述

老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心。

言归正传,今天我们来聊聊 SpringCloud 的网关组件 Gateway,之前我们去访问 SpringCloud 不同服务的接口,都要去找每个服务的 IP地址 和 端口,有了 Gateway 这个组件,我们就可以从一个入口,去访问所有在 Eureka 中注册的服务。

闲话不多说,直接上代码。

2. Gateway 工程的搭建

2.1 主要依赖

        <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-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
        </dependency>

2.2 application.yml 主要配置

server:
  port: 44000

spring:
  application:
    name: my-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true

eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Eureka Server的地址

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

2.3 启动类注解

@SpringBootApplication
@EnableDiscoveryClient
public class MyGateWayApplication {

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

2.4 启动 Gateway 服务,并访问测试

1)启动 Eureka 服务

2)启动之前章节中讲到的 my-eureka-client 服务

3)启动 Gateway 服务

4)通过 Gateway 服务调用 my-eureka-client 服务暴露的接口

接口地址:http://localhost:44000/MY-EUREKA-CLIENT/eurekaClient/hello

地址格式:http://Gateway服务IP:端口/服务名大写/请求路径

2.5 地址中服务名改为小写

刚刚的实验中,我们发现,通过 Gateway 访问服务接口,服务的名称必须写为大写,才能正确访问接口,有点不习惯,我们可以通过配置将其统一改为小写。

增加如下配置即可:

cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true   # service-id 是否用小写

3. 自定义动态路由

3.1 概述

有时我们不想用服务名当做接口的前缀,可以自定义动态路由。

3.2 增加动态路由

POST http://localhost:44000/actuator/gateway/routes/myroute

最后的 myroute 是自定义路由的 ID,保证唯一即可。

参数:

{
    "predicates": [
        {
            "name":"Path",
            "args":{
                "_genkey_0":"/myroute/**"
            }
        }
    ],
    "filters": [
        {
            "name":"StripPrefix",
            "args":{
                "_genkey_0":"1"
            }
        }
    ],
    "uri": "lb://MY-EUREKA-CLIENT",
    "order": 0
  }

参数的含义是,所有以 myroute 开头的URL,统一转发到 MY-EUREKA-CLIENT 服务。

3.3 使用动态路由访问接口

http://localhost:44000/myroute/eurekaClient/hello

3.4 删除动态路由

DELETE http://localhost:44000/actuator/gateway/routes/myroute

4. 综述

今天聊了一下 SpringCloud 的 Gateway 组件,希望可以对大家的工作有所帮助。

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,每天更新Java干货。

5. 个人公众号

追风人聊Java,欢迎大家关注

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追风人聊Java

您的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值