教程:在Azure上部署Java EE应用程序(第1部分)

从传统的IaaS(基础设施即服务),PaaS(平台即服务)和CaaS(容器即服务)一直到现在,基于云的应用程序开发有多种选择。 Kubernetes和Serverless(也许还有更多我可能会错过的东西!)。 可以将其视为一种频谱,而不是“一种适合所有模型的尺寸”,每种选择都有其优缺点。 最终,每种情况都是唯一的,最终选择是由需求决定的-但是知道您有“选择”可供选择总是很高兴的!

This is the first of a series of blogs that will walk you through one of the options of running Java EE applications on Azure. We will follow the most basic approach of deploying our Java EE app to an application server which is set up in a Virtual Machine on Microsoft Azure along with the Azure Database for PostgreSQL service as the backend database. In essence, this is the combination of IaaS (Azure VM) along with a PaaS (managed PostgreSQL on Azure)

其他选项,例如容器和Kubernetes,将在以后的文章中介绍

The example used in the blog post is a simple three-tier application that uses Java EE 8 specifications such as JAX-RS, EJB, CDI, JPA, JSF, Bean Validation. We will use the Payara Server to deploy the application and use PostgreSQL as the relational database.

在本教程的过程中,我们将介绍:

  • Azure上的Postgres和虚拟机设置在虚拟机上设置Payara服务器配置和安装Java EE应用程序探索其功能

Except for minor changes, the application used in this tutorial has been adapted from this project by Reza Rahman

Pre-requisites

You will need a Microsoft Azure account and the Azure CLI to work through the tutorial.

If you don't have a Microsoft Azure account, go ahead and sign up for a free one!. The Azure CLI is a cross-platform command-line experience for managing Azure resources - please install it using these instructions.

First things first...

使用将用于本教程的Azure CLI设置Azure订阅ID。

设置您的Azure订阅ID

    export AZURE_SUBSCRIPTION_ID=[to be filled]
    az account set --subscription $AZURE_SUBSCRIPTION_ID

创建一个资源组,其中将包含您将在本教程中创建的所有服务(资源)。 资源组就像一个逻辑容器,其中包含用于Azure解决方案的相关资源。 资源组包括您要作为组管理的那些资源。

创建资源组

    export AZURE_RESOURCE_GROUP_NAME=[to be filled]
    export AZURE_LOCATION=[to be filled]
    az group create --name $AZURE_RESOURCE_GROUP_NAME --location $AZURE_LOCATION

Install Postgres on Azure

一种zure Database for PostgreSQL is a relational database service based on the open-source Postgres database engine. It's a fully managed database-as-a-service offering which is available in two deployment options, as a single server and as a Hyperscale (Citus) cluster

在本教程中,我们将使用单服务器选项

We will use the az postgres server create command to create a Postgres server instance on Azure. First, set up some of the server properties such as the name, admin user etc.

    export AZURE_POSTGRES_SERVER_NAME=[to be filled]
    export AZURE_POSTGRES_ADMIN_USER=[to be filled]
    export AZURE_POSTGRES_ADMIN_PASSWORD=[to be filled]
    export SKU=B_Gen5_1
    export STORAGE=5120

For storage and SKU options, please refer to the documentation

然后,调用命令以启动数据库实例创建:

    az postgres server create --resource-group $AZURE_RESOURCE_GROUP_NAME --name $AZURE_POSTGRES_SERVER_NAME  --location $AZURE_LOCATION --admin-user $AZURE_POSTGRES_ADMIN_USER --admin-password $AZURE_POSTGRES_ADMIN_PASSWORD --storage-size $STORAGE --sku-name $SKU

设置过程将需要几分钟。

To check the details of the Postgres database instance you just provisioned, invoke az postgres server show command

    az postgres server show --resource-group $AZURE_RESOURCE_GROUP_NAME --name $AZURE_POSTGRES_SERVER_NAME

您应该得到一个JSON响应。 请记下fullyQualifiedDomainName属性,因为稍后将使用它来连接到Postgres实例。

格式应为:[AZURE_POSTGRES_DB_NAME] .postgres.database.azure.com

Install Virtual Machine on Azure

We will use a Virtual machine on Azure to host the Payara JavaEE application server

具体来说,这将是基于Ubuntu的Linux VM

让我们从设置虚拟机所需的信息开始

    export AZURE_VM_NAME=[to be filled]
    export AZURE_VM_USER=[to be filled]
    export AZURE_VM_PASSWORD=[to be filled]
    export VM_IMAGE=UbuntuLTS

We will use the az vm create command to create the VM instance

    az vm create --resource-group $AZURE_RESOURCE_GROUP_NAME --name $AZURE_VM_NAME --image $VM_IMAGE --admin-username $AZURE_VM_USER --admin-password $AZURE_VM_PASSWORD

VM置备将花费几分钟。

