Orleans10的非对称部署(部署更友好的方式)

创建定义所有接口的库(一个),但需分别在不同的库中实现接口;利用Orleans服务自动发现已加载程序集中的所有Gain的特性,在进程启动时,先动态加载Gain的实现程序集,在启动silo服务时,控制当前silo启动哪个Gain。

启动Silo前使用Assembly.LoadFrom方法调用选择的程序集。

cs 复制代码
            // 非对称部署(物理分离方案):Orleans 7.0+ 移除了 GrainClassOptions.ExcludedGrainTypes,
            // 改用"运行时按角色加载 Grain 程序集"实现------只有被 Assembly.LoadFrom 加载进 AppDomain
            // 的 Grain 实现才会被 Silo 发现并托管。HelloGrain 和 WeatherGrain 分别在独立的
            // Grains.Hello.dll / Grains.Weather.dll 中(由 SiloHost.csproj 的 CopyGrainDlls target 复制)。
            LoadGrainAssemblies(role);

            var builder = Host.CreateApplicationBuilder();
            builder.Logging.AddProvider(new RichTextLoggerProvider(_logBox));
            builder.Logging.AddFilter("Orleans", LogLevel.Information);

            // 让每个 Grain 能报告自己运行在哪个 Silo(验证非对称部署)
            builder.Services.AddSingleton<ISiloIdentity>(new SiloIdentity(siloName));

            builder.UseOrleans(siloBuilder =>
            {
                siloBuilder
                    .Configure<ClusterOptions>(options =>
                    {
                        options.ClusterId = ClusterId;
                        options.ServiceId = ServiceId;
                    })
                    .Configure<SiloOptions>(options => options.SiloName = siloName)
                    .ConfigureEndpoints("localhost", siloPort, gatewayPort)
                    .UseRedisClustering(options =>
                    {
                        options.ConfigurationOptions = ConfigurationOptions.Parse(redisConn);
                    });
            });

            _host = builder.Build();
            await _host.StartAsync();
            Log($"Silo '{siloName}' 已加入集群 '{ClusterId}'。");
            _stopBtn.Enabled = true;
cs 复制代码
        try
        {
            var redisConn = _redisBox.Text.Trim();
            Log($"通过 Redis '{redisConn}' 连接集群 '{ClusterId}' ...");

            var builder = Host.CreateApplicationBuilder();
            builder.UseOrleansClient(clientBuilder =>
            {
                clientBuilder
                    .Configure<ClusterOptions>(options =>
                    {
                        options.ClusterId = ClusterId;
                        options.ServiceId = ServiceId;
                    })
                    .UseRedisClustering(options =>
                    {
                        options.ConfigurationOptions = ConfigurationOptions.Parse(redisConn);
                    });
                    // Orleans 7.0+ 已移除 Application Parts,改由 Source Generator 自动发现 Grain 类型。
                    // Client 项目已通过 ProjectReference 引用 Contracts 程序集,无需手动注册。
            });

            _host = builder.Build();
            await _host.StartAsync();
            _client = _host.Services.GetRequiredService<IClusterClient>();
            Log("已连接到集群,可以调用 Grain。");
            _helloBtn.Enabled = true;
            _weatherBtn.Enabled = true;
        }
        catch (Exception ex)
        {
            Log($"连接失败: {ex}");
            _connectBtn.Enabled = true;
            if (_host is not null)
            {
                try { _host.Dispose(); } catch { }
                _host = null;
            }
        }



    private async void OnCallHello(object? sender, EventArgs e)
    {
        if (_client is null) return;
        var name = _nameBox.Text.Trim();
        if (string.IsNullOrEmpty(name)) { Log("请输入名字。"); return; }
        _helloBtn.Enabled = false;
        try
        {
            var grain = _client.GetGrain<IHelloGrain>(name);
            var result = await grain.SayHello(name);
            Log($"HelloGrain -> {result}");
        }
        catch (Exception ex)
        {
            Log($"调用 HelloGrain 失败: {ex.Message}");
        }
        finally
        {
            _helloBtn.Enabled = true;
        }
    }

    private async void OnCallWeather(object? sender, EventArgs e)
    {
        if (_client is null) return;
        var city = _cityBox.Text.Trim();
        if (string.IsNullOrEmpty(city)) { Log("请输入城市。"); return; }
        _weatherBtn.Enabled = false;
        try
        {
            var grain = _client.GetGrain<IWeatherGrain>(city);
            var result = await grain.GetForecast(city);
            Log($"WeatherGrain -> {result}");
        }
        catch (Exception ex)
        {
            Log($"调用 WeatherGrain 失败: {ex.Message}");
        }
        finally
        {
            _weatherBtn.Enabled = true;
        }
    }

如对应Gain没有启动服务,客户端调用没有明显卡顿,直接抛异常。

相关推荐
BruceNeter21 小时前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net
何以解忧唯有撸码2 天前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
国家一级摸鱼选手3 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
汤姆宝库3 天前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
Risehuxyc4 天前
C# 发展历史
开发语言·c#
kyle~4 天前
C# --- 反射
开发语言·c#
暴走约伯5 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316346 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船6 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261357 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#