Orleans解决并发之痛(三):集群
Orleans本身的設計是一個分布式的框架,多個Silo構成集群,Grains分布在多個Silo中。一旦一個Silo掛了,原來歸屬這個Silo的Grains會自動在其他Silo中激活。生產環境下還是需要以集群方式來部署。
cluster
在Orleans解決并發之痛(二):Grain狀態文章中提到內存存儲State是不靠譜的,同樣,以內存方式存儲集群中Silo的成員關系也是不靠譜的,所以本文使用SQL Server來做Silo的成員關系存儲,以內存方式存儲成員關系存在主節點之說,其他節點的啟動必須依賴主節點的啟動狀態,但以Azure Table、SQL Server 、ZooKeeper、Consul 等存儲成員關系,所有的Silo都是平等的,不需要等待誰。
之前在Orleans解決并發之痛(一):單線程Demo中是以內存存儲集群成員關系的,有興趣可以返回查看。
這篇文章的Demo是Orleans解決并發之痛(二):Grain狀態的基礎上完成的,所以在原來代碼的基礎上做一些調整即可。我們會啟動3個Silo,構建成一個集群環境。實際上提供3個配置文件即可,配置文件稍做修改就可實現。
Silo配置文件
OrleansConfiguration1.xml:
<?xml version="1.0" encoding="utf-8" ?> <OrleansConfiguration xmlns="urn:orleans"><Globals><SystemStore SystemStoreType="SqlServer" DeploymentId="OrleansTest" DataConnectionString="Server=.;Database=OrleansStorage;User ID=sa;Password=123456;"/><StorageProviders><Provider Type="Orleans.Storage.AdoNetStorageProvider"Name="OrleansStorage"AdoInvariant="System.Data.SqlClient"DataConnectionString="Server=.;Database=OrleansStorage;User ID=sa;Password=123456;"/></StorageProviders></Globals><Defaults><Networking Address="localhost" Port="11111" /><ProxyingGateway Address="localhost" Port="30000" /></Defaults> </OrleansConfiguration>OrleansConfiguration2.xml和OrleansConfiguration3.xml除了Networking 、ProxyingGateway 配置有所區別,其他完全一樣。
OrleansConfiguration2.xml:
<Networking Address="localhost" Port="11112" /> <ProxyingGateway Address="localhost" Port="30001" />OrleansConfiguration3.xml:
<Networking Address="localhost" Port="11113" /> <ProxyingGateway Address="localhost" Port="30002" />這次配置文件中引入了一個SystemStore節點:
SystemStoreType:存儲的類型;如:AzureTable、SqlServer、ZooKeeper等;
DeploymentId:部署的唯一Id標識,具有相同的DeploymentId的Silo會加入一個集群中;
DataConnectionString:連接字符串;
3臺Silo啟動成功后,在OrleansStorage庫的OrleansMembershipTable表中會記錄下成員關系:
systemStore
Client配置文件
ClientConfiguration.xml:
<?xml version="1.0" encoding="utf-8" ?> <ClientConfiguration xmlns="urn:orleans"><SystemStore SystemStoreType ="SqlServer"DeploymentId="OrleansTest"DataConnectionString="Data Source=.;Database=OrleansStorage;User ID=sa;Password=123456;" AdoInvariant="System.Data.SqlClient" /> </ClientConfiguration>Client通過DeploymentId標識連接Silo集群。具體最終調用那個Silo完成方法的調用,由其內部調配。當某一臺Silo掛了,Grain會重新在另一個Silo上激活,達到高可用狀態。
Client的測試代碼:
我們用一個死循環,創建很多Grain,來觀察Silo控制臺的輸出效果
var random = new Random(); while (true) {Thread.Sleep(1000);var grainId = random.Next().ToString();var grain = GrainClient.GrainFactory.GetGrain<IPersonGrain>("Test-" + grainId);grain.SayHelloAsync(); }測試結果:
從控制臺輸出結果來開,每臺Silo上Grain的分配還是比較均勻的
Test Result
當殺掉一個Silo后,服務依然是正常運行,具體Grain是否重新被分配有興趣可以測試一下:
Test Result2
參考鏈接:
Actor模型
Orleans
案例Demo-OrleansCluster
關文章:?
.NET的Actor模型:Orleans
微軟分布式云計算框架Orleans(1):Hello World
微軟分布式云計算框架Orleans(2):容災與集群(1)
Aaron Stannard談Akka.NET 1.1
使用Akka.net開發第一個分布式應用
Orleans入門例子
Orleans例子再進一步
Orleans稍微復雜的例子—互動
Orleans簡單配置
Orleans配置---持久化
Orleans—一些概念
Orleans的集群構建
Oleans集群之Consul再解釋
Orleans解決并發之痛(一):單線程
Orleans解決并發之痛(二):Grain狀態
原文地址:http://www.jianshu.com/p/fdf4b3456039
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的Orleans解决并发之痛(三):集群的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【深圳】掌通宝科技有限公司技术总监(兼架
- 下一篇: 发达国家与发展中国家编程语言技术的分布差