⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 http://www.iocoder.cn/Spring-Cloud-Gateway/route-locator-route-custom-kotlin/ 「芋道源码」欢迎转载,保留摘要,谢谢!

本文主要基于 Spring-Cloud-Gateway 2.0.X M4


🙂🙂🙂关注**微信公众号:【芋道源码】**有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言将得到认真回复。甚至不知道如何读源码也可以请教噢
  4. 新的源码解析文章实时收到通知。每周更新一篇左右
  5. 认真的源码交流微信群。

阅读源码最好的方式,是使用 IDEA 进行调试 Spring Cloud Gateway 源码,不然会一脸懵逼。

胖友可以点击「芋道源码」扫码关注,回复 git019 关键字
获得艿艿添加了中文注释的 Spring Cloud Gateway 源码地址。

阅读源码很孤单,加入源码交流群,一起坚持!

1. 概述

本文主要分享如何使用 Kotlin 实现自定义 RouteLocator

😈 由于笔者暂时不了解 Kotlin ,也比较,暂时不准备了解 Kotlin ,所以本文很大可能性是 "一本正经的胡说八道"


推荐 Spring Cloud 书籍

2. RouteLocatorDsl

org.springframework.cloud.gateway.route.RouteLocatorDsl ,使用 Kotlin 实现自定义 RouteLocator 。我们先打开 GatewayDsl.kt ,大体浏览一下。

下面我们来看一段示例程序,我们会把 GatewayDsl.kt 的代码实现嵌入其中。代码如下 :

import org.springframework.cloud.gateway.filter.factory.GatewayFilters.addResponseHeader
import org.springframework.cloud.gateway.handler.predicate.RoutePredicates.host
import org.springframework.cloud.gateway.handler.predicate.RoutePredicates.path
import org.springframework.cloud.gateway.route.RouteLocator
import org.springframework.cloud.gateway.route.gateway
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

1: @Configuration
2: class AdditionalRoutes {
3:
4: @Bean
5: fun additionalRouteLocator(): RouteLocator = gateway {
6: route(id = "test-kotlin") {
7: uri("http://httpbin.org:80") // Route.Builder#uri(uri)
8: predicate(host("kotlin.abc.org") and path("/image/png")) // Route.Builder#predicate(predicate)
9: add(addResponseHeader("X-TestHeader", "foobar")) // Route.Builder#add(webFilter)
10: }
11: }
12:
13: }

  • 调用 #gateway(...) 方法,创建自定义的 RouteLocator 。代码如下 :

    // GatewayDsl.kt
    fun gateway(routeLocator: RouteLocatorDsl.() -> Unit) = RouteLocatorDsl().apply(routeLocator).build()

  • 第 6 至 10 行 :调用 RouteLocatorDsl#route(...) 方法,配置一个 Route 。代码如下 :

    // GatewayDsl.kt
    private val routes = mutableListOf<Route>()

    /**
    * DSL to add a route to the [RouteLocator]
    *
    * @see [Route.Builder]
    */
    fun route(id: String? = null, order: Int = 0, uri: String? = null, init: Route.Builder.() -> Unit) {
    val builder = Route.builder()
    if (uri != null) {
    builder.uri(uri)
    }
    routes += builder.id(id).order(order).apply(init).build()
    }

  • 第 7 行 :调用 Route.Builder#uri(uri) 方法,设置 uri

  • 第 8 行 :调用 Route.Builder#predicate(predicate) 方法,设置 predicates

    • 使用 RoutePredicates 创建每个 Route 的 Predicate 。

    • and / or 操作符,代码如下 :

      // GatewayDsl.kt

      /**
      * A helper to return a composed [Predicate] that tests against this [Predicate] AND the [other] predicate
      */
      infix fun <T> Predicate<T>.and(other: Predicate<T>) = this.and(other)

      /**
      * A helper to return a composed [Predicate] that tests against this [Predicate] OR the [other] predicate
      */
      infix fun <T> Predicate<T>.or(other: Predicate<T>) = this.or(other)

      • x
  • 第 9 行 :调用 Route.Builder#add(webFilter) 方法,添加 filters

    • 使用 GatewayFilters 创建每个 Route 的 GatewayFilter 。

666. 彩蛋

知识星球

😈 "一本正经" 的写完了,反正我是不管了。哈哈哈哈。

胖友,分享一波朋友圈可好!

文章目录
  1. 1. 1. 概述
  2. 2. 2. RouteLocatorDsl
  3. 3. 666. 彩蛋