简体   繁体   English

Spring Cloud 函数:如何将供应商映射到 Azure 函数

[英]Spring Cloud Function: How to map a Supplier to Azure function

I'm trying to map a Supplier Bean to an Azure function using Spring Cloud Function 2.0, but I need to extend AzureSpringBootRequestHandler, which seems to only support functions with an input parameter and a return value.我正在尝试使用 Spring Cloud Function 2.0 将供应商 Bean 映射到 Azure 函数,但我需要扩展 AzureSpringBootRequestHandler,它似乎只支持具有输入参数和返回值的函数。 class AzureSpringBootRequestHandler has two type parameters: input and output, and AzureSpringBootRequestHandler.handleRequest() also expects the input parameter. class AzureSpringBootRequestHandler 有两个类型参数:输入和输出,AzureSpringBootRequestHandler.handleRequest() 也需要输入参数。

@Bean
public Supplier<List<String>> foo() {
    return () -> Arrays.asList("foo1", "foo2");
}

/////

class FooFunction extends AzureSpringBootRequestHandler<Void, List<String>> {
    @FunctionName("foo")
    List<String> foo(@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST},
        authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
                               ExecutionContext context) {
        return handleRequest(null, context);
    }
}

The code above causes NPE at reactor.core.publisher.FluxJust.(FluxJust.java:60)上面的代码在 reactor.core.publisher.FluxJust.(FluxJust.java:60) 处导致 NPE

Changing the @Bean return type to Function<Void, List<String>> causes IllegalStateException "No function defined with name=foo" at AzureSpringFunctionInitializer.lookup将 @Bean 返回类型更改为Function<Void, List<String>>会导致 AzureSpringFunctionInitializer.lookup 中的 IllegalStateException“No function defined with name=foo”

Adding a dummy int parameter works.添加虚拟 int 参数有效。

PS Ideally I don't even need the return value so instead of Supplier I would make it a Runnable, but this seems completely unsupported. PS 理想情况下,我什至不需要返回值,因此我将其设为 Runnable 而不是供应商,但这似乎完全不受支持。

Any help would be appreciated.任何帮助,将不胜感激。

Support for supplier and consumer is added in Spring Cloud Function 3.0.0. Spring Cloud Function 3.0.0 中添加了对供应商和消费者的支持。 This is currently still a milestone.目前,这仍然是一个里程碑。

More information this change .更多信息此更改

I solved the issue, using Spring Cloud Function 2.x, by changing the signature of AzureSpringBootRequestHandler to use Optional as follows:我使用 Spring Cloud Function 2.x 解决了这个问题,方法是将 AzureSpringBootRequestHandler 的签名更改为使用 Optional,如下所示:

public class SomeFunction extends AzureSpringBootRequestHandler<Optional<?>, List<Foo>> {

    @FunctionName("some-function")
    public List<Device> execute(@HttpTrigger(name = "req",
            methods = {HttpMethod.GET},
            authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Void> request,
            ExecutionContext context) {
        return handleRequest(Optional.empty(), context);
    }
}

You'll also have to change the type of your bean to match this:您还必须更改 bean 的类型以匹配以下内容:

@Bean(name="some-function")
public Function<Optional<?>, List<Device>> someFunction() {
    return v -> fooService.bar();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM