在 Azure App Service 中設定 Tomcat、JBoss 或 Java SE 應用程式的資料來源

本文說明如何在 App Service 中設定 Java SE、Tomcat 或 JBoss 應用程式中的資料來源。

Azure App Service 在完全託管的服務上執行三種類型的 Java 網頁應用程式:

  • Java 標準版(SE)。 Java SE 可執行以 Java 壓縮包(JAR)套件部署的應用程式,該套件包含嵌入式伺服器,例如 Spring Boot、Quarkus、Dropwizard,或是內建 Tomcat 或 Jetty 伺服器的應用程式。
  • Tomcat。 內建的 Tomcat 伺服器可執行以網頁應用程式壓縮套件(WAR)部署的應用程式。
  • JBoss 企業應用程式平臺 (EAP):內建的 JBoss EAP 伺服器可以執行部署為 WAR 或企業封存 (EAR) 套件的應用程式。 此選項支援 Linux 應用程式,包含免費、Premium v3 及 Isolated v2 等價格層級。

備註

JBoss EAP 現在在 App Service 上支援「自備授權」(Bring Your Own License,BYOL)計費。 BYOL 允許擁有現有 Red Hat 訂閱的客戶,直接將這些授權套用到 Azure App Service 上的 JBoss EAP 部署。 欲了解更多資訊,請參閱 BYOL 對 App Service 上 JBoss EAP 的支援

設定資料來源

若要連線到 Spring Boot 應用程式中的數據源,建議您建立連接字串,並將其插入 application.properties 檔案中。

  1. 在 App Service 頁面的左側窗格,選擇 設定>環境變數。 在 Connection 字串 標籤中,選擇 新增。 為字串設定 名稱 ,將你的 JDBC 連線字串貼到 Value 欄位,並將 Type 設為 自訂。 你可以選擇性地將連接字串設為插槽設定。

    連接字串可作為名為 CUSTOMCONNSTR_<your-string-name>的環境變數,對你的應用程式開放。 例如: CUSTOMCONNSTR_exampledb

  2. 在你的 application.properties 檔案中,參考帶有環境變數名稱的連線字串。 在前述範例中,你會使用以下程式碼:

    app.datasource.url=${CUSTOMCONNSTR_exampledb}
    

欲了解更多資訊,請參閱 Spring Boot 關於資料存取外部化設定的文件。

秘訣

Linux Tomcat 容器可以在 Tomcat 伺服器中自動配置共享資料來源,只要你將環境變數 WEBSITE_AUTOCONFIGURE_DATABASE 設為 true。 你唯一能做的就是新增一個包含有效 JDBC 連線字串的應用程式設定,連結到 Oracle、SQL Server、PostgreSQL 或 MySQL 資料庫(包括連線憑證)。 App Service 會自動將對應的共享資料庫加入 /usr/local/tomcat/conf/context.xml,並使用容器中可用的驅動程式。 若想了解如何採用此方法的端對端情境,請參見 教學:在 Linux 和 MySQL 上使用 Azure App Service 建置 Tomcat 網頁應用程式

這些指示適用於所有資料庫連線。 你需要用你選擇的資料庫的驅動程式類別名稱和 JAR 檔案取代佔位符。 下表提供常見資料庫的類別名稱及驅動程式下載資料。

