AppCAT 7 的规则开发指南

本指南适用于工程师、顾问和其他希望为 Azure Migrate 应用程序和代码评估(Java 版本 7 的 AppCAT 工具)创建基于 YAML 的自定义规则的人士。

有关概述,请参阅 适用于 Java 的 Azure Migrate 应用程序和代码评估概述 ,有关详细信息,请参阅 AppCAT 7 的 CLI 命令指南

AppCAT 包含基于规则的迁移工具,用于分析计划迁移的应用程序使用的 API、技术和体系结构。 事实上,AppCAT 分析过程是使用 AppCAT 规则实现的。 AppCAT 在内部使用规则提取文件,从存档文件中反编译文件、扫描并分类文件类型、分析 XML 及其他文件内容、分析应用程序代码,并生成报告。

规则入门

AppCAT 规则集项目提供未来规则来帮助静态代码分析。 该项目还贡献了主题专家分享的问题,以帮助创建更丰富的规则集。

基本规则格式来自上游 konveyor 规则集。 在继续作之前,请务必查看有关规则元数据的文档。

AppCAT 中的更多规则元数据

AppCAT 包含 konveyor 规则集中描述的规则元数据,但提供有关使报表更分层的规则的详细信息,如以下列表所述:

  • AppCAT 为规则添加三个域:
    • Azure 就绪情况:目标是将应用程序资源迁移到 Azure,以便与 Azure 资源无缝协作。
    • 云原生:目标是优化应用程序以遵循云友好原则
    • Java 现代化:目标是通过采用受支持的 Java 版本和新式 API 来优化应用程序,同时避免弃用的功能的安全风险。
  • AppCAT 添加规则类别。 如有必要,可以将不同的规则分组到类别中 - 例如,找到的 MySQL/PostgreSQL 数据库 都属于 数据库迁移

域/类别元数据被写在 `label` 属性下以用于自定义规则。 AppCAT 报表中仅显示具有此类标签的规则,如以下示例所示:

labels:
  - domain=azure-readiness
  - category=database-migration

创建规则

以下规则示例标识在项目中是否找到 MySQL 数据库。 如果找到数据库,则规则建议将其迁移到 Azure Database for MySQL。

- category: potential
  customVariables: []
  description: MySQL database found
  effort: 3
  labels:
  - konveyor.io/target=azure-aks
  - konveyor.io/source
  - domain=azure-readiness
  - category=database-migration
  links:
  - title: Azure Database for MySQL documentation
    url: https://docs.azure.cn/mysql
  message: |-
    To migrate a Java application that uses a MySQL database to Azure, you can follow these recommendations:

     * Use a managed **Azure Database for MySQL**: For that create a managed MySQL database in Azure and choose the appropriate pricing tier based on your application's requirements for performance, storage, and availability.

     * **Migrate** the existing MySQL database: For that you can use the Azure Database Migration Service (DMS) to perform an online migration with minimal downtime.

     * Update the application's **database connection** details: Modify the Java application's configuration to point to the newly provisioned Azure Database for MySQL. Update the connection string, hostname, port, username, and password information accordingly.

     * Enable **monitoring and diagnostics**: Utilize Azure Monitor to gain insights into the performance and health of your Java application and the underlying MySQL database. Set up metrics, alerts, and log analytics to proactively identify and resolve issues.

     * Implement **security** measures: Apply security best practices to protect your Java application and the MySQL database. This includes implementing authentication and authorization mechanisms with passwordless connections and leveraging Microsoft Defender for Cloud for threat detection and vulnerability assessments.

     * **Backup** your data: Azure Database for MySQL provides automated backups by default. You can configure the retention period for backups based on your requirements. You can also enable geo-redundant backups, if needed, to enhance data durability and availability.
  ruleID: azure-database-mysql-01000
  when:
    and:
    - or:
      - java.dependency:
          lowerbound: 0.0.0
          nameregex: ([a-zA-Z0-9._-]*)mysql([a-zA-Z0-9._-]*)
      - builtin.filecontent:
          filePattern: build\.gradle$
          pattern: mysql
      - builtin.file:
          pattern: ^([a-zA-Z0-9._-]*)mysql-connector([a-zA-Z0-9._-]*)\.jar$
      - builtin.filecontent:
          filePattern: (/|\\)([a-zA-Z0-9._-]+)\.(xml|properties|yaml|yml)$
          pattern: (?i)jdbc:mysql
      - builtin.filecontent:
          filePattern: (/|\\)([a-zA-Z0-9._-]+)\.(xml|properties|yaml|yml)$
          pattern: (?i)r2dbc:mysql
      - builtin.filecontent:
          filePattern: (/|\\)([a-zA-Z0-9._-]+)\.(xml|properties|yaml|yml)$
          pattern: (?i)r2dbc:pool:mysql
    - not: true
      or:
      - builtin.filecontent:
          filePattern: (/|\\)(application|bootstrap)(-[a-zA-Z0-9]+)*?\.(properties|yaml|yml)$
          pattern: ([a-zA-Z0-9-]+)\.mysql\.database\.azure\.com$
      - java.dependency:
          lowerbound: 0.0.0
          nameregex: ([a-zA-Z0-9._-]*)spring-cloud-azure-starter-jdbc-mysql([a-zA-Z0-9._-]*)

运行一个规则

在 AppCAT CLI 中运行规则时,可以选择仅运行规则,或者使用 AppCAT 默认规则集一起运行规则,如以下示例所示:

# run appcat rules with default ruleset, it means run your rules with appcat provided rules toger
appcat analyze \
    --input <input> \
    --output <output> \
    --target <target> \
    --rules custom-rule1.yaml,custom-rule2.yaml

# only run your own rules
appcat analyze \
    --input <input> \
    --output <output> \
    --target <target> \
    --rules custom-rule1.yaml,custom-rule2.yaml \
    --enable-default-rulesets=false