本文共 3375 字,大约阅读时间需要 11 分钟。
Dym 是一个支持 .NET Core 2.2 和 .NET Framework 4.6.1 的分布式开发框架。它提供了强大的工具支持,适合构建高性能和可扩展的后台服务。接下来,我们将详细介绍如何运行和配置这个框架。
首先,你需要使用 dotnet 运行 Dym 的示例项目。进入项目文件夹 dymDemo\YY.AppCenter\bin\Debug\netcoreapp2.2,运行以下命令:
dotnet AppCenter.dll
默认情况下,程序会在 6660 端口上监听。如果你看到类似下面的输出,说明程序已经成功运行:
(Assembly loaded: dym.Rpc.Center) Bootstrap v1.0.0.0
在 dym.config 文件中,配置你的服务设置。一个基本的配置文件可能如下:
6660 120000
接下来,进入 dymDemo\YY.Server\bin\Debug\netcoreapp2.2 文件夹,运行命令:
dotnet YY.Server.dll
默认情况下,服务会在 6659 端口上监听。如果你看到以下信息,说明服务已经成功注册到注册中心:
(Assembly loaded: dym.Rpc.Server) Bootstrap v1.0.0.0
创建一个新的 Plug 类,继承自 IPlugsConfigurationBootstrap。在 ConfigurationBootstrap 方法中,添加自定义的业务逻辑配置。
using System;using dym.EngineData;namespace dym.Plugs.YYTestService{ public class YYTestBootstrap : IPlugsConfigurationBootstrap { public void ConfigurationBootstrap() { // 服务启动时会自动调用此方法 // 可以用来加载插件配置 } public void PreConfigurationBootstrap() { // Ioc 注入之前执行 } }} 在同一个命名空间中,添加一个新的模块类。以下是一个简单的示例:
using dym.EngineData;using System;using System.Collections.Generic;namespace dym.Plugs.YYTestService{ public class MyFirstModule : BaseModule { public MyFirstModule() { } public ActionResult MyT() { var xx = RequestString("XX"); Console.WriteLine($"来自客户端的消息:{xx}"); return new ActionResult(true, new { Msg = "I from dym.Plugs.YYTestService MyFirstModule!" }, null, xx); } }} 进入 dymDemo\YY.Client\bin\Debug\netcoreapp2.2 文件夹,运行命令:
dotnet YY.Client.dll
在客户端代码中,配置默认参数如下:
using System;using dym.Rpc.Client;namespace YY.Client{ class Program { static void Main(string[] args) { DefaultConfigManager.SetDefaultConfiguration("YY.Client", "127.0.0.1", 6660, false); while (true) { Console.WriteLine("请输入一个消息然后回车发送到服务器:"); var inputMsg = Console.ReadLine(); var input = new InputTest() { channel = "dym.Plugs.YYTest", router = "MyFirst", method = "MyT", XX = $"{inputMsg}参数1" }; var rltStr = Connector.BrokerDns(input); Console.WriteLine(rltStr); Console.WriteLine("-----------------------------------------------------------------------------"); var inputYYTest = new InputTest() { channel = "dym.Plugs.YYTest", router = "MySecond", method = "MyT", XX = $"{inputMsg}参数2" }; var rltStrYY = Connector.BrokerDns(inputYYTest); Console.WriteLine(rltStrYY); goto Restart; } } } public class InputTest : InputDtoBase { public string XX { get; set; } }} 通过客户端调用服务的方法,测试分布式框架的运行效果。输出结果会显示在控制台中。
通过以上步骤,你已经成功配置并运行了 Dym 分布式开发框架。接下来可以深入探索高级功能,如负载均衡、事件总线优化以及 CQRS 模式的应用。
转载地址:http://ogfuz.baihongyu.com/