首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在AWS Lambda中使用API Gateway实现函数路由的Spring Cloud Function

在AWS Lambda中使用API Gateway实现函数路由的Spring Cloud Function
EN

Stack Overflow用户
提问于 2020-01-17 06:02:34
回答 1查看 950关注 0票数 1

我尝试在AWS Lambda中部署一个具有多种功能的Spring Cloud Function

为了访问超文本传输协议,我创建了一个HTTP API Gateway (not a REST API)。我想使用这里描述的函数路由:https://cloud.spring.io/spring-cloud-static/spring-cloud-function/3.0.1.RELEASE/reference/html/spring-cloud-function.html#_function_routing

使用以下配置,RoutingFunction应根据function HTTP-Header删除对正确函数的调用

代码语言:javascript
复制
spring.cloud.function.routing-expression=headers.function

Lambda处理程序类是:

代码语言:javascript
复制
org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler

配置的函数名(FUNCTION_NAME)为:functionRouter

当我向API-Gateway发送请求时,FunctionRouter会得到一个FluxJust对象,而不是一个消息对象,因为RequestHandler似乎是一个发布者。因此,我得到了以下异常:

代码语言:javascript
复制
EL1008E: Property or field 'headers' cannot be found on object of type
'reactor.core.publisher.FluxJust' - maybe not public or not valid?:
org.springframework.expression.spel.SpelEvaluationException

org.springframework.expression.spel.SpelEvaluationException:
EL1008E: Property or field 'headers' cannot be found on object of type
'reactor.core.publisher.FluxJust' - maybe not public or not valid?

我目前的解决方法是在将请求委托给RoutingFunction之前拦截该请求,并尝试使用以下代码从HashMap重新构建有效负载:

代码语言:javascript
复制
@Autowired
RoutingFunction f;

@Bean
public Function<Message<LinkedHashMap>,?> router() {
    return value -> {
        String json = new JSONObject(value.getPayload()).toString();
        Message<?> m = MessageBuilder.createMessage(json, value.getHeaders());
        Object result = f.apply(m);
        return result;
    };
}

有没有一种在AWS中结合使用HTTP API Gateway和Function Routing的正确方法

EN

回答 1

Stack Overflow用户

发布于 2020-07-05 22:21:15

在我的项目中,我已经这样设置了function.definitionfunction.routing-expression

代码语言:javascript
复制
spring.cloud.function.definition=functionRouter
spring.cloud.function.routing-expression=headers['pathParameters']['proxy']

这将使用Spring Cloud Function提供的org.springframework.cloud.function.context.config.RoutingFunction,表达式将从路径中获取函数名。

如果你仍然想使用头文件,你可以这样做:spring.cloud.function.routing-expression=headers['headers']['function']。HTTP头被添加到headers属性的消息头中。因此,我们首先获取消息头,然后是HTTP头,然后是头键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59778471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档