You need to get the public IP address of the VM. Do so using the az vm list-ip-addresses command

    az vm list-ip-addresses --resource-group $AZURE_RESOURCE_GROUP_NAME --name $AZURE_VM_NAME

您将看到一个JSON响应-查看publicIpAddresses并记下值IP地址属性。 将其配置为环境变量,因为您将在后续步骤中使用它

    export VM_IP=[to be filled]

Allow VM to access the Postgres database

The Postgres database is not accessible by default. Use the az postgres server firewall-rule create command to create a firewall rule to explicitly allow the VM to access the Postgres instance. This will allow the JavaEE application deployed inside the VM to communicate with Postgres.

    export FIREWALL_RULE_NAME=AllowJavaEECafeAppOnVM

    az postgres server firewall-rule create --resource-group $AZURE_RESOURCE_GROUP_NAME --server $AZURE_POSTGRES_SERVER_NAME --name $FIREWALL_RULE_NAME --start-ip-address $VM_IP --end-ip-address $VM_IP

Install Payara server on the Virtual Machine

Payara Server is an open source application server derived from GlassFish that supports reliable and secure deployments of Java EE (Jakarta EE) and MicroProfile applications in any environment: on-premise, in the cloud or hybrid. Check out the project on GitHub or dive into its documentation to learn more!

使用您指定的用户名和VM IP SSH进入刚刚配置的Linux VM

    ssh $AZURE_VM_USER@$VM_IP

提示时输入密码。 登录虚拟机后,请继续以下步骤。

Install required toolset

在安装Payara服务器之前,我们需要设置一些东西,例如JDK等。

    sudo apt-get update
    sudo apt install openjdk-8-jdk
    sudo apt install maven

Setup Payara server

我们正在使用Payara服务器版本5.193.1这是撰写本教程时的最新内容。 该设置仅涉及下载和提取服务器zip文件。

    export PAYARA_VERSION=5.193.1

    wget https://s3-eu-west-1.amazonaws.com/payara.fish/Payara+Downloads/$PAYARA_VERSION/payara-$PAYARA_VERSION.zip
    sudo apt install unzip
    unzip payara-$PAYARA_VERSION.zip

确认,运行ls〜/ payara5 /

使用启动服务器管理员

    ~/payara5/bin/asadmin start-domain

服务器启动将需要一些时间。 您应该看到以下日志:

    Waiting for domain1 to start ..................
    Successfully started the domain : domain1
    domain  Location: /home/abhishgu/payara5/glassfish/domains/domain1
    Log File: /home/abhishgu/payara5/glassfish/domains/domain1/logs/server.log
    Admin Port: 4848
    Command start-domain executed successfully.

Setup and deploy the application

现在我们已经启动并运行了VM和Payara服务器,现在可以部署应用程序了! 克隆git仓库

    git clone https://github.com/abhirockzz/javaee-on-azure-iaas
    export APP_FOLDER_NAME=javaee-on-azure-iaas

的web.xml文件(在javaee-on-azure-iaas / src / main / webapp / WEB-INF)需要使用Azure上Postgres数据库的JDBC URL更新。

这存在于<url>的属性<data-source部分,其格式如下:

    jdbc:postgresql://POSTGRES_FQDN:5432/postgres?user=AZURE_POSTGRES_ADMIN_USER@=AZURE_POSTGRES_SERVER_NAME&amp;password=AZURE_POSTGRES_ADMIN_PASSWORD&amp;sslmode=require

这是构成JDBC URL一部分的占位符列表:

  • POSTGRES_FQDN的价值fullyQualifiedDomainName对于Postgres实例AZURE_POSTGRES_ADMIN_USER使用用于配置PG的管理员用户名AZURE_POSTGRES_SERVER_NAME服务器名称用于配置PGAZURE_POSTGRES_ADMIN_PASSWORD使用用于设置PG的管理员密码

设置所需的值

    export POSTGRES_FQDN=[to be filled]
    export AZURE_POSTGRES_ADMIN_USER=[to be filled]
    export AZURE_POSTGRES_SERVER_NAME=[to be filled]
    export AZURE_POSTGRES_ADMIN_PASSWORD=[to be filled]

只需使用这些命令即可替换

    export FILE_NAME=javaee-on-azure-iaas/src/main/webapp/WEB-INF/web.xml

    sed -i 's/POSTGRES_FQDN/'"$POSTGRES_FQDN"'/g' $FILE_NAME
    sed -i 's/AZURE_POSTGRES_SERVER_NAME/'"$AZURE_POSTGRES_SERVER_NAME"'/g' $FILE_NAME
    sed -i 's/AZURE_POSTGRES_ADMIN_USER/'"$AZURE_POSTGRES_ADMIN_USER"'/g' $FILE_NAME
    sed -i 's/AZURE_POSTGRES_ADMIN_PASSWORD/'"$AZURE_POSTGRES_ADMIN_PASSWORD"'/g' $FILE_NAME

