Spring Cloud Gateway接入记录

依赖注意

Spring Cloud Gateway是使用netty+webflux实现因此不需要再引入spring-boot-starter-web包,这里我们引入Eureka,是为了直接通过Eureka 获取注册服务并发送请求。

Spring Cloud Gateway切记不可添加spring-boot-starterspring-boot-starter-web依赖,否则报404错误

<!--    <dependency>-->
<!--        <groupId>org.springframework.boot</groupId>-->
<!--        <artifactId>spring-boot-starter</artifactId>-->
<!--    </dependency>-->

基本依赖

<dependencies>
<!--    <dependency>-->
<!--        <groupId>org.springframework.boot</groupId>-->
<!--        <artifactId>spring-boot-starter</artifactId>-->
<!--    </dependency>-->
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

开启调试日志

logging:
  level:
    org.springframework.cloud.gateway: debug

基本配置(Java)

@Configuration
public class RouteLocatorConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
//                .route("path_route", r -> r.path("/about")  //http://127.0.0.1:9000/about -> http://www.appblog.cn/about
//                        .uri("http://www.appblog.cn"))
                .route("path_route", r -> r.path("/about")  //http://127.0.0.1:9000/about -> http://www.appblog.cn
                        .filters(f -> f.stripPrefix(1))
                        .uri("http://www.appblog.cn"))
                .route(r -> r.path("/test/**")  //http://127.0.0.1:9000/test/hello -> http://127.0.0.1:9001/hello
                        .filters(f -> f.stripPrefix(1))
                        .uri("http://127.0.0.1:9001")
                )
                .route("service_route", r -> r.path("/consumer/**")
                        .filters(f -> f.stripPrefix(1)
                                .addResponseHeader("X-AnotherHeader", "test"))
                        .uri("lb://spring-cloud-consumer")
                )
                .route(r -> r.path("/test/rateLimit")
                        .filters(f -> f.rewritePath("test", "hello")
                                .filter(new GatewayRateLimitByIpFilter(10, 1, Duration.ofSeconds(1))))
                        .uri("http://127.0.0.1:9001")
                )
                .build();
    }
}

基本配置(yml)

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
      # http://127.0.0.1:9000/about -> http://www.appblog.cn
      - id: appblog
        uri: http://www.appblog.cn
        predicates:
          - Path=/about
        filters:
          - StripPrefix=1
      # http://127.0.0.1:9000/consumer/hello -> http://spring-cloud-consumer/hello
      - id: consumer-service
        uri: lb://spring-cloud-consumer
        predicates:
          - Path=/consumer/**
        filters:
          - StripPrefix=1
      - id: producer-service
        uri: lb://spring-cloud-producer
        predicates:
          - Path=/producer/**
        filters:
          - StripPrefix=1

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/04/01/spring-cloud-gateway-access/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Spring Cloud Gateway接入记录
依赖注意 Spring Cloud Gateway是使用netty+webflux实现因此不需要再引入spring-boot-starter-web包,这里我们引入Eureka,是为了直接通过Eureka 获取注册服务……
<<上一篇
下一篇>>
文章目录
关闭
目 录