主要内容

本页采用了机器翻译。点击此处可查看英文原文。

使用生产服务器存档编译器创建 MATLAB Production Server 存档

支持的平台:Windows®、Linux®Mac

此示例展示了如何使用 Production Server 存档编译器将 MATLAB® 函数打包到 MATLAB Production Server™ 存档中。您可以将存档部署到 MATLAB Production Server

在 R2025a 之前的版本中: 请参阅为 MATLAB Production Server 创建可部署存档 (R2024b)

创建 MATLAB 函数

编写一个您想要部署的 MATLAB 程序。有关编写 MATLAB 代码用于 MATLAB Production Server 的信息,请参阅MATLAB 编码准则

对于此示例,创建一个名为 addmatrix.m 的函数。

function a = addmatrix(a1, a2)
a = a1 + a2;

在 MATLAB 命令提示符下,输入 addmatrix([1 4 7; 2 5 8; 3 6 9], [1 4 7; 2 5 8; 3 6 9])

输出是一个 3×3 的矩阵。

     2     8    14
     4    10    16
     6    12    18

创建工程和编译器任务

使用 Production Server 存档编译器创建函数的编译任务。编译器任务允许您针对特定部署目标编译工程中的文件。

要打开该 App,请在 App 选项卡上展开 App 库。在应用程序部署部分,点击 Production Server 存档编译器

Application Deployment section of the Apps gallery

您还可以在 MATLAB 命令行窗口中使用 productionServerArchiveCompiler 函数打开该 App。

打开该 App 后,“创建编译器任务”对话框会提示您向新的或现有 MATLAB 工程中添加编译器任务。对于此示例,请选择启动新工程并创建编译器任务,并在您的工作文件夹中创建一个名为 MatrixProject 的新工程。有关创建和使用 MATLAB 工程的详细信息,请参阅创建工程

Create compiler task dialog box with the text 'To deploy your MATLAB code, you need a MATLAB project to organize code and a compiler task to handle deployment.' The option 'Start a new project and create a compiler task' is selected.

编辑器中打开了一个名为 ProductionServerArchive1 的新编译任务。您可以通过打开编译器任务管理器或转到管理任务选项卡并创建一个新的编译器任务来编译其他部署目标的代码。

指定编译选项

在打包之前,您可以为 MATLAB Production Server 存档指定选项,以自定义构建和打包过程。例如,您可以混淆处理 MATLAB 代码或添加函数签名文件。有关函数签名的信息,请参阅JSON 中的 MATLAB 函数签名 (MATLAB Production Server)

在此示例中,在编译器任务的导出的函数部分,点击添加导出的函数,然后选择 addmatrix.m。在“工程”面板中,该文件现在带有标签 DesignExported Function File

Exported file section of the compiler task with no file selected and a button labeled Add Exported Function

包信息部分,将字符串 MyProductionServerArchive 替换为您的 MATLAB Production Server 包的名称,即 AddMatrixArchive

查看代码和包 MATLAB Production Server 存档

要查看包含组件编译和打包说明的代码,请点击导出编译脚本按钮旁边的箭头,然后选择显示代码。在右侧,将打开一个窗口,显示与您的构建选项相对应的包含 compiler.build.productionServerArchive 函数的部署脚本。

您可以通过点击导出编译脚本按钮将此代码转换为 MATLAB 脚本文件。运行生成的构建脚本与点击打包按钮等效。

Three buttons labeled Test Client, Export Build Script, and Package

要创建 MATLAB Production Server 存档,请点击打包

编译器将在您的工程文件夹中的 <compiler_task_name>/output 文件夹中生成文件。要为生成的文件选择不同的输出位置,请更新输出位置部分中的路径。

测试和部署存档

创建可部署存档后,您需要为客户端应用程序编写源代码,该源代码使用 HTTP 或 HTTPS 请求调用存档中的 MATLAB 函数。要使用 Java® 客户端 API 中的 MWHttpClient 类创建此示例的客户端,请参阅使用 MWHttpClient 类创建 MATLABProduction ServerJava 客户端 (MATLAB Production Server)

您可以使用开发版本的 MATLAB Production Server 测试 RESTful API 或 Java 客户端。MATLAB Compiler SDK™ 包括开发版本的 MATLAB Production Server,用于在将应用程序代码和 Excel® 加载项部署到 Web 应用程序和企业系统之前进行测试和调试。要测试您的客户端,请参阅使用 MATLAB Production Server 的开发版本测试和调试 RESTful API 及 Java 客户端

将存档文件部署到 MATLAB 生产服务器的方法有几种:

在生产服务器上的存档编译器应用内部署存档

您可以直接通过该应用将存档文件部署到 MATLAB Production Server 实例上。

前提条件

请在托管 MATLAB Production Server 实例的计算机上完成以下步骤。

配置属性

若要直接从应用程序部署存档文件,请在服务器实例的 main_config 文件中启用并设置以下配置属性。有关配置属性的更多信息,请参阅 Server Configuration 属性 (MATLAB Production Server)

设置证书和密钥路径:

  • 设置私钥路径。

    --x509-private-key <path-to-key-file>

  • 设置证书链路径。

    --x509-cert-chain <path-to-cert-chain-file>

启用 HTTPS:

  • 取消以下行注释,以在 9920 端口上启用 HTTPS。

    --https 9920

指定配置文件路径:

  • 请指定您的访问控制配置文件。

    --access-control-config ./config/jwt_idp.json
  • 指定您的访问控制策略文件。

    --access-control-policy ./config/ac_policy.json

启用访问控制:

  • --access-control-provider OAuth2

启用存档管理:

  • --enable-archive-management

部署存档

打包完 MATLAB Production Server 存档后,您可以直接从应用中进行部署。

要部署您的存档,请点击部署到服务器

Window showing successful creation of a Production Server Archive, with the option to deploy to server

接下来,输入您的 JSON Web Token,然后点击上传

Window prompting you to enter your JSON web token to upload your archive

有关将存档部署到 MATLAB Production Server 的其他方法,请参阅 将存档部署至 MATLAB Production Server (MATLAB Production Server)。如需使用 RESTful API 部署和管理存档,请参阅 用于安全管理可部署存档的 RESTful API (MATLAB Production Server)

另请参阅

|

主题