美文网首页
SpringCloud 2020.0.4 系列之 Gateway

SpringCloud 2020.0.4 系列之 Gateway

作者: 追风人聊Java | 来源:发表于2022-01-11 13:34 被阅读0次

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干货。

相关文章

  • SpringCloud 2020.0.4 系列之 Gateway

    1. 概述 老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心。 言归正传,今天我们来聊聊 SpringClo...

  • SpringCloud 2020.0.4 系列之Eureka

    1. 概述 老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气、抱怨或生气。 言归正传,微服务是当...

  • SpringCloud 2020.0.4 系列之 Bus

    1. 概述 老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了。 言归正传,之前我们聊了...

  • SpringCloud 2020.0.4 系列之 Stream

    1. 概述 老话说的好:出错不怕,怕的是出了错,却不去改正。如果屡次出错,无法改对,就先记下了,然后找援军解决。 ...

  • SpringCloud 2020.0.4 系列之 Config

    1. 概述 老话说的好:一条路走不通,就去走另一条路,A计划执行不下去,就按B计划执行,多准备几套方案总是有用的。...

  • SpringCloud 2020.0.4 系列之 Sleuth

    1. 概述 老话说的好:安全不能带来财富,但盲目的冒险也是不可取的,大胆筹划,小心实施才是上策。 言归正传,微服务...

  • SpringCloud 2020.0.4 系列之 Feign

    1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解。...

  • SpringCloud 2020.0.4 系列之 Stream

    1. 概述 老话说的好:事情太多,做不过来,就先把事情记在本子上,然后理清思路、排好优先级,一件一件的去完成。 言...

  • SpringCloud 2020.0.4 系列之 Stream

    1. 概述 老话说的好:对待工作要有责任心,不仅要完成自己的部分,还要定期了解整体的进展。 言归正传,我们在开发产...

  • SpringCloud 2020.0.4 系列之Hystrix看

    1. 概述 老话说的好:沉默是金,有时适当的沉默,比滔滔不绝更加有效。 言归正传,前面我们聊了有关 Hystrix...

网友评论

      本文标题:SpringCloud 2020.0.4 系列之 Gateway

      本文链接:https://www.haomeiwen.com/subject/cgmbcrtx.html