Java 如何将我的spring boot应用程序转移到aws lambda

Java 如何将我的spring boot应用程序转移到aws lambda,java,amazon-web-services,spring-boot,api,aws-lambda,Java,Amazon Web Services,Spring Boot,Api,Aws Lambda,我用SpringBoot构建了一个非常基本的RESTAPI,目前已将其部署在AWSEC2上。我想尝试AWS lambda,但是,我仍然不清楚我需要对代码进行哪些更改才能使其作为lambda函数运行。我在网上查过教程,但到目前为止没有一本有效 为了作为aws lambda函数运行,我应该对我的文件进行哪些添加(例如添加LambdaHandler.Java和sam.yml等)?让这种应用程序作为aws lambda函数运行相对容易吗?还是我应该坚持使用ec2 我的项目结构如下: |-- produc

我用SpringBoot构建了一个非常基本的RESTAPI,目前已将其部署在AWSEC2上。我想尝试AWS lambda,但是,我仍然不清楚我需要对代码进行哪些更改才能使其作为lambda函数运行。我在网上查过教程,但到目前为止没有一本有效

为了作为aws lambda函数运行,我应该对我的文件进行哪些添加(例如添加LambdaHandler.Java和sam.yml等)?让这种应用程序作为aws lambda函数运行相对容易吗?还是我应该坚持使用ec2

我的项目结构如下:

|-- product-service
    |-- .idea
    |-- scripts
    |-- src
        |-- main
            |-- java
                |-- com.product
                    |-- controllers
                    |-- domain
                    |-- dto
                    |-- repositories
                    |-- services
                    |-- util
                    |-- Application.java
            |-- resources
        |-- test
    |-- target
    |-- appspec.yml
    |-- buildspec.yml
    |-- product-service.iml
    |-- pom.xml
Application.java如下所示:

package com.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
package com.product.controllers;

import com.product.dto.ProductDTO;
import com.product.dto.ProductListDTO;
import com.product.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping("/")
    public String getHomePage() {
        return "Welcome to the home page!";
    }

    @RequestMapping("/products")
    public List<ProductListDTO> getAllProducts() {
        return productService.getAllProducts();
    }

    @RequestMapping("/products/{id}")
    public ProductDTO getProductById(@PathVariable("id") String id) {
        return productService.getProductById(id);
    }
}
控制器如下所示:

package com.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
package com.product.controllers;

import com.product.dto.ProductDTO;
import com.product.dto.ProductListDTO;
import com.product.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping("/")
    public String getHomePage() {
        return "Welcome to the home page!";
    }

    @RequestMapping("/products")
    public List<ProductListDTO> getAllProducts() {
        return productService.getAllProducts();
    }

    @RequestMapping("/products/{id}")
    public ProductDTO getProductById(@PathVariable("id") String id) {
        return productService.getProductById(id);
    }
}
package com.product.controllers;
导入com.product.dto.ProductDTO;
导入com.product.dto.ProductListDTO;
导入com.product.services.ProductService;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.*;
导入java.util.List;
@RestController
类ProductController{
@自动连线
产品服务产品服务;
@请求映射(“/”)
公共字符串getHomePage(){
返回“欢迎访问主页!”;
}
@请求映射(“/products”)
公共列表getAllProducts(){
return productService.getAllProducts();
}
@请求映射(“/products/{id}”)
public ProductDTO getProductById(@PathVariable(“id”)字符串id){
返回productService.getProductById(id);
}
}
然后使用-mvn clean install将其内置到jar文件中。您需要重构(稍微重写)以执行从Spring启动应用程序(Rest API)到基于Lambda的迁移

两个音符

A

  • 您可以为多个API接近单个Lambda 或
  • 您可以为多个API接近多个Lambda
  • B.您可以使用Dagger(或类似的)替换Spring依赖项

    C.您可以将服务/dao类从Spring Boot迁移到Lambda


    D.设置AWS CloudWatch调度程序以预热Lambda如果需要。

    请注意,spring boot和lambda通常不是很好的匹配,因为spring boot应用程序的启动时间非常明显,因此请求可能需要几秒钟才能完成,因为应用程序必须启动。在这种情况下,您是否认为只使用ec2而不使用ec2可能是明智的没有服务器?我从配置的aurora数据库转到了无服务器数据库,它做得很好。