日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

[转载]Surging 分布式微服务框架使用入门

發(fā)布時(shí)間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载]Surging 分布式微服务框架使用入门 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

本文非 Surging 官方教程,只是自己學(xué)習(xí)的總結(jié)。如有哪里不對,還望指正。

?我對 surging 的看法

我目前所在的公司采用架構(gòu)就是類似與Surging的RPC框架,在.NET 4.0框架上搭建Socket RPC,通過分組輪詢的方式調(diào)度RPC,經(jīng)歷過3次雙十一的考驗(yàn),實(shí)際最高時(shí)有800多人同時(shí)作業(yè),同時(shí)并發(fā)達(dá)到600人/連接rpc?,24小時(shí)不間斷作業(yè),這是實(shí)際數(shù)據(jù),理論上更高,只需要加RPC就可以了,剩下的就看數(shù)據(jù)庫以及緩存的性能了,說到數(shù)據(jù)庫,這又是另外一個(gè)技術(shù)棧了。雖然這個(gè)數(shù)據(jù)并不能說明RPC有多高效,但確是實(shí)實(shí)在在的現(xiàn)場數(shù)據(jù)。

surging的出現(xiàn)給了我眼前一亮的感覺。surging 是一個(gè)分布式微服務(wù)框架,提供高性能RPC遠(yuǎn)程服務(wù)調(diào)用,采用Zookeeper、Consul作為surging服務(wù)的注冊中心,集成了哈希,隨機(jī),輪詢、壓力最小優(yōu)先作為負(fù)載均衡的算法,RPC集成采用的是netty框架,采用異步傳輸。內(nèi)部RPC,外部網(wǎng)關(guān)。原來這就是微服務(wù)框架,數(shù)據(jù)監(jiān)控、流量控制、分流控制、重試、熔斷........。居然還能這樣做,盡管部分術(shù)語你可能很早很早就聽過了,但卻沒有形成一個(gè)框架,或者使用起來很困難。surging 恰恰就是這樣一個(gè)集大成者的框架,所有這些surging幫你做了,而且非常非常高效。這個(gè)高效不僅僅體現(xiàn)在surging的性能上(surging性能作者有過測試),還體現(xiàn)在開發(fā)上,只要稍有基礎(chǔ)就能輕易駕馭,剩下就是整合業(yè)務(wù)進(jìn)行開發(fā)了。

這是Surging作者的PC電腦全套運(yùn)行測試的數(shù)據(jù),可以說是非常厲害了。相信在部署集群的環(huán)境中,多實(shí)例,性能肯定不存在問題。

Surging 分布式微服務(wù)框架適合做什么

  1. 企業(yè)級互聯(lián)網(wǎng)架構(gòu)平臺(tái);
  2. 傳統(tǒng)大型項(xiàng)目,伸縮性很強(qiáng)的項(xiàng)目,可應(yīng)對突發(fā)的流量暴增(比如雙十一訂單暴增);
  3. 移動(dòng)互聯(lián)網(wǎng)項(xiàng)目,比如為了因?qū)ν话l(fā)的因營銷等帶來的流量暴增,評論暴增等等情況;

歡迎補(bǔ)充...

一、準(zhǔn)備

服務(wù)注冊中心的選擇:目前 Surging 提供了 Zookeeper、Consul 作為服務(wù)注冊中心,后期還可能會(huì)把?service-fabric?加入進(jìn)來,中文官方地址:https://docs.microsoft.com/zh-cn/azure/service-fabric/

Event Bus 的選擇:Surging 同樣提供了多種選擇,有 RabbitMQ,Kafka 等選擇。

Redis

具體使用哪種,就看自己的技術(shù)棧啦

?二、示例開發(fā)

1.在sqlserver中建立Test 數(shù)據(jù)庫

運(yùn)行下面腳本,生成user表

test.db

2.運(yùn)行Surging Demo

clone代碼?git clone https://github.com/billyang/SurgingDemo.git

因?yàn)楸臼纠?xiàng)目沒有從nuget?引用,直接從 surging 項(xiàng)目引用,沒有拷貝一份放在自己的解決方案,

假設(shè)目錄結(jié)構(gòu)如下:

D:\git\surging
D:\git\SurgingDemo

最終保持SurgingSurgingDemo在同一個(gè)目錄

這樣做的好處:

  • 是和 surging 保持最新代碼
  • 是方便學(xué)習(xí)surging和調(diào)試,畢竟你想使用surging、理解surging才是踏出第一步

?

Surging.ApiGateway 提供了服務(wù)管理以及網(wǎng)關(guān)統(tǒng)一訪問入口。 目前開發(fā)還不完善,如果現(xiàn)在要用于正式開發(fā)建議自己要部分重寫 ApiGateway,加入權(quán)限驗(yàn)證。相信等到1.0版本作者也會(huì)把數(shù)據(jù)監(jiān)控、流量控制、數(shù)據(jù)安全、分流控制、身份認(rèn)證等管理功能加入,當(dāng)然這些功能并不會(huì)影響正常使用。