資料庫 駕駛組別名稱 JDBC 驅動程式
PostgreSQL org.postgresql.Driver 下載
MySQL com.mysql.jdbc.Driver 下載 (選擇 平台獨立
SQL Server com.microsoft.sqlserver.jdbc.SQLServerDriver 下載

要設定 Tomcat 使用 Java 資料庫連接性(JDBC)或 Java 持久化 API(JPA),首先自訂 Tomcat 在啟動時讀取的 CATALINA_OPTS 環境變數。 透過 App Service Maven 外掛中的應用程式設定來設定此值:

<appSettings>
    <property>
        <name>CATALINA_OPTS</name>
        <value>"$CATALINA_OPTS -Ddbuser=${DBUSER} -Ddbpassword=${DBPASSWORD} -DconnURL=${CONNURL}"</value>
    </property>
</appSettings>

或者在 Azure 入口網站的設定環境>頁面的應用程式設定標籤中設定環境變數。

接著,判斷資料來源是對某一個應用程式開放,還是所有在 Tomcat servlet 上執行的應用程式。

應用程式層級資料來源

若要設定應用程式層級資料來源:

  1. 在專案的META-INF/ 目錄中建立 context.xml 檔案。 如果 META-INF/ 目錄不存在,請加以建立。

  2. context.xml中,新增 Context 元素以將數據源連結至 JNDI 位址。 將 driverClassName 佔位符替換為本文前面表格中的驅動程序類別名稱。

    <Context>
        <Resource
            name="jdbc/dbconnection"
            type="javax.sql.DataSource"
            url="${connURL}"
            driverClassName="<insert your driver class name>"
            username="${dbuser}"
            password="${dbpassword}"
        />
    </Context>
    
  3. 更新應用程式的 web.xml ,以在應用程式中使用數據源。

    <resource-env-ref>
        <resource-env-ref-name>jdbc/dbconnection</resource-env-ref-name>
        <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
    </resource-env-ref>
    

共用伺服器層級資源

秘訣

Linux Tomcat 容器可透過以下規範自動套用 XSLT 檔案到複製到 /home/site/wwwroot 的檔案:若 server.xml.xslserver.xml.xslt 存在,檔案會套用到 Tomcat 的 server.xml。 如果 存在或 出現,檔案將會套用到 Tomcat 的

新增共用的伺服器層級資料來源需要您編輯 Tomcat 的 server.xml。 由於目錄外的 /home 檔案變更是暫時的,因此需要以程式設計方式套用對 Tomcat 設定檔的變更,如下所示:

  • 上傳啟動腳本,並在設定>設定中設定腳本路徑。堆疊設定 標籤中,將路徑加入啟動 指令 框。 你可以用 FTP 上傳啟動腳本。

你的啟動腳本會將 XSL 轉換 成該 server.xml 檔案,並將所得的 XML 檔案輸出成 /usr/local/tomcat/conf/server.xml。 啟動腳本應該安裝libxsltxlstproc,視你網頁應用程式 Tomcat 的發行版本而定,如以下範例腳本的註解所示。 你可以用 FTP 上傳你的 XSL 檔案和啟動腳本。

# Install the libxslt package on Alpine-based images:
apk add --update libxslt

# Install the xsltproc package on Debian or Ubuntu-based images:
apt install xsltproc

# Also copy the transform file to /home/tomcat/conf/
# Usage: xsltproc --output output.xml style.xsl input.xml
xsltproc --output /home/tomcat/conf/server.xml /home/tomcat/conf/transform.xsl /usr/local/tomcat/conf/server.xml

下列範例 XSL 檔案會將新的連接器節點加入至 Tomcat server.xml。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()" name="Copy">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@* | node()" mode="insertConnector">
    <xsl:call-template name="Copy" />
  </xsl:template>

  <xsl:template match="comment()[not(../Connector[@scheme = 'https']) and
                                 contains(., '&lt;Connector') and
                                 (contains(., 'scheme=&quot;https&quot;') or
                                  contains(., &quot;scheme='https'&quot;))]">
    <xsl:value-of select="." disable-output-escaping="yes" />
  </xsl:template>

  <xsl:template match="Service[not(Connector[@scheme = 'https'] or
                                   comment()[contains(., '&lt;Connector') and
                                             (contains(., 'scheme=&quot;https&quot;') or
                                              contains(., &quot;scheme='https'&quot;))]
                                  )]
                      ">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" mode="insertConnector" />
    </xsl:copy>
  </xsl:template>

  <!-- Add the new connector after the last existing connector if there is one -->
  <xsl:template match="Connector[last()]" mode="insertConnector">
    <xsl:call-template name="Copy" />

    <xsl:call-template name="AddConnector" />
  </xsl:template>

  <!-- ... or before the first engine if there's no existing connector -->
  <xsl:template match="Engine[1][not(preceding-sibling::Connector)]"
                mode="insertConnector">
    <xsl:call-template name="AddConnector" />

    <xsl:call-template name="Copy" />
  </xsl:template>

  <xsl:template name="AddConnector">
    <!-- Add new line -->
    <xsl:text>&#xa;</xsl:text>
    <!-- This is the new connector -->
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" 
               maxThreads="150" scheme="https" secure="true" 
               keystoreFile="${{user.home}}/.keystore" keystorePass="changeit"
               clientAuth="false" sslProtocol="TLS" />
  </xsl:template>

</xsl:stylesheet>

完成設定

最後,將驅動程式 JAR 放入 Tomcat 類別路徑,並重新啟動你的 App Service 應用程式。

  • 請確定 Tomcat classloader 可以使用 JDBC 驅動程式檔案,方法是將它們放在 [/home/site/lib] 目錄中。 在 Cloud Shell 中,針對每個驅動程式 JAR 執行 az webapp deploy --type=lib
az webapp deploy --resource-group <group-name> --name <app-name> --src-path <jar-name>.jar --type=lib --path <jar-name>.jar

如果您已建立伺服器層級的資料來源,請重新啟動 App Service Linux 應用程式。 Tomcat 會將 CATALINA_BASE 重設為 /home/tomcat,並使用更新後的設定。

