SpringCloud之gateway使用

发布时间 2023-05-03 18:47:53作者: shigp1

SpringCloud Gateway是为了取代Zuul而开发出来的新一代网关,采用了响应式编程。
 
新建Module GatewayServer,添加依赖:

<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>

注意:不要添加web依赖。

 
 

配置application.yml:

server:
  port: 8500

spring:
  cloud:
    gateway:
      enabled: true
      routes:
        - id: Goods-Server  # 路由 id,唯一标识
          uri: http://www.baidu.com
          predicates:
              - Path=/**  # 断言,路由匹配条件,匹配 / 开头的所有 api
          filters:
              - StripPrefix=1



eureka:
  client:
    service-url:
      defaultZone: http://user:123@localhost:8761/eureka/

启动后访问http://localhost:8500/,跳转到百度首页。访问http://localhost:8500/producer/,跳转到百度首页。访问http://localhost:8500/producer/hello,报了404异常。访问路径localhost:8500后面不能超过2个子路径。