服務(wù)管理使用 consul,因?yàn)檎{(diào)試簡單,只需 consul agent -dev 即可開啟consul

在 windows 中啟動(dòng):
發(fā)布網(wǎng)關(guān) 1. ApiGateway ? ? ? ? ? ? ? ?dotnet run Surging.ApiGateway
啟用服務(wù) 2. Server ? ? ? ? ? ? ?dotnet Bill.Demo.Services.Server.dll
發(fā)布客戶端(本示例使用 web mvc) 3. Bill.Demo.Web ? ? ? ? ? ? ? dotnet run Bill.Demo.Web

假設(shè)你已經(jīng)把SurgingDemo已運(yùn)行起來了,即可對根據(jù)Dapper對User進(jìn)行增刪改查?

?

?

三、介紹一下本示例各項(xiàng)目的職責(zé)

Bill.Demo.Core 用戶定義數(shù)據(jù)模型

Bill.Demo.DapperCore (Dapper倉儲(chǔ),其中倉儲(chǔ)需繼承 UserRepository: Surging.Core.CPlatform.Ioc.BaseRepository)

?

 1 public class UserRepository: BaseRepository, IBaseRepository<User>
 2     {
 3         /// <summary>
 4         /// 創(chuàng)建一個(gè)用戶
 5         /// </summary>
 6         /// <param name="entity">用戶</param>
 7         /// <param name="connectionString">鏈接字符串</param>
 8         /// <returns></returns>
 9         public Task<Boolean> CreateEntity(User entity, String connectionString = null)
10         {
11             using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString))
12             {
13                 string insertSql = @"INSERT  INTO dbo.auth_User
14                                     ( TenantId ,
15                                       Name ,
16                                       Password ,
17                                       SecurityStamp ,
18                                       FullName ,
19                                       Surname ,
20                                       PhoneNumber ,
21                                       IsPhoneNumberConfirmed ,
22                                       EmailAddress ,
23                                       IsEmailConfirmed ,
24                                       EmailConfirmationCode ,
25                                       IsActive ,
26                                       PasswordResetCode ,
27                                       LastLoginTime ,
28                                       IsLockoutEnabled ,
29                                       AccessFailedCount ,
30                                       LockoutEndDateUtc
31                                     )
32                             VALUES  ( @tenantid ,
33                                       @name ,
34                                       @password ,
35                                       @securitystamp ,
36                                       @fullname ,
37                                       @surname ,
38                                       @phonenumber ,
39                                       @isphonenumberconfirmed ,
40                                       @emailaddress ,
41                                       @isemailconfirmed ,
42                                       @emailconfirmationcode ,
43                                       @isactive ,
44                                       @passwordresetcode ,
45                                       @lastlogintime ,
46                                       @islockoutenabled ,
47                                       @accessfailedcount ,
48                                       @lockoutenddateutc
49                                     );";
50                 return Task.FromResult<Boolean>(conn.Execute(insertSql, entity) > 0);
51             }
52         }
53    }

?

Bill.Demo.IModuleServices (和Surging項(xiàng)目一樣,定義模塊服務(wù)接口以及領(lǐng)域模型)

        Task<UserDto> GetUserById(Int64 id);Task<Boolean> UpdateUser(UserDto user);Task<Boolean> DeleteUser(Int64 userId);

Bill.Demo.ModuleServices (和Surging項(xiàng)目一樣,實(shí)現(xiàn)模塊服務(wù))

如:

 1         public async Task<UserDto> GetUserById(Int64 id)2  {  3 var user = await _repository.GetEntityById(id);  4 return new UserDto()  5  {  6 Id = user.Id,  7 EmailAddress = user.EmailAddress,  8 Name = user.Name,  9 PhoneNumber = user.PhoneNumber, 10 Surname = user.Surname, 11 TenantId = user.TenantId, 12 FullName = user.FullName, 13  }; 14  } 15 16 public async Task<Boolean> UpdateUser(UserDto user) 17  { 18 var entity = await _repository.GetEntityById(user.Id); 19 entity.Name = user.Name; 20 entity.Password = user.Password; 21 entity.FullName = user.FullName; 22 entity.Surname = user.Surname; 23 entity.EmailAddress = user.EmailAddress; 24 entity.PhoneNumber = user.PhoneNumber; 25 26 return await _repository.Update(entity); 27 28  } 29 30 public async Task<Boolean> DeleteUser(Int64 userId) 31  { 32 return await _repository.Delete(userId); 33 }

Bill.Demo.Services.Server 服務(wù)

Bill.Demo.Web 客戶端

        public async Task<IActionResult> Delete(Int64 id){var service = ServiceLocator.GetService<IServiceProxyFactory>();var userProxy = service.CreateProxy<IUserService>("User"); await userProxy.DeleteUser(id); return RedirectToAction("User"); } public async Task<JsonResult> GetUser(Int64 id) { var service = ServiceLocator.GetService<IServiceProxyFactory>(); var userProxy = service.CreateProxy<IUserService>("User"); var output= await userProxy.GetUserById(id); return new JsonResult(output); } public async Task<JsonResult> Update(UserDto dto) { var service = ServiceLocator.GetService<IServiceProxyFactory>(); var userProxy = service.CreateProxy<IUserService>("User"); var output = await userProxy.UpdateUser(dto); return new JsonResult(output); }

?碼托管在github,https://github.com/billyang/SurgingDemo

Surging:

博客:http://www.cnblogs.com/fanliang11/

開源:https://github.com/dotnetcore/surging

?

原文:https://www.cnblogs.com/billyang/p/8376076.html

轉(zhuǎn)載于:https://www.cnblogs.com/bigmango/p/10392681.html

總結(jié)

以上是生活随笔為你收集整理的[转载]Surging 分布式微服务框架使用入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。