你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:使用服务连接器将应用程序部署到 Azure Spring Apps 并将其连接到 Azure Database for MySQL 灵活服务器

在本教程中,你将使用 Azure 门户或 Azure CLI 完成以下任务。 以下过程介绍了这两种方法。

  • 预配 Azure Spring Apps 的实例
  • 生成应用并将其部署到 Azure Spring Apps
  • 使用服务连接器将 Azure Spring Apps 与 Azure Database for MySQL 集成

先决条件

预配 Azure Spring Apps 的实例

以下过程使用 Azure CLI 扩展来预配 Azure Spring Apps 的实例。

  1. 使用 Azure Spring Apps 扩展更新 Azure CLI。

    az extension update --name spring
    
  2. 登录到 Azure CLI 并选择你的有效订阅。

    az login
    az account list -o table
    az account set --subscription <Name or ID of subscription, skip if you only have 1 subscription>
    
  3. 创建可包含应用和 Azure Spring Apps 服务实例的资源组。

    az group create --name ServiceConnector-tutorial-mysqlf --location eastus
    
  4. 创建 Azure Spring Apps 的实例。 其名称必须为 4 到 32 个字符,只能包含小写字母、数字及连字符。 Azure Spring Apps 实例名称的第一个字符必须是字母,最后一个字符必须是字母或数字。

    az spring create -n my-azure-spring -g ServiceConnector-tutorial-mysqlf
    

创建 Azure Database for MySQL 灵活服务器

创建 MySQL 灵活服务器实例。 在以下命令中,将 <admin-username><admin-password> 替换为所选凭据,以便为 MySQL 灵活服务器创建管理员用户。 管理员用户名不能是 azure_superuser、azure_pg_admin、admin、administrator、root、guest 或 public 。 不能以 pg_ 开头。 密码必须包含以下字符类别中的三类(8 到 128 个字符):英文大写字母、英文小写字母、数字及非字母数字字符(例如 !#%)。 该密码不能包含 username

az mysql flexible-server create \
    --resource-group ServiceConnector-tutorial-mysqlf \
    --name mysqlf-server \
    --database-name mysqlf-db \
    --admin-user <admin-username> \
    --admin-password <admin-password>

服务器是使用以下默认值创建的(除非手动替换这些默认值):

设置 默认值 说明
server-name 系统生成的 用于标识 Azure Database for MySQL 服务器的唯一名称。
sku-name GP_Gen5_2 SKU 的名称。 请遵循简写约定 {pricing tier}_{compute generation}_{vCores}。 默认为带 2 个 vCore 的常规用途 Gen5 服务器。 有关定价的详细信息,请转到定价页
backup-retention 7 备份保留时间。 单位为天。
geo-redundant-backup 已禁用 是否应该为此服务启用异地冗余备份。
location westus2 服务器的 Azure 位置。
ssl-enforcement 已启用 是否应该为此服务器启用 SSL。
storage-size 5120 服务器的存储容量(以 MB 为单位)。
版本 5.7 MySQL 主版本。

注意

默认情况下会使用 Standard_B1ms SKU。 有关定价详细信息,请参阅 Azure Database for MySQL 定价

注意

有关 az mysql flexible-server create 命令及其附加参数的详细信息,请参阅 Azure CLI 文档

生成并部署应用

  1. 创建已分配公共终结点的应用。 如果在生成 Azure Spring Apps 项目时选择了 Java 版本 11,请包含 --runtime-version=Java_11 开关。

    az spring app create -n hellospring -s my-azure-spring -g ServiceConnector-tutorial-mysqlf --assign-endpoint true
    
  2. 运行 az spring connection create 命令,将部署到 Azure Spring Apps 的应用程序连接到 MySQL 灵活服务器数据库。 将下面的占位符替换为你自己的信息。

    az spring connection create mysql-flexible \
        --resource-group ServiceConnector-tutorial-mysqlf \
        --service my-azure-spring \
        --app hellospring \
        --target-resource-group ServiceConnector-tutorial-mysqlf \
        --server mysqlf-server \
        --database mysqlf-db \
        --secret name=<admin-username> secret=<admin-secret>
    
    设置 说明
    --resource-group 包含 Azure Spring Apps 托管的应用的资源组的名称。
    --service Azure Spring Apps 资源的名称。
    --app 连接到目标服务的 Azure Spring Apps 托管的应用程序的名称。
    --target-resource-group 包含存储帐户的资源组的名称。
    --server 要连接到的 MySQL 灵活服务器
    --database 之前创建的数据库的名称。
    --secret name MySQL 灵活服务器用户名。
    --secret MySQL 灵活服务器密码。

    注意

    如果看到错误消息“该订阅未注册为使用 ServiceLinker”,请运行 az provider register -n Microsoft.ServiceLinker 注册服务连接器资源提供程序,然后再次运行连接命令。

  3. 克隆示例代码

    git clone https://github.com/Azure-Samples/serviceconnector-springcloud-mysql-springboot.git
    
  4. 使用 Maven 生成项目。

    cd serviceconnector-springcloud-mysql-springboot
    mvn clean package -DskipTests 
    
  5. 为应用 target/demo-0.0.1-SNAPSHOT.jar 部署 JAR 文件。

    az spring app deploy \
        --name hellospring \
        --service my-azure-spring \
        --resource-group ServiceConnector-tutorial-mysqlf \
        --artifact-path target/demo-0.0.1-SNAPSHOT.jar
    
  6. 在部署后通过以下命令查询应用状态。

    az spring app list  --resource-group ServiceConnector-tutorial-mysqlf --service my-azure-spring --output table
    

    应该会看到以下输出:

    Name         Location    ResourceGroup                     Public Url                                                 Production Deployment    Provisioning State    CPU    Memory    Running Instance    Registered Instance    Persistent Storage    Bind Service Registry    Bind Application Configuration Service
    -----------  ----------  --------------------------------  ---------------------------------------------------------  -----------------------  --------------------  -----  --------  ------------------  ---------------------  --------------------  -----------------------  ----------------------------------------
    hellospring  eastus      ServiceConnector-tutorial-mysqlf  https://my-azure-spring-hellospring.azuremicroservices.io  default                  Succeeded             1      1Gi       1/1                 0/1                    -                     -
    
    

后续步骤

参考下面列出的教程来详细了解服务连接器。