如何使用 Spring Cloud Function 公开多个函数端点? [英] How to expose multiple function endpoints with Spring Cloud Function?
问题描述
我有一个基本的 Spring Cloud Function 应用程序,其中包含两个声明的函数小写"和大写".如果我将应用程序创建为普通的 @SpringBootApplication 并将两个函数注释为 @Beans(功能 bean),则一切正常.这两个函数都通过单独的 HTTP 端点公开,我可以通过以下方式调用这些函数:
I have a basic Spring Cloud Function application with two declared functions "lowercase" and "uppercase". If I create the application as a normal @SpringBootApplication and annotate both functions as @Beans (functional beans), everything works just fine. Both functions are exposed via seperate HTTP endpoints and I'm able to call the functions via:
- curl localhost:8080/uppercase -H "Content-Type: text/plain" -d 'My输入文本'
- curl localhost:8080/lowercase -H "Content-Type:text/plain" -d '我的输入文本'
现在我将应用程序的主类转换为函数形式"以缩短应用程序启动时间(如官方文档中所述:http://cloud.spring.io/spring-cloud-function/multi/multi__functional_bean_definitions.html):
Now I converted the application's main class to "functional form" to improve application startup time (as adviced in the official doc: http://cloud.spring.io/spring-cloud-function/multi/multi__functional_bean_definitions.html):
@SpringBootConfiguration
public class LambdaMicroserviceApplication implements ApplicationContextInitializer<GenericApplicationContext> {
public Function<String, String> uppercase() {
return String::toUpperCase;
}
public Function<String, String> lowercase() {
return String::toLowerCase;
}
public static void main(String[] args) {
FunctionalSpringApplication.run(LambdaMicroserviceApplication.class, args);
}
@Override
public void initialize(GenericApplicationContext context) {
context.registerBean("uppercase", FunctionRegistration.class,
() -> new FunctionRegistration<>(uppercase())
.type(FunctionType.from(String.class).to(String.class)));
context.registerBean("lowercase", FunctionRegistration.class,
() -> new FunctionRegistration<>(lowercase())
.type(FunctionType.from(String.class).to(String.class)));
}
}
问题:
现在只有一个单个端点直接暴露在根路径上:
Only one single endpoint now gets exposed directly at the root path:
curl localhost:8080/-H "Content-Type: text/plain" -d '我的输入文本'
它在内部调用大写"函数,而不管 initialize 函数中 bean 的注册顺序.
It calls the "uppercase" function internally, regardless of registration order of the beans in the initialize function.
问题:
有没有办法通过它们的专用端点再次调用两个函数:localhost:8080/uppercase 和 localhost:8080/lowercase?
Is there a way to call both functions again via their dedicated endpoints: localhost:8080/uppercase and localhost:8080/lowercase?
推荐答案
原来这其实是 Spring Cloud Function 函数形式中缺失的一个功能.现已在 2.1.0.M1 版本中实现.
It turned out that this is actually a missing functionality in the functional form of Spring Cloud Function. It is now implemented in version 2.1.0.M1.
参见:https://github.com/spring-cloud/spring-cloud-function/issues/293
这篇关于如何使用 Spring Cloud Function 公开多个函数端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!