首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET 分散式多模組系統:OSGi 原型實作 - 遠端服務存取與跨 AppDomain 通訊

     編輯:關於.NET

前言

模組化架構中,跨進程、跨機器的服務互調是核心挑戰。本文展示基於 .NET Remoting 的 OSGi 原型,實現多模組分散式部署、服務註冊發現、跨 AppDomain 透明呼叫。

架構拓撲

3 台 Server 分散式模組拓撲

  • 3 台 Server,各承載不同模組
  • 模組間透過介面契約相依、互調
  • 統一 OSGi 執行階段管理生命週期

核心通訊原理:.NET Remoting

  1. 請求介面 (取得 Service Instance) 時,OSGi 透過 Remoting 連接至提供服務的 URL (如 tcp://localhost:8888/OSGI)
  2. 連接成功發送 OpenService 命令,讓目標 OSGi 註冊 TCP Channel
  3. 依介面型別、DLL 路徑、AppDomain 名稱,在特定 AppDomain 中解析介面
  4. 透過 AppDomain.DoCallBack 連接至目標 OSGi 新開的服務端點

設定檔:Manifests.xml

純轉發節點 (無本地模組)

<?xml version="1.0" encoding="utf-8"?>
<Manifests>
    <ServicePort>1000</ServicePort>
    <RemoteServiceMap>
        <Service TargetIP="127.0.0.1" TargetPort="2000">
            OrderModule.PublicInterfaces.IOrderProcessor
        </Service>
    </RemoteServiceMap>
</Manifests>

混合節點 (本地模組 + 遠端相依)

<?xml version="1.0" encoding="utf-8"?>
<Manifests>
    <ServicePort>2000</ServicePort>
    <Manifest>D:\Projects\OSGIDemo\OrderModule\bin\Debug\Manifest.xml</Manifest>
    <RemoteServiceMap>
        <Service TargetIP="127.0.0.1" TargetPort="3000">
            LoggingModule.PublicInterfaces.ILog
        </Service>
    </RemoteServiceMap>
</Manifests>

模組定義:Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<Module>
    <Name>Order Module</Name>
    <Version>1</Version>
    <Assembly>OrderModule</Assembly>
    <Activator>OrderModule.Activator</Activator>
    <RequiredService>LoggingModule.PublicInterfaces.ILog</RequiredService>
    <ProvidedService>OrderModule.PublicInterfaces.IOrderProcessor</ProvidedService>
</Module>

Activator 生命週期管理

using OSGIFramework;

public class Activator : BundleActivator
{
    public override void Startup(BundleContext bc)
    {
        Console.WriteLine("OrderModule Startup");
        BundleContext.Current.RegisterProvidedService();
    }

    public override void Stop(BundleContext bc)
    {
        Console.WriteLine("OrderModule Stop");
        BundleContext.Current.UnRegisterProvidedService();
    }
}

服務實作:跨 AppDomain 透明呼叫


public class OrderProcessorImpl : ServiceProvider, IOrderProcessor
{
    public Guid PlaceOrder(OrderDto order)
    {
        BundleContext ctx = BundleContext.Current;
        ILog log = ctx.GetProvidedService(); // 透明取得遠端服務
        log.Log("log something...");
        Console.WriteLine("PlaceOrder body");
        return Guid.NewGuid();
    }
}

關鍵點:繼承 ServiceProvider (必須繼承 MarshalByRefObject) 才能跨 AppDomain 封送。

主程式啟動流程


static void Main(string[] args)
{
    BundleRuntime runtime = new BundleRuntime();
    runtime.Start();
    
    BundleContext ctx = BundleContext.Current;
    IOrderProcessor processor = (IOrderProcessor)ctx.GetProvidedService(typeof(IOrderProcessor).FullName);
    
    OrderDto order = new OrderDto();
    Guid id = processor.PlaceOrder(order);
    Console.WriteLine("id=" + id.ToString());
    
    Console.ReadKey();
    runtime.Stop();
    runtime.Dispose();
}

專案結構

Demo 專案結構 3 Host

執行結果

執行輸出畫面

架構優勢與擴展規劃

  • 隔離性:各模組獨立 AppDomain,互不干擾
  • 動態性:支援 AppDomain 卸載、DLL 版本熱切換
  • 透明化:本地/遠端服務呼叫程式碼零差異
  • 規劃重構:遷移至 WCF 整合交易、安全性、可靠傳訊

程式碼下載:OSGIDemo2.rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved