将Quarkus应用程序部署到AWS Elastic Beanstalk
Elastic Beanstalk允許在AWS云中部署和管理應(yīng)用程序,而無(wú)需了解運(yùn)行這些應(yīng)用程序的基礎(chǔ)架構(gòu)。
使用Elastic Beanstalk,您可以運(yùn)行可處理HTTP請(qǐng)求的網(wǎng)站,Web應(yīng)用程序或Web API,但也可以運(yùn)行輔助應(yīng)用程序以運(yùn)行長(zhǎng)任務(wù)。 Elastic Beanstalk支持多個(gè)預(yù)配置平臺(tái),包括Go , .NET或Java (僅Java 8),也支持通用Docker平臺(tái)。
您只需使用AWS CLI , AWS EB CLI或Elastic Beanstack console上傳應(yīng)用程序,然后Elastic Beanstalk即可自動(dòng)處理其余部分。
在此博客文章中,您將學(xué)習(xí)如何在Elastic Beanstalk上使用基于Quarkus的應(yīng)用程序啟動(dòng)單容器Docker環(huán)境。
注意:此博客沒(méi)有描述從頭開(kāi)始創(chuàng)建應(yīng)用程序。 相反,它基于我為Quarkus入門博客文章創(chuàng)建的Quarkus Pet Clinic REST API應(yīng)用程序 。 可以在Github上找到源代碼: https : //github.com/kolorobot/quarkus-petclinic-api
TL; DR:創(chuàng)建軟件包并上傳到Elastic Beanstalk
在Elastic Beanstalk控制臺(tái)中創(chuàng)建新的應(yīng)用程序
如果您還不是AWS客戶,則需要?jiǎng)?chuàng)建一個(gè)AWS賬戶。 通過(guò)注冊(cè),您可以訪問(wèn)Elastic Beanstalk和其他所需的AWS服務(wù)。
- 使用以下鏈接打開(kāi)Elastic Beanstalk控制臺(tái): https ://us-west-2.console.aws.amazon.com/elasticbeanstalk/home?region=us-west-2#/gettingStarted?applicationName =Pet診所API
- 對(duì)于Platform選擇Docker
- 對(duì)于Application Code選擇Sample Application
- 選擇Configure more options
- 在列表中找到Database ,然后單擊Modify
- 點(diǎn)擊Create app
Elastic Beanstalk將使用所有必需的資源(包括RDS)為您創(chuàng)建示例應(yīng)用程序。
創(chuàng)建應(yīng)用程序后,您將可以看到該應(yīng)用程序的鏈接。
注意:以上步驟基于官方文檔: https : //docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.CreateApp.html
Preapare應(yīng)用程序包
- 克隆存儲(chǔ)庫(kù)
- 導(dǎo)航到應(yīng)用程序目錄并執(zhí)行:
上面的命令創(chuàng)建具有以下內(nèi)容的軟件包:
$ unzip -l target/quarkus-petclinic-api- 1.0 . 1 -eb.zip Archive: target/quarkus-petclinic-api- 1.0 . 1 -eb.zip Length Date Time Name --------- ---------- ----- ---- 0 03 - 15 - 2020 13 : 35 config/ 2059 03 - 15 - 2020 13 : 34 Dockerfile 369 03 - 15 - 2020 13 : 34 config/application.properties 38604205 03 - 15 - 2020 13 : 35 quarkus-petclinic-api- 1.0 . 1 -runner.jar --------- ------- 38606633 4 files將應(yīng)用程序上傳到Elastic Beanstalk
- 使用Elastic Beanstalk控制臺(tái)上傳軟件包
- 導(dǎo)航到https://console.aws.amazon.com/elasticbeanstalk
而已。 在下一段中,您將學(xué)習(xí)如何使用Maven準(zhǔn)備軟件包。
循序漸進(jìn):為Elastic Beanstalk配置應(yīng)用程序
運(yùn)行時(shí)配置
讓我們從特定于Elastic Beanstalk環(huán)境的應(yīng)用程序配置開(kāi)始。
Quarkus提供了幾種在運(yùn)行時(shí)覆蓋屬性的選項(xiàng)。 我決定將這種方法與配置文件一起放在config/application.properties文件中。 Quarkus將自動(dòng)讀取該文件,并且該文件中的所有屬性都優(yōu)先于默認(rèn)值。
創(chuàng)建src/main/resources/application-eb.properties文件,并將quarkus.http.port設(shè)置為5000因?yàn)檫@是Elastic Beanstalk Web應(yīng)用程序的默認(rèn)端口。
隨著應(yīng)用程序?qū)⑦B接到RDS(PostgreSQL),下一個(gè)屬性與數(shù)據(jù)源配置有關(guān)。 RDS實(shí)例的連接信息可通過(guò)運(yùn)行容器可用的RDS_*環(huán)境屬性提供給在Elastic Beanstalk上運(yùn)行的應(yīng)用程序。 要使用此屬性,請(qǐng)?jiān)O(shè)置以下屬性:
quarkus.datasource.url=jdbc:postgresql: //${RDS_HOSTNAME}:${RDS_PORT}/${RDS_DB_NAME} quarkus.datasource.username=${RDS_USERNAME} quarkus.datasource.password=${RDS_PASSWORD}閱讀有關(guān)將應(yīng)用程序連接到RDS的更多信息: https : //docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.db.html
Dockerfile
Elastic Beanstalk使用Dockerfile構(gòu)建和運(yùn)行映像。 該文件必須位于應(yīng)用程序目錄的root目錄中。 我使用了原始的src/main/docker/Dockerfile.jvm并進(jìn)行了以下調(diào)整:
- 將config/application.properties復(fù)制到容器
- 暴露端口5000而不是8080
完整的src/main/docker/Dockerfile.eb :
FROM registry.access.redhat.com/ubi8/ubi-minimal: 8.1 ARG JAVA_PACKAGE=java- 11 -openjdk-headless ARG RUN_JAVA_VERSION= 1.3 . 5 ENV LANG= 'en_US.UTF-8' LANGUAGE= 'en_US:en' # Install java and the run-java script # Also set up permissions for user ` 1001 ` RUN microdnf install openssl curl ca-certificates ${JAVA_PACKAGE} \ && microdnf update \ && microdnf clean all \ && mkdir /deployments \ && chown 1001 /deployments \ && chmod "g+rwX" /deployments \ && chown 1001 :root /deployments \ && curl https: //repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ && chown 1001 /deployments/run-java.sh \ && chmod 540 /deployments/run-java.sh \ && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security ENV JAVA_OPTIONS= "-Djava.util.logging.manager=org.jboss.logmanager.LogManager" COPY *-runner.jar /deployments/app.jar COPY config /deployments/config EXPOSE 5000 USER 1001 ENTRYPOINT [ "/deployments/run-java.sh" ]使用Maven創(chuàng)建應(yīng)用程序包
到目前為止,已創(chuàng)建以下兩個(gè)文件:
- src/main/resources/application-eb.properties具有特定于Elastic Beanstalk環(huán)境的屬性
- src/main/docker/Dockerfile.eb具有用于Elastic Beanstack環(huán)境的容器配置。
要完成配置和配置程序包組裝,我們將使用“ Copy Rename Maven Plugin和“ Maven Assembly Plugin 。
準(zhǔn)備組裝文件
修改pom.xml并添加復(fù)制和重命名將存儲(chǔ)在最終應(yīng)用程序包zip文件中的文件的目標(biāo):
< build > < plugin > < groupId >com.coderplus.maven.plugins</ groupId > < artifactId >copy-rename-maven-plugin</ artifactId > < version >1.0</ version > < executions > < execution > < id >copy-file</ id > < phase >package</ phase > < goals > < goal >copy</ goal > </ goals > < configuration > < fileSets > < fileSet > < sourceFile >src/main/resources/application-eb.properties</ sourceFile > < destinationFile >target/eb/application.properties</ destinationFile > </ fileSet > < fileSet > < sourceFile >src/main/docker/Dockerfile.eb</ sourceFile > < destinationFile >target/eb/Dockerfile</ destinationFile > </ fileSet > </ fileSets > </ configuration > </ execution > </ executions > </ plugin > </ build >copy-file目標(biāo)將在package階段運(yùn)行,并將先前創(chuàng)建的文件(名稱已調(diào)整)復(fù)制到target/eb 。
配置程序集插件
Maven Assembly Plugin將用于創(chuàng)建應(yīng)用程序包。 將以下配置添加到pom.xml :
< build > < plugin > < artifactId >maven-assembly-plugin</ artifactId > < version >3.2.0</ version > < configuration > < descriptors > < descriptor >src/assembly/eb.xml</ descriptor > </ descriptors > </ configuration > </ plugin > </ build >現(xiàn)在,創(chuàng)建src/assembly/eb.xml指示所述組件插件來(lái)創(chuàng)建一個(gè)描述符zip含有Dockerfile , config/application.properties和Quarkus uber-jar 。 所有這三個(gè)文件將位于存檔的root中:
< assembly > < id >eb</ id > < formats > < format >zip</ format > </ formats > < includeBaseDirectory >false</ includeBaseDirectory > < files > < file > < source >target/eb/Dockerfile</ source > < outputDirectory ></ outputDirectory > < filtered >false</ filtered > </ file > < file > < source >target/eb/application.properties</ source > < outputDirectory >config</ outputDirectory > < filtered >false</ filtered > </ file > < file > < source >target/${project.build.finalName}-runner.jar</ source > < outputDirectory ></ outputDirectory > < filtered >false</ filtered > </ file > </ files > </ assembly >這樣就完成了配置。 現(xiàn)在,您可以通過(guò)運(yùn)行以下命令創(chuàng)建包(程序集):
經(jīng)過(guò)以上所有更改,我們可以創(chuàng)建程序包:
./mvnw clean package assembly:single -Dquarkus. package .uber-jar= true在本地測(cè)試軟件包
要在本地測(cè)試包,請(qǐng)運(yùn)行:
unzip target/quarkus-petclinic-api- 1.0 . 1 -eb.zip -d target/eb-dist && cd target/eb-dist docker build -t quarkus/petclinic-api-jvm-eb .在運(yùn)行容器之前,請(qǐng)啟動(dòng)數(shù)據(jù)庫(kù):
docker run -it --name petclinic-db -p 5432 : 5432 -e POSTGRES_DB=petclinic -e POSTGRES_USER=petclinic -e POSTGRES_PASSWORD=petclinic -d postgres: 11.6 -alpine運(yùn)行傳遞RDS環(huán)境變量的應(yīng)用程序并鏈接到數(shù)據(jù)庫(kù)容器:
docker run -i --rm -p 8080 : 5000 --link petclinic-db -e RDS_HOSTNAME=petclinic-db -e RDS_PORT= 5432 -e RDS_DB_NAME=petclinic -e RDS_USERNAME=petclinic -e RDS_PASSWORD=petclinic quarkus/petclinic-api-jvm-eb在瀏覽器中打開(kāi)http://localhost:8080 ,您應(yīng)該會(huì)看到主頁(yè)。
源代碼
可以在Github上找到本文的源代碼: https : //github.com/kolorobot/quarkus-petclinic-api
參考文獻(xiàn)
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/single-container-docker.html
- https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.db.html
- https://quarkus.io/guides/config#package-and-run-the-application
也可以看看
- Quarkus入門
- 使用Testcontainers和PostgreSQL進(jìn)行Quarkus測(cè)試
翻譯自: https://www.javacodegeeks.com/2020/03/deploy-quarkus-application-to-aws-elastic-beanstalk.html
總結(jié)
以上是生活随笔為你收集整理的将Quarkus应用程序部署到AWS Elastic Beanstalk的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 抖音设置了不看对方怎么恢复
- 下一篇: gradle 构建应用流程_使用Grad