簡體   English   中英

Vanilla Spring Cloud Function 帶路由

[英]Vanilla Spring Cloud Function with Routing

作為 Spring 雲中的新蜜蜂,我正在開發多功能 spring 雲 function 應用程序,它與 spring-cloud-function-RELEASE-web 工作正常(依賴項.30.9.RELEASE -web) 注意:我在不同的 package 中有不同的 function 並且使用以下配置它工作正常。

cloud:
    function:
        scan:
            packages: zoo.app1.vanilla

例如,使用[POST] localhost:8080/func1它正在調用Func1 implements Function<I, O> 現在我要介紹路由。 為此,我只更改了application.yml中的以下內容

cloud:
    function:
        definition: functionRouter
        routing-expression: headers['function.name']
        scan:
            packages: zoo.app1.vanilla

現在當我調用 using

curl --location --request POST 'http://localhost:8080/functionRouter' \
--header 'function.name: func1' \
--header 'Content-Type: text/plain' \
--data-raw '1'

例外是

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'headers' cannot be found on object of type 'reactor.core.publisher.FluxMapFuseable' - maybe not public or not valid?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:55) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:91) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:117) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:375) ~[spring-expression-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.cloud.function.context.config.RoutingFunction.functionFromExpression(RoutingFunction.java:173)

現在,當我查看找到的代碼時, RequestProcessor的代碼如下 function

private Object getTargetIfRouting(FunctionWrapper wrapper, Object function) {
    if (function instanceof RoutingFunction) {
        String name = wrapper.headers.get("function.name").iterator().next();
        function = this.functionCatalog.lookup(name);
    }
    return function;
}

似乎,默認情況下,它需要消息 header 中的“function.name”以進行路由,因此我想在 application.yml 中注釋掉routing-expression行,它進入無限循環導致 stackoverflow 錯誤

2020-08-13 11:47:16.454 DEBUG 85560 --- [nio-8080-exec-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Applying function: functionRouter
2020-08-13 11:47:16.454  INFO 85560 --- [nio-8080-exec-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Looking up function 'functionRouter' with acceptedOutputTypes: []
2020-08-13 11:47:16.454  INFO 85560 --- [nio-8080-exec-1] o.s.c.f.context.config.RoutingFunction   : Resolved function from provided [definition] property functionRouter
2020-08-13 11:47:16.454 DEBUG 85560 --- [nio-8080-exec-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Applying function: functionRouter
2020-08-13 11:47:16.454  INFO 85560 --- [nio-8080-exec-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Looking up function 'functionRouter' with acceptedOutputTypes: []
2020-08-13 11:47:16.454  INFO 85560 --- [nio-8080-exec-1] o.s.c.f.context.config.RoutingFunction   : Resolved function from provided [definition] property functionRouter
2020-08-13 11:47:16.454 DEBUG 85560 --- [nio-8080-exec-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Applying function: functionRouter
2020-08-13 11:47:16.454  INFO 85560 --- [nio-8080-exec-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Looking up function 'functionRouter' with acceptedOutputTypes: []
2020-08-13 11:47:16.454  INFO 85560 --- [nio-8080-exec-1] o.s.c.f.context.config.RoutingFunction   : Resolved function from provided [definition] property functionRouter
2020-08-13 11:47:16.454 DEBUG 85560 --- [nio-8080-exec-1] o.s.c.f.c.c.SimpleFunctionRegistry       : Applying function: functionRouter
2020-08-13 11:47:16.468 ERROR 85560 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause

我做錯什么了嗎? 即使這可行,它是否會根據spring.cloud.function.scan.packages屬性識別我擁有的不同功能 請幫忙。 另外我還有兩個問題,

  1. in some blog/posts/documents it seems I can pass the spring.cloud.function.definition as http header. 如果這適用於這個 3.0.9.RELEASE,那么我是否需要在 application.yml 中提及相同的屬性?

  2. 我可以在不使用routingFunction的情況下使用spring.cloud.function.definition=func1;func2並期望路由行為正常工作嗎? 或者這是為了其他功能?

由於上述問題,我從未測試/玩過不同的配置選項。 請原諒我對 spring 雲的一點了解,或者如果我問了任何幼稚的問題。

編輯

經過調試並在Spring 文檔的幫助下,我找到了正確的配置

cloud:
    function:
        scan:
            packages: zoo.app1.vanilla
    stream:
        function:
            routing:
                enabled: true

使用此配置,它能夠將消息路由到我的 function,但僅限於第一次 現在,這讓我完全困惑。 一旦我啟動應用程序並從 postman 命中,它就能夠識別實際的 function 並按預期將輸入轉換為GenericMessage (盡管稍后無法解析請求正文)。 但是當我第二次(及以后)點擊時,它甚至無法解析我對 GenericMessage 的輸入並給我不同的錯誤。 這是可重復的行為。

供參考請找到兩個連續請求的日志(連同postman curl)

第一個請求:正確路由第二個請求:路由失敗

該問題在 3.1.0.RELEASE 中得到解決,感謝 Oleg Zhurakousky 和 Spring 團隊。 參考: 問題跟蹤器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM