Apache Ignite .NET 项目模板完全指南:基于 `dotnet new` 快速创建 Ignite 示例工程

原创2026-10-14 13:43:481,671 阅读
文章标签:分布式数据库缓存

Apache Ignite .NET 项目模板完全指南:基于 dotnet new 快速创建 Ignite 示例工程

Apache Ignite 的 .NET 平台为开发者提供了一套基于 .NET CLI 模板(dotnet new)的项目脚手架,用于快速生成 Ignite 示例解决方案与最小可运行工程。本文以仓库中的 templates 目录说明 为核心骨架,结合 examples 示例工程、模板源码与构建脚本,系统讲解 Public/Internal 两类模板的设计目的、安装命令、dotnet new 用法以及模板生成的工程结构,帮助你在本机快速创建 Apache Ignite.NET 的 Thick 与 Thin Client 示例项目。

模板体系总览:Public 与 Internal 两级划分

Apache Ignite .NET 的模板体系分为 Public(对外发布) 与 Internal(内部使用) 两个层次,二者定位完全不同:

  • Public 模板:以 NuGet 包形式发布给最终用户,用户通过一条 dotnet new 命令即可获得整个示例解决方案;
  • Internal 模板:仅用于 Ignite 仓库内部开发与验证,不发布到 NuGet,供维护者在本地生成单个示例工程。

两者的源码均位于仓库的 modules/platforms/dotnet/templates/ 目录下,目录结构如下:

modules/platforms/dotnet/templates/
├── README.md                          # 模板使用说明(本文所依据的文档)
├── public/
│   └── Apache.Ignite.Examples/        # 对外发布的示例模板工程
│       └── Apache.Ignite.Examples.csproj
└── internal/
    ├── Apache.Ignite.Example/         # Server 与 Thick Client 示例模板
    │   ├── ExampleProject.csproj
    │   └── Program.cs
    └── Apache.Ignite.ExampleThin/     # Thin Client 示例模板
        ├── ExampleProjectThin.csproj
        └── Program.cs

从 templates/README.md 可知,Public 模板是整个 Apache.Ignite.Examples.sln 解决方案的载体,用于把仓库中全部示例以最便捷的方式交付给最终用户;Internal 模板则面向两类典型的 Ignite.NET 开发形态——Server/Thick 模式与 Thin Client 模式。

Public 模板:Apache.Ignite.Examples

模板用途与设计理念

Apache.Ignite.Examples 是唯一对外发布的模板,其设计目标非常明确:把整套示例解决方案原样打包进 NuGet 包,让用户通过 dotnet new 一键获得。这正是 templates/README.md 中所描述的"an easy way to deliver examples to the end user"。

它的打包方式很有特点:模板工程本身并不包含任何编译代码,而是把整个示例解决方案作为 content 打进包内。查看 Apache.Ignite.Examples.csproj 可以看到关键配置:

<PropertyGroup>
    <PackageType>Template</PackageType>
    <PackageId>Apache.Ignite.Examples</PackageId>
    <Title>Apache Ignite Examples</Title>
    <Description>Apache Ignite.NET example projects. This package is a convenient way to download and explore the examples.</Description>
    <PackageTags>apache;ignite;examples;distributed;cache;sql;nosql;linq;dotnet-new;templates;</PackageTags>
    <IncludeContentInPack>true</IncludeContentInPack>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <ContentTargetFolders>content</ContentTargetFolders>
</PropertyGroup>

<ItemGroup>
    <Content Include="examples\**\*.*"
             Exclude="examples\**\bin\**;examples\**\obj\**;examples\.idea" />
    <Compile Remove="**\*" />
</ItemGroup>

几个关键点值得注意:

  • PackageType=Template 声明这是一个 dotnet new 模板包;
  • IncludeBuildOutput=false 表示包内不携带编译产物(DLL),只携带内容;
  • ContentTargetFolders=content 配合 <Content Include="examples\**\*.*"> 将整个示例解决方案目录打进包,同时通过 Exclude 剔除 bin/、obj/ 编译产物和 .idea(JetBrains IDE 目录),保证包体积干净;
  • <Compile Remove="**\*" /> 明确告知 SDK 无需编译任何源文件——这个工程只是"搬运工"。

构建与发布流程

模板包的构建被集成进 Ignite .NET 的发布脚本 modules/platforms/dotnet/build.ps1。在 NuGet 打包阶段,脚本会临时把 Apache.Ignite.Examples.csproj 复制到当前工作目录,再执行 dotnet pack(脚本注释解释了原因:dotnet-new 模板无法与父目录内容一起打包):

# Examples template
# Copy csproj to current dir temporarily: dotnet-new templates can't be packed with parent dir content.
Copy-Item .\templates\public\Apache.Ignite.Examples\Apache.Ignite.Examples.csproj $pwd

Exec "dotnet pack Apache.Ignite.Examples.csproj --output $nupkgDir -p:PackageVersion=$ver"

Remove-Item Apache.Ignite.Examples.csproj

从 build.ps1 的上下文可以看到,该脚本在打包 Apache.Ignite.sln 的全部 NuGet 包之后执行此步骤,版本号取自编译产物的 ProductVersion,并支持通过 -version 参数覆盖。打包完成后,nupkg/Apache.Ignite.Examples.<版本号>.nupkg 即可在发布时上传到 NuGet。构建脚本随后还会执行 dotnet build .\examples\Apache.Ignite.Examples.sln(见 build.ps1)验证示例可正常编译。

安装与使用

Public 模板的完整使用流程只有两步:

# 1. 从 NuGet 安装模板(模板 ID 为 Apache.Ignite.Examples)
dotnet new install Apache.Ignite.Examples

# 2. 用模板创建示例解决方案(在当前目录生成 Apache.Ignite.Examples 工程文件)
dotnet new ignite-examples

第二条命令的 ignite-examples 正是模板的短名称(ShortName),也就是 templates/README.md 中 "Use: dotnet new ignite-examples" 所对应的命令。执行后,当前目录下会得到与仓库中 modules/platforms/dotnet/examples/ 一致的结构。

如果希望离线安装本地包,也可以直接指向 .nupkg 文件:

dotnet new install ./Apache.Ignite.Examples.2.11.0.nupkg

生成出的示例解决方案结构

模板包内携带的是完整的示例解决方案,其结构(与仓库 examples 目录 一致)如下:

examples/
├── Apache.Ignite.Examples.sln    # 解决方案文件,可直接用 IDE 打开
├── Directory.Build.props
├── ServerNode/                   # 用于启动 Ignite 服务器节点的工程
├── Shared/                       # 公共代码:配置类、模型类、服务类
├── Thick/                        # Thick 模式示例(Cache、Compute、SQL 等)
│   ├── Cache/                    #   BinaryMode、DataStreamer、EntryProcessor、PutGet ...
│   ├── Compute/
│   ├── DataStructures/
│   ├── Misc/
│   └── Sql/
└── Thin/                         # Thin Client 模式示例
    ├── Cache/                    #   BinaryModeThin、PutGetThin、QueryScanThin ...
    ├── Misc/
    └── Sql/

从 examples/README.md 可以提炼出该解决方案的三大组织原则:

  1. 按 Thick / Thin 模式分组:示例分别存放在 Thick 与 Thin 目录下,二者对应 Ignite.NET 两种截然不同的连接形态;
  2. Shared 工程承载公共代码:包括服务器节点配置、Thin Client 配置以及示例模型类(如 Employee、Organization、Address);
  3. ServerNode 工程独立运行:用于启动 Ignite 服务器节点,Thin 示例运行前必须先启动它。

需要特别说明的是,Thick 与 Thin 是两种不同的架构形态(详见 Thick API 示例说明 与 Thin Client 示例说明):

  • Thick 模式:在当前进程中启动 JVM 并运行完整的 Ignite 节点,是服务端用户代码执行(远程查询过滤器、Service、Compute 等)的前提;
  • Thin Client 模式:轻量级客户端,启动快、内存占用低,通过 TCP 连接远程 Ignite 节点。

示例工程的公共配置:Shared/Utils.cs

无论 Thick 还是 Thin 示例,都依赖 Shared 工程中的 Utils.cs 提供配置。理解这份配置对后续修改模板生成的代码至关重要:

// 服务器节点配置(Thick 示例直接使用)
public static IgniteConfiguration GetServerNodeConfiguration()
{
    return new IgniteConfiguration
    {
        Localhost = "127.0.0.1",
        DiscoverySpi = new TcpDiscoverySpi
        {
            IpFinder = new TcpDiscoveryMulticastIpFinder
            {
                Endpoints = new[] { "127.0.0.1:47500..47502" }
            }
        },
        JvmOptions = new[]
        {
            "-DIGNITE_QUIET=true",
            "-DIGNITE_PERFORMANCE_SUGGESTIONS_DISABLED=true",
            "-DIGNITE_UPDATE_NOTIFIER=false"
        },
        Logger = new ConsoleLogger { MinLevel = LogLevel.Error },
        PeerAssemblyLoadingMode = PeerAssemblyLoadingMode.CurrentAppDomain
    };
}

// Thin Client 配置
public static IgniteClientConfiguration GetThinClientConfiguration()
{
    return new IgniteClientConfiguration { Endpoints = new[] { "127.0.0.1" } };
}

这段代码(源码注释)明确指出:这些选项都不是示例运行所必需的,设置它们的目的是加快启动速度、减少控制台输出(例如将日志级别限制为 Error、禁用更新通知与性能建议)。Localhost = "127.0.0.1" 与固定端口段 47500..47502 的发现配置,保证了本机多节点/多进程间的快速组网。此外,Utils.cs 还提供了 PopulateCache、GetSampleEmployees(内置 7 条员工示例数据)与 DeployDefaultServices(部署名为 default-map-service 的节点单例服务)等辅助方法。

Internal 模板:Apache.Ignite.Example 与 Apache.Ignite.ExampleThin

Internal 模板仅用于仓库内部,不随发布上传 NuGet。它们的核心价值在于:为 Thick(Server/客户端)与 Thin Client 各提供一个最小可运行骨架,让内部开发者在新建示例时可以对照参考。

Apache.Ignite.Example(Server 与 Thick Client 模板)

安装与使用命令:

# 安装(路径指向 internal 目录下的模板工程)
dotnet new --install internal/Apache.Ignite.Example

# 使用
dotnet new ignite-example

从模板源码 Program.cs 可以看到 Thick 示例骨架的核心形态——在当前进程内直接启动一个 Ignite 节点:

using (IIgnite ignite = Ignition.Start(Utils.GetServerNodeConfiguration()))
{
    Console.WriteLine();
    Console.WriteLine(">>> Example started.");

    // TODO: 在此编写示例逻辑,例如 cache.Put / cache.Get

    Console.WriteLine();
}

Console.WriteLine();
Console.WriteLine(">>> Example finished, press any key to exit ...");
Console.ReadKey();

对应工程文件 ExampleProject.csproj 的关键配置:

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>Apache.Ignite.Examples.Thick.ExampleProject</RootNamespace>
</PropertyGroup>

<ItemGroup>
    <ProjectReference Include="..\..\..\Shared\Shared.csproj" />
</ItemGroup>

模板通过 ProjectReference 引用 Shared 工程(与正式示例保持一致),并且默认 using 了 Apache.Ignite.Core.Binary、Apache.Ignite.Core.Cache 等命名空间,Main 中预留了 TODO 位置,开发者只需补充缓存操作等业务代码即可。

Apache.Ignite.ExampleThin(Thin Client 模板)

安装与使用命令:

# 安装
dotnet new --install internal/Apache.Ignite.ExampleThin

# 使用
dotnet new ignite-example-thin

Thin 模板骨架与 Thick 的最大区别在于启动方式——它不再启动本地节点,而是以客户端身份连接远程服务器节点(见 Program.cs):

using (IIgniteClient ignite = Ignition.StartClient(Utils.GetThinClientConfiguration()))
{
    Console.WriteLine();
    Console.WriteLine(">>> Example started.");

    // TODO

    Console.WriteLine();
}

模板源码的 XML 注释还给出了启动服务器节点的三种方式,这是运行 Thin 示例的硬性前提:

  • docker run -p 10800:10800 apacheignite/ignite(通过 Docker 镜像启动,10800 是 Thin Client 默认端口);
  • dotnet run -p ServerNode.csproj(运行示例解决方案中的 ServerNode 工程);
  • ignite.sh / ignite.bat(使用 Ignite 发行版自带启动脚本)。

对应工程文件 ExampleProjectThin.csproj 与 Thick 版本结构相同,差异仅在 RootNamespace 为 Apache.Ignite.Examples.Thin.ExampleProjectThin,并额外引入了 Apache.Ignite.Core.Client、Apache.Ignite.Core.Client.Cache 两个客户端命名空间。两者目标框架均为 net6.0。

