如何将我的Spring Boot应用程序迁移到AWS Lambda。

huangapple go评论68阅读模式
英文:

How do I transfer my spring boot app to aws lambda

问题

以下是翻译好的部分:

我用spring-boot构建了一个相当基本的REST API,并且目前已经在AWS EC2上部署。我想尝试一下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);
    }
}

Controller的内容如下:

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 "欢迎访问主页!";
    }

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

    @RequestMapping("/products/{id}")
    public ProductDTO getProductById(@PathVariable("id") String id) {
        return productService.getProductById(id);
    }
}

然后使用命令 -mvn clean install 将其构建成jar文件。

英文:

I have got a pretty basic REST API I have built with spring-boot and I currently have it deployed on AWS EC2. I would like to try out AWS lambda, however, it is still unclear to me what changes I need to make to my code in order for it to run as lambda function. I have looked up tutorials online but none of them have worked so far.

What additions should I make to my files (such as adding LambdaHandler.Java and sam.yml etc.) in order to run this as aws lambda function? Is it relatively easy to make this sort of app run as aws lambda function or should I just stick with ec2?

my project structure is as follows:

|-- 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 looks like this:

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);
    }
}

Controller looks like this:

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(&quot;/&quot;)
    public String getHomePage() {
        return &quot;Welcome to the home page!&quot;;
    }

    @RequestMapping(&quot;/products&quot;)
    public List&lt;ProductListDTO&gt; getAllProducts() {
        return productService.getAllProducts();
    }

    @RequestMapping(&quot;/products/{id}&quot;)
    public ProductDTO getProductById(@PathVariable(&quot;id&quot;) String id) {
        return productService.getProductById(id);
    }
}

This then gets built into jar file using -mvn clean install

答案1

得分: 1

需要对Spring Boot应用程序(Rest APIs)进行一些重构(稍作改写),以便迁移到基于Lambda的架构。

几点注意事项:

A.

  1. 您可以选择为多个API使用单个Lambda。
    或者
  2. 您可以为多个API使用多个Lambda。

B. 您可以使用Dagger(或类似的工具)来替代Spring的依赖项。

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

D. 如果需要,可以设置AWS CloudWatch Scheduler来预热Lambda函数。

英文:

You need to refactor (rewrite a bit) to perform the migrate from Spring Boot application (Rest APIs) to Lambda based.

Couple notes

A.

  1. You can approach Single Lambda for multiple APIs
    or
  2. You can approach Multiple Lambda for multiple APIs

B. You can replace Spring dependencies by using Dagger (or similar ones)

C. You can migrate your service/dao classses from Spring Boot to Lambda

D. Setup AWS CloudWatch Scheduler to warm Lambda(s) if required.

huangapple
  • 本文由 发表于 2020年10月13日 15:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64330846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定