秘訣

預設情況下,Linux JBoss 容器能自動在 JBoss 伺服器中設定共享資料來源。 你唯一需要做的就是加入一個包含有效 JDBC 連線字串的應用程式設定到 Oracle、SQL Server、PostgreSQL 或 MySQL 資料庫(包括連線憑證),然後加上 app 設定/環境變數 WEBSITE_AUTOCONFIGURE_DATABASE 的值 true。 也支援使用服務連接器建立的 JDBC 連線。 App Service 會根據應用程式設定名稱與後綴 _DS,自動新增對應的共享資料來源,並使用容器中可用的適當驅動程式。 關於使用此方法的端對端情境,請參見 教學:在 Linux 和 MySQL 上使用 Azure App Service 建構 JBoss 網頁應用程式

註冊 資料來源到 JBoss EAP 有三個主要步驟:

  1. 上傳 JDBC 驅動程式。
  2. 將 JDBC 驅動程式新增為模組。
  3. 使用模組新增資料來源。

App Service 是無狀態的主機服務,所以你需要把這些步驟放進啟動腳本,每次 JBoss 容器啟動時執行。 以下是 PostgreSQL、MySQL 和 Azure SQL 資料庫的範例:

備註

JBoss EAP 在 App Service 上支援 Bring Your Own License (BYOL) 計費。 BYOL 允許擁有現有 Red Hat 訂閱的客戶,直接將這些授權套用到 Azure App Service 上的 JBoss EAP 部署。 欲了解更多資訊,請參閱 JBoss EAP 的 BYOL 支援

  1. 將您的 JBoss CLI 命令放入名為 jboss-cli-commands.cli 的檔案中。 JBoss 命令必須新增模組,並將其註冊為資料來源。 下列範例顯示 JBoss CLI 命令,用來建立具有 JNDI 名稱 java:jboss/datasources/postgresDS 的 PostgreSQL 資料來源。

    module add --name=org.postgresql --resources=/home/site/libs/postgresql-42.7.4.jar
    /subsystem=datasources/jdbc-driver=postgresql:add(driver-name="postgresql",driver-module-name="org.postgresql",driver-class-name="org.postgresql.Driver",driver-xa-datasource-class-name="org.postgresql.xa.PGXADataSource")
    data-source add --name=postgresql --driver-name="postgresql" --jndi-name="java:jboss/datasources/postgresDS" --connection-url="jdbc:postgresql://\${env.DB_HOST}:5432/postgres" --user-name="\${env.DB_USERNAME}" --password="\${env.DB_PASSWORD}" --enabled=true --use-java-context=true
    

    請注意,module add 命令使用三個環境變數 (DB_HOSTDB_USERNAMEDB_PASSWORD),您必須在 App Service 中新增為應用程式設定。 指令碼會新增它們,而不加上 --resolve-parameter-values 旗標,讓 JBoss 不會以純文字儲存其值。

  2. 建立會呼叫 JBoss CLI 命令的啟動腳本 startup.sh。 以下範例說明如何呼叫你的 jboss-cli-commands.cli 檔案。 之後,你要設定 App Service 在容器啟動時執行這個腳本。

    $JBOSS_HOME/bin/jboss-cli.sh --connect --file=/home/site/scripts/jboss-cli-commands.cli
    
  3. 使用您選擇的部署選項,將 JDBC 驅動程式 、jboss-cli-commands.clistartup.sh 上傳至個別腳本中指定的路徑。 將 startup.sh 上傳為啟動檔案。 例如:

    export RESOURCE_GROUP_NAME=<resource-group-name>
    export APP_NAME=<app-name>
    
    # The lib type uploads to /home/site/libs by default.
    az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path postgresql-42.7.4.jar --target-path postgresql-42.7.4.jar --type lib
    az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path jboss-cli-commands.cli --target-path /home/site/scripts/jboss-cli-commands.cli --type static
    # The startup type uploads to /home/site/scripts/startup.sh by default.
    az webapp deploy --resource-group $RESOURCE_GROUP_NAME --name $APP_NAME --src-path startup.sh --type startup
    

    如需詳細資訊,請參閱 將檔案部署至 App Service

要確認資料來源是否已加入 JBoss 伺服器,請用 SSH 進入你的網頁應用程式並執行 $JBOSS_HOME/bin/jboss-cli.sh --connect。 連接 JBoss 後,執行 /subsystem=datasources:read-resource 顯示資料來源清單。

根據 jboss-cli-commands.cli 的定義,你可以使用 JNDI 名稱 java:jboss/datasources/postgresDS存取 PostgreSQL 連線。