日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

部署微服务– Spring Boot fatjar到Amazon Elastic Beanstalk

發(fā)布時間:2023/12/3 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 部署微服务– Spring Boot fatjar到Amazon Elastic Beanstalk 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近,我正在研究概念驗證的Web應(yīng)用程序,我想將其部署到公共云以進(jìn)行快速演示。

我決定使用Amazon,因為我已經(jīng)有過使用它的經(jīng)驗。 亞馬遜提供了幾種不同的方式來部署Java Web應(yīng)用程序。

EC2使我們可以靈活地在機箱上安裝和配置任何我們想要的東西。 對于想要控制其應(yīng)用程序的部署和運行方式的人來說,這是一個非常靈活的解決方案,但是缺點是我們必須自己完成大部分服務(wù)器安裝和配置以及應(yīng)用程序部署任務(wù)。

Elastic Beanstalk是一項易于使用的服務(wù),可自動處理部署,自動擴展,負(fù)載平衡和運行狀況監(jiān)視。 只需單擊幾下,任何人都可以使用Elastic Beanstalk將Web應(yīng)用程序部署到Amazon云。

我決定使用快速簡便的Elastic Beanstalk選項...

Elastic Beanstalk具有多種部署Java應(yīng)用程序的方式:

  • 使用Docker
  • 將War文件部署到Tomcat服務(wù)器
  • 部署從命令行執(zhí)行的fatjar
  • 在本文中,我將使用Fatjar來介紹該選項,該Fatjar基本上是一個jar文件,其中捆綁了所有類和jar依賴項。

    為了使其在亞馬遜上正常工作,必須將jar文件放在一個zip文件中。

    創(chuàng)建JAR和ZIP文件

    在本文中,我將使用Maven來創(chuàng)建上述的jar和zip文件。 讓我們來看一個示例pom.xml文件,看看它是如何完成的:

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>...<groupId>com.kaviddiss</groupId><artifactId>spring-boot-aws-beanstalk</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging>...<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath /><!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>...</dependencies><build><finalName>${project.artifactId}</finalName><plugins>...<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin?<plugin><!-- NOTE: We don't need a groupId specification because the group is org.apache.maven.plugins ...which is assumed by default. --><artifactId>maven-assembly-plugin</artifactId><version>2.6</version><executions><execution><id>make-zip</id><!-- this is used for inheritance merges --><phase>package</phase><!-- bind to the packaging phase --><goals><goal>single</goal></goals></execution></executions><configuration><appendAssemblyId>false</appendAssemblyId><descriptors><descriptor>src/assembly/zip.xml</descriptor></descriptors></configuration></plugin></plugins></build> </project>

    該文件基于為http://start.spring.io/上的 Spring Boot Web應(yīng)用程序生成的pom.xml文件,它包括一些其他更改:

  • 確保包裝標(biāo)簽設(shè)置為jar
  • spring-boot-maven-plugin將負(fù)責(zé)創(chuàng)建jar文件,包括文件本身內(nèi)部的所有jar依賴項
  • 使用src / assembly / zip.xml描述符文件配置maven-assembly-plugin以從jar文件生成zip文件
  • 在pom.xml文件中配置了maven-assembly-plugin之后,我們還需要配置zip.xml描述符,該描述符告訴程序集插件如何構(gòu)造zip文件本身。

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"><id>zip</id><formats><format>zip</format></formats><includeBaseDirectory>false</includeBaseDirectory><fileSets><fileSet><directory>${project.build.directory}</directory><includes><include>${project.artifactId}.jar</include></includes><outputDirectory>.</outputDirectory></fileSet></fileSets> </assembly>

    正如您在上面看到的,我們將輸出配置為zip,這是許多受支持的格式之一。

    我們還配置了將jar文件包含在zip文件的頂級目錄中。 這是Amazon從命令行運行應(yīng)用程序所需要的,

    使用Spring Boot的示例微服務(wù)

    為了能夠測試Maven配置,我創(chuàng)建了一個簡單的Spring Boot Web應(yīng)用程序(請參見下文)。

    應(yīng)用程序

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@SpringBootApplication @Controller public class Application { ? ?@RequestMapping(value = "/hello") ? public @ResponseBody String helloWorld() { ? return "Hello, World!"; ? } ? ? public static void main(String[] args) { ? SpringApplication.run(Application.class, args); ? ?} }

    這個Application類是Web應(yīng)用程序的主類,它將是從命令行運行fatjar的入口點

    部署到Elastic Beanstalk

    如果您尚未注冊AWS Elastic Beanstalk,則可以單擊以下鏈接: https : //console.aws.amazon.com/elasticbeanstalk 。

    選擇Amazon Elastic Beanstalk平臺

    進(jìn)入控制臺后,單擊“為應(yīng)用程序創(chuàng)建新環(huán)境”。 然后單擊“創(chuàng)建Web服務(wù)器”,然后在平臺下拉列表中選擇“ Java”,然后單擊“立即啟動”。

    設(shè)置環(huán)境變量

    AWS將nginx代理服務(wù)器配置為將請求轉(zhuǎn)發(fā)到在端口8080上運行的應(yīng)用程序。為了告訴nginx有關(guān)端口8080的信息,請在配置->軟件配置中添加一個環(huán)境變量,其鍵和值設(shè)置為PORT = 8080。

    就是這樣。 如果一切順利,您應(yīng)該可以在AWS上訪問示例Web應(yīng)用程序!

    翻譯自: https://www.javacodegeeks.com/2016/03/deploying-microservice-spring-boot-fatjar-amazon-elastic-beanstalk.html

    總結(jié)

    以上是生活随笔為你收集整理的部署微服务– Spring Boot fatjar到Amazon Elastic Beanstalk的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。