一、前言 至今為止編程開發已經11個年頭,從 VB6.0,ASP時代到ASP.NET再到MVC, 從中見證了.NET技術發展,從無畏無知的懵懂少年,到現在的中年大叔,從中的酸甜苦辣也只有本人自知。隨著歲月的成長,技 ...
一、前言
至今為止編程開發已經11個年頭,從 VB6.0,ASP時代到ASP.NET再到MVC, 從中見證了.NET技術發展,從無畏無知的懵懂少年,到現在的中年大叔,從中的酸甜苦辣也只有本人自知。隨著歲月的成長,技術也從原來的三層設計到現在的領域驅動設計,從原來的關係型資料庫SQL 2000到現在的NOSQL (mongodb,couchbase,redis),從原來基於SOAP協議的web service到現在基於restful 協議的web api,wcf,再到現在rpc微服務。技術的成長也帶來歲月的痕跡。
現在微軟又出了.NET CORE,為了緊跟微軟的步伐,研究了將近1年,從中看了不少開源代碼,如NetEscapades.Configuration,eShopOnContainers,rabbit.RPC等等,從中學到了不少知識,後面利用所學加上自己的想法,開發出分散式微服務框架surging。開源地址:點擊打開鏈接。下麵會以三篇文章的形式介紹
surging
1.基於.NET CORE微服務框架 -surging的介紹和簡單示例 (開源)
2.剝析surging的架構思想
3.後續surging的架構完善工作
二、什麼是surging
surging從中文譯義來說,衝擊,洶涌,也可以翻譯成風起雲涌。我所希望的是.net core 能成為i最流行的技術。
surging從技術層面來說就是基於RPC協議的分散式微服務技術框架,框架依賴於Netty 進行非同步通信,採用Zookeeper作為服務註冊中心,集成了哈希,隨機和輪詢作為負載均衡演算法
1.服務化應用基本框架
框架的執行過程如下:
1.服務提供者啟動,根據RPC協議通過配置的IP和port綁定到netty上
2.註冊服務信息存儲至Zookeeper
3.客戶端CreateProxy調用服務時,從記憶體中拿到上次通知的所有效的服務地址,根據路由信息和負載均衡機制選擇最終調用的服務地址,發起調用
2.簡單示例
創建IModuleServices
IUserService.cs:
+ View Code?12345678910111213141516171819 | [ServiceBundle] //服務標記 public interface IUserService { Task< string > GetUserName( int id); Task< bool > Exists( int id); Task< int > GetUserId( string userName); Task<DateTime> GetUserLastSignInTime( int id); Task<UserModel> GetUser( int id); Task< bool > Update( int id, UserModel model); Task<IDictionary< string , string >> GetDictionary(); Task TryThrowException(); } |
創建領域對象
UserModel:
?123456789 | [ProtoContract] public class UserModel { [ProtoMember(1)] public string Name { get ; set ; } [ProtoMember(2)] public int Age { get ; set ; } } |
AssemblyInfo.cs,擴展AssemblyModuleType來標識模塊,根據AssemblyModuleType進行相關規則的反射註冊
?123456 | [assembly: AssemblyTitle( "Surging.IModuleServices.Common" )] [assembly: AssemblyDescription( "業務模塊介面" )] [assembly: AssemblyModuleType(ModuleType.InterFaceService)] // 如果此項目向 COM 公開,則下列 GUID 用於類型庫的 ID [assembly: Guid( "2103624d-2bc2-4164-9aa5-1408daed9dee" )] |
創建Domain Service
PersonService.cs
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | [ModuleName( "Person" )] //標識實例化名稱 public class PersonService : ServiceBase,IUserService { #region Implementation of IUserService private readonly UserRepository _repository; public PersonService(UserRepository repository) { this ._repository = repository; } public Task< string > GetUserName( int id) { return GetService<IUserService>( "User" ).GetUserName(id); } public Task< bool > Exists( int id) { return Task.FromResult( true ); } public Task< int > GetUserId( string userName) { return Task.FromResult(1); } public Task<DateTime> GetUserLastSignInTime( int id) { return Task.FromResult(DateTime.Now); } public Task<UserModel> GetUser( int id) { return Task.FromResult( new UserModel { Name = "fanly" , Age = 18 }); } public Task< bool > Update( int id, UserModel model) { return Task.FromResult( true ); } public Task<IDictionary< string , string >> GetDictionary() { return Task.FromResult<IDictionary< string , string >>( new Dictionary< string , string > { { "key" , "value" } }); } public async Task Try() { Console.WriteLine( "start" ); await Task.Delay(5000); Console.WriteLine( "end" ); } public Task TryThrowException() { throw new Exception( "用戶Id非法!" ); } #endregion Implementation of IUserService } } |
UserService.cs
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | [ModuleName( "User" )] //標識實例化名稱 public class UserService: IUserService { #region Implementation of IUserService private readonly UserRepository _repository; public UserService(UserRepository repository) { this ._repository = repository; } public Task< string > GetUserName( int id) { return Task.FromResult($ "id:{id} is name fanly." ); } public Task< bool > Exists( int id) { return Task.FromResult( true ); } public Task< int > GetUserId( string userName) { return Task.FromResult(1); } public Task<DateTime> GetUserLastSignInTime( int id) { return Task.FromResult(DateTime.Now); } public Task<UserModel> GetUser( int id) { return Task.FromResult( new UserModel { Name = "fanly" , Age = 18 }); } public Task< bool > Update( int id, UserModel model) { return Task.FromResult( true ); } public Task<IDictionary< string , string >> GetDictionary() { return Task.FromResult<IDictionary< string , string >>( new Dictionary< string , string > { { "key" , "value" } }); } public async Task Try() { Console.WriteLine( "start" ); await Task.Delay(5000); Console.WriteLine( "end" ); } public Task TryThrowException() { throw new Exception( "用戶Id非法!" ); } #endregion Implementation of IUserService } } |
AssemblyInfo.cs,擴展AssemblyModuleType來標識模塊,根據AssemblyModuleType進行相關規則的反射註冊
?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | [ModuleName( "User" )] //標識實例化名稱 public class UserService: IUserService { #region Implementation of IUserService private readonly UserRepository _repository; public UserService(UserRepository repository) { this ._repository = repository; } public Task< string > GetUserName( int id) { return Task.FromResult($ "id:{id} is name fanly." ); } public Task< bool > Exists( int id) { return Task.FromResult( true ); } public Task< int > GetUserId( string userName) { return Task.FromResult(1); } public Task<DateTime> GetUserLastSignInTime( int id) { return Task.FromResult(DateTime.Now); } public Task<UserModel> GetUser( int id) { return Task.FromResult( new UserModel { Name = "fanly" , Age = 18 }); } public Task< bool > Update( int id, UserModel model) { return Task.FromResult( true ); } public Task<IDictionary< string , string >> GetDictionary() { return Task.FromResult<IDictionary< string , string >>( new Dictionary< string , string > { { "key" , "value" } }); } public async Task Try() { Console.WriteLine( "start" ); await Task.Delay(5000); Console.WriteLine( "end" ); } public Task TryThrowException() { throw new Exception( "用戶Id非法!" ); } #endregion Implementation of IUserService } } |