从模板到实战:运行一个完整的 Thin 示例

以仓库中的 PutGetThin 示例(examples/Thin/Cache/PutGetThin/)为参照,演示模板生成工程后的完整运行闭环:

第一步:启动服务器节点(Thin 示例的前提,需在独立终端运行):

cd ServerNode
dotnet run

运行的是 ServerNode/Program.cs,它会启动一个采用 Utils.GetServerNodeConfiguration() 配置的节点,并部署 default-map-service 服务:

using (var ignite = Ignition.Start(Utils.GetServerNodeConfiguration()))
{
    Utils.DeployDefaultServices(ignite);
    Console.WriteLine(">>> Server node started, press any key to exit ...");
    Console.ReadKey();
}

第二步:运行 Thin 示例:

cd Thin/Cache/PutGetThin
dotnet run

第三步(可选):运行 Thick 示例无需单独启动节点(它会自带节点),直接执行即可:

cd Thick/Cache/PutGet
dotnet run

以 Thick/Cache/PutGet 示例 为例,它演示了四种缓存读写模式:单个 Put/Get(强类型反序列化)、WithKeepBinary 的二进制对象读取(不反序列化直接按字段取值)、批量 PutAll/GetAll 以及批量二进制模式:

using (IIgnite ignite = Ignition.Start(Utils.GetServerNodeConfiguration()))
{
    var cache = ignite.GetOrCreateCache<object, object>("dotnet_cache_put_get");
    cache.Clear();

    // 单个写入/读取
    var org = new Organization("Microsoft", new Address("1096 Eddy Street, San Francisco, CA", 94109),
        OrganizationType.Private, DateTime.Now);
    cache.Put(1, org);
    Organization orgFromCache = cache.Get(1);

    // 二进制模式:不反序列化,直接读取字段
    var binaryCache = cache.WithKeepBinary<int, IBinaryObject>();
    var binaryOrg = binaryCache.Get(1);
    string name = binaryOrg.GetField<string>("name");
}

该示例同时给出了 Ignite.NET 类型化缓存(ICache<int, Organization>)与二进制缓存(IBinaryObject)两种访问视角,以及 WithKeepBinary 投影的典型用法。

第四步(IDE 环境):也可以直接用 Visual Studio / Rider 打开 Apache.Ignite.Examples.sln,在运行工具栏选择对应示例启动;VS Code 用户则打开目录后进入 "Run" 面板(Ctrl+Shift+D)从下拉框选择示例运行(详见 examples/README.md)。

运行环境与前置条件

无论使用模板生成的工程还是直接使用仓库示例,都需要满足以下环境要求(来自 examples/README.md):

依赖 版本要求 说明
.NET SDK .NET 6 所有示例工程目标框架均为 net6.0
JDK JDK 17 Thick 模式与 Server 节点需要 JVM 支持
操作系统 Windows / Linux / macOS 三大平台均可运行

模板工程与示例工程统一以 net6.0 为目标框架,Thick/Server 场景依赖 JDK 17 提供 JVM 运行时(Ignite .NET 通过 JNI 与 Java 内核交互)。若本机尚未安装对应版本的 JDK,Ignition.Start 会因无法定位 JVM 而失败。

总结:如何选择模板

模板 类型 安装命令 使用命令 适用场景
Apache.Ignite.Examples Public dotnet new install Apache.Ignite.Examples dotnet new ignite-examples 最终用户获取完整示例解决方案
Apache.Ignite.Example Internal dotnet new --install internal/Apache.Ignite.Example dotnet new ignite-example 内部开发,Server / Thick Client 骨架
Apache.Ignite.ExampleThin Internal dotnet new --install internal/Apache.Ignite.ExampleThin dotnet new ignite-example-thin 内部开发,Thin Client 骨架

对普通用户而言,dotnet new ignite-examples 是首选——一条命令即可获得 Apache.Ignite.Examples.sln 完整解决方案,涵盖 Cache、Compute、SQL、数据流、事务、持续查询等各类示例;对希望从零搭建自定义示例的开发者,则可以在理解 Internal 模板骨架(Ignition.Start 与 Ignition.StartClient 两种启动形态)的基础上,参考 Shared/Utils.cs 的配置范式快速起步。

登录后查看全文
ignite