我在Elastic Beanstalk上尝试将Spring Boot运行起来

由于在Elastic Beanstalk上推出Spring Boot相对较少,所以我将其作为备忘录留下来。
为了使用Spring Boot的内嵌Tomcat,我们使用的Beanstalk平台是Java而不是Tomcat。
此外,我们将在CentOS7环境下进行操作。我们将按照CentOS7 -> Elastic Beanstalk的流程进行部署。

在工作环境中安装Java。

sudo yum install java-1.8.0-openjdk-devel.x86_64 -y

在工作环境中安装Gradle。

cd /tmp
wget https://services.gradle.org/distributions/gradle-2.10-all.zip
sudo unzip -n /tmp/gradle-2.10-all.zip -d /usr/local/
sudo sed -i -e 's%^PATH\(.*\)%PATH\1:/usr/local/gradle-2.10/bin%' /root/.bash_profile
sed -i -e 's%^PATH\(.*\)%PATH\1:/usr/local/gradle-2.10/bin%' ~/.bash_profile
rm -i /tmp/gradle-2.10-all.zip

在工作环境中安装 AWS CLI / EB CLI。

sudo pip install awscli
sudo pip install awsebcli

之后执行`aws configure`,将AWS配置为可以发出命令的状态。

在工作环境中,准备Spring Boot应用程序。

package trial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }
}
package trial;

import org.springframework.stereotype.Controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class TrialController
{
    @RequestMapping("/")
    @ResponseBody
    public String getReq() {
        return "Good morning, Spring Boot サンプル";
    }
}
server.port=5000

由于Beanstalk的nginx将端口号5000进行了ProxyPass处理,所以我们需要让Spring Boot在5000端口上等待连接。

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE")
  }
}

apply plugin: "java"
apply plugin: "spring-boot"

jar {
  baseName = "spring-boot-trial"
  version =  "0.0.1-SNAPSHOT"
}

repositories {
  mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
  compile("org.springframework.boot:spring-boot-starter-web")
  testCompile("org.springframework.boot:spring-boot-starter-test")
}

在工作环境中,构建Spring Boot应用程序

请转到项目目录下,并使用Gradle进行构建。

gradle build

在构建成功后,确认在build/libs目录下创建了一个jar文件。确认后,将创建的jar文件复制到项目目录的顶层。

在工作环境中,创建 Procfile 并将其添加到 Jar 文件中。

请将项目目录移动到根目录,并在一个名为Procfile的文件中记录下述内容并保存。

web: java -jar 作成したjarファイル名(今回はspring-boot-trial-0.0.1-SNAPSHOT.jar)

如果有JVM选项(比如Xms等),请适当地添加。
将已创建的Procfile附加到上述创建的jar文件中。

zip 作成したjarファイル(今回はspring-boot-trial-0.0.1-SNAPSHOT.jar) Procfile

在工作环境中,准备 Elastic Beanstalk。

转到项目目录的最底层。

eb init

执行。

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-southeast-1 : Asia Pacific (Singapore)
7) ap-southeast-2 : Asia Pacific (Sydney)
8) ap-northeast-1 : Asia Pacific (Tokyo)
9) sa-east-1 : South America (Sao Paulo)
10) cn-north-1 : China (Beijing)
(default is 3): 8

Select an application to use
1) 初めての Elastic Beanstalk アプリケーション
2) [ Create new Application ]
(default is 1): 1

Enter Application Name
(default is "○○○○○"):
Application ○○○○○ has been created.

Select a platform.
1) Node.js
2) PHP
3) Python
4) Ruby
5) Tomcat
6) IIS
7) Docker
8) Multi-container Docker
9) GlassFish
10) Go
11) Java
(default is 1): 11

Select a platform version.
1) Java 8
2) Java 7
(default is 1): 1
Do you want to set up SSH for your instances?
(y/n): y

Select a keypair.
1) □□□□
2) [ Create new KeyPair ]
(default is 2): 1

请选择/输入适当的环境名称和密钥对。

从工作环境中创建和部署 Elastic Beanstalk 环境。

移动到项目目录的根目录下方。

eb create 環境名(springboot-dev等) \
--tier webserver \
--instance_type インスタンスサイズ(t2.micro等) \
--single \
--vpc.id VPCのID(vpc-xxxxxxxx) \
--vpc.ec2subnets SubnetのID(subnet-xxxxxxxx) \
--vpc.publicip \
--vpc.securitygroups セキュリティグループのID(sg-xxxxxxxx) \

执行操作。
※以上是不使用ELB的单一配置。

等待一段时间后,AWS将完成环境的建立。您可以在AWS控制台的弹性 Beanstalk 页面上查看所创建环境的URL,并在浏览器中确认。

Good morning, Spring Boot サンプル

屏幕会显示。

广告
将在 10 秒后关闭
bannerAds