【问题标题】:Accessing AWS Lambda Context from Spring Cloud Function从 Spring Cloud Function 访问 AWS Lambda 上下文
【发布时间】:2019-04-13 07:15:13
【问题描述】:

我正在使用 Spring Cloud Function 1.0.0.RELEASE 和相应的 AWS 适配器在 AWS lambda 中运行它。有没有办法从 Spring 应用程序上下文中检索 lambda 函数上下文?

我知道如果你自己实现了RequestHandler接口,那么你会得到Context对象作为handleRequest方法的第二个参数(见下文),但是由于SpringBootRequestHandler正在处理这个,我不清楚如何访问Context目的。有什么想法吗?

直接实现RequestHandler的例子

public class LambdaRequestHandler implements RequestHandler<String, String> {

    public String handleRequest(String input, Context context) {
        context.getLogger().log("Input: " + input);
        return "Hello World - " + input;
    }
}

将RequestHandler的实现推迟到SpringBootRequestHandler

public class SomeFunctionHandler 
      extends SpringBootRequestHandler<SomeRequest, SomeResponse> {
}

【问题讨论】:

    标签: amazon-web-services spring-boot aws-lambda spring-cloud


    【解决方案1】:

    如果您将 Function 公开为 bean,您可以简单地 Autowire Context 对象。

    例子:

    
        @Autowired
        private Context context;
    
        @Bean
        public Function<String, String> uppercase() {
            logger.info("ARN=" + context.getInvokedFunctionArn());
            return value -> {
                if (value.equals("exception")) {
                    throw new RuntimeException("Intentional exception which should result in HTTP 417");
                }
                else {
                    return value.toUpperCase();
                }
            };
        }
    

    来源:this 答案。

    【讨论】:

      【解决方案2】:

      SomeFunctionHandler 扩展了SpringBootRequestHandler,因此它可以覆盖handleRequest 方法来访问AWS lambda Context 对象。

      public class SomeFunctionHandler extends SpringBootRequestHandler<SomeRequest, SomeResponse> {
      
          private static final Logger logger = LoggerFactory.getLogger(SomeFunctionHandler.class);
      
          @Override
          public Object handleRequest(SomeRequest event, Context context) {
              logger.info("ARN=" + context.getInvokedFunctionArn());
              return super.handleRequest(event, context);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-01
        • 1970-01-01
        • 2019-11-08
        • 2017-10-14
        • 2018-06-22
        • 2022-06-16
        • 2017-03-10
        • 2017-07-15
        • 2022-09-28
        相关资源
        最近更新 更多