java less函数_AWS Serverless部署java api(LAMBDA篇)

本文介绍了如何将一个Spring框架的Java应用部署到AWS Lambda,使用aws-serverless-java-container-spring组件,配置StreamLambdaHandler和SpringApiConfig,并展示了如何处理数据库连接。在完成代码配置后,通过Lambda控制台创建函数并测试,成功实现Java API在Lambda上的运行。
摘要由CSDN通过智能技术生成

本文承接RDS for MySQL,前面我们已经部署好了数据库服务,并测试了连通性,这里则开始部署我们的代码,并连通rds服务.

可以以官方demo为例,或者引入aws提供的aws-serverless-java-container-spring组件,进行部分改造即可。

这里介绍如何在我们的工程里添加lambda支持,我们的已有工程为Spring框架,首先在pom.xml中引入serverless依赖.

bf09c659568a

image

接着需要添加StreamLambdaHandler.class,SpringApiConfig.class.

bf09c659568a

image

两者功能在这里简单介绍一下,RequestStreamHandler是aws lambda 应用程序的入口,我们声明的StreamLambdaHandler继承了RequestStreamHandler,并加载了初始化时,需要扫描的方法类,这些类都在SpringApiConfig中注解引入。

RequestStreamHandler.class

public class StreamLambdaHandler implements RequestStreamHandler {

private static SpringLambdaContainerHandler handler;

static {

try {

handler = SpringLambdaContainerHandler.getAwsProxyHandler(SpringApiConfig.class);

} catch (ContainerInitializationException e) {

// if we fail here. We re-throw the exception to force another cold start

e.printStackTrace();

throw new RuntimeException("Could not initialize Spring framework", e);

}

}

@Override

public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)

throws IOException {

handler.proxyStream(inputStream, outputStream, context);

}

}

在SpringApiConfig中,一次性引入多个需要调用的方法类,否则发布程序后,lambda将无法读取到相应的类。

SpringApiConfig.class:

@Configuration

// We use direct @Import instead of @ComponentScan

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值