这是例如 什么<data-source>部分如下所示:

    <data-source>
        <name>java:global/JavaEECafeDB</name>
        <class-name>org.postgresql.ds.PGPoolingDataSource</class-name>
        <url>jdbc:postgresql://foobar-pg.postgres.database.azure.com:5432/postgres?user=foobar@foobar-pg&amp;password=foobarbaz&amp;sslmode=require</url>
    </data-source>

现在已配置该应用程序。 让我们来构建它!

    mvn clean install -f $APP_FOLDER_NAME/pom.xml

您应该有可用的WAR文件。 确认

    ls -lrt $APP_FOLDER_NAME/target | grep javaee-cafe.war

作为应用程序设置过程的最后一步,让我们下载Postgres的Java驱动程序并将其添加到Payara

我们正在使用驱动程序版本42.2.8
    export PG_DRIVER_JAR=postgresql-42.2.8.jar
    wget https://jdbc.postgresql.org/download/$PG_DRIVER_JAR

将JAR添加到Payara,只需调用asadmin添加库

    ~/payara5/glassfish/bin/asadmin add-library $PG_DRIVER_JAR

最后,要部署WAR文件,只需将其复制到域中自动部署夹

    cp $APP_FOLDER_NAME/target/javaee-cafe.war ~/payara5/glassfish/domains/domain1/autodeploy

部署将需要一些时间。 同时,您可以使用以下方法跟踪日志:

    tail -f ~/payara5/glassfish/domains/domain1/logs/server.log

您应该会看到日志消息,指示成功部署了爪哇咖啡厅应用

    [2019-11-18T13:34:21.317+0000] [Payara 5.193] [INFO] [NCLS-DEPLOYMENT-02035] [javax.enterprise.system.tools.deployment.autodeploy] [tid: _ThreadID=104 _ThreadName=payara-executor-service-scheduled-task] [timeMillis: 1574084061317] [levelValue: 800] [[
    [AutoDeploy] Successfully autodeployed : /home/abhishgu/payara5/glassfish/domains/domain1/autodeploy/javaee-cafe.war.]]

Explore the application

现在该试驾我们的JavaEE应用程序了! 首先,我们可以使用网络浏览器访问该应用程序。 但是,就像Postgres实例一样,默认情况下,托管Payara服务器和应用程序的虚拟机也受到保护,即您不能从公共Internet访问它。

We need to create a firewall rule using the az vm open-port to access it from our local machine. We just need to expose port 8080 since that's the default HTTP port which Payara server uses

    az vm open-port --port 8080 --resource-group $AZURE_RESOURCE_GROUP_NAME --name $AZURE_VM_NAME

Access the JSF front end

使用浏览器访问http:// [ENTER_VM_IP]:8080 / javaee-cafe。 您可以使用用户界面创建,删除和查看咖啡。

Use the REST API

该应用程序还公开了一个REST API,用于创建,删除和列出咖啡。

    export VM_IP=[to be filled]

制作咖啡

    curl -X POST $VM_IP:8080/javaee-cafe/rest/coffees -d '{"name":"cappuccino","price":"10"}' -H "Content-Type: application/json"
    curl -X POST $VM_IP:8080/javaee-cafe/rest/coffees -d '{"name":"caffe-latte","price":"15"}' -H "Content-Type: application/json"

拿到所有咖啡

    curl -H "Accept: application/json" $VM_IP:8080/javaee-cafe/rest/coffees

您应该看到一个JSON响应,其中列出了您刚刚添加的两个咖啡选项

通过ID喝咖啡

    curl -H "Accept: application/json" $VM_IP:8080/javaee-cafe/rest/coffees/1

通过ID删除咖啡

    curl -X DELETE $VM_IP:8080/javaee-cafe/rest/coffees/1
    curl -H "Accept: application/json" $VM_IP:8080/javaee-cafe/rest/coffees

注意卡布奇诺咖啡现在已删除

Clean up resources

探索完应用程序后,您可以删除资源。 由于我们使用了资源组,因此执行单个命令很容易。

请注意,这将删除组中的所有资源,包括您在教程中创建的资源(VM,Postgres等),以及如果您使用的是其他服务实例,已经存在资源组
    az group delete --name $AZURE_RESOURCE_GROUP_NAME

Summary

您学习了如何使用部署到虚拟机的应用程序服务器以及托管数据库产品提供标准Java EE应用程序到Azure的长期持久性。

如前所述,每个选项都有其优点和缺点。 在这种情况下,您可以完全控制应用程序,其部署基础结构,扩展方式等。另一方面,请记住,管理基础结构,为应用程序调整大小,保护其安全等是一组职责。 作为应用程序功能的一部分,您必须将其与交付核心业务逻辑一起进行。

下一部分将深入探讨如何使用Docker容器平台来部署Java EE应用程序。 敬请关注!

from: https://dev.to//itnext/deploying-java-ee-apps-to-azure-part-1-217e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值