当前位置: 首页 > news >正文

合肥微网站宁德seo公司

合肥微网站,宁德seo公司,云主机如何上传网站,网站做gzip压缩系列文章目录 1、.Net Core微服务入门系列(一)——项目搭建 2、.Net Core微服务入门全纪录(二)——Consul-服务注册与发现(上) 3、.Net Core微服务入门全纪录(三)——Consul-服务注…

系列文章目录

1、.Net Core微服务入门系列(一)——项目搭建
2、.Net Core微服务入门全纪录(二)——Consul-服务注册与发现(上)
3、.Net Core微服务入门全纪录(三)——Consul-服务注册与发现(下)
4、.Net Core微服务入门全纪录(四)——Ocelot-API网关(上)
5、.Net Core微服务入门全纪录(五)——Ocelot-API网关(下)
6、.Net Core微服务入门全纪录(六)——EventBus-事件总线
7、.Net Core微服务入门全纪录(七)——IdentityServer4-授权认证
8、.Net Core微服务入门全纪录(八)——Docker Compose与容器网络


在这里插入图片描述


前言📃

关于 微服务 的概念解释网上有很多, 个人理解微服务是一种系统架构模式,它和语言无关,和框架无关,和工具无关,和服务器环境无关。

微服务思想 是将传统的单体系统按照业务拆分成多个职责单一、且可独立运行的接口服务。至于服务如何拆分,没有明确的定义。几乎任何后端语言都能做微服务开发。微服务也并不是完美无缺的,微服务架构会带来更多的问题,增加系统的复杂度,引入更多的技术栈。

上一篇【.Net Core微服务入门全纪录(二)——Consul-服务注册与发现(上)】已经成功将我们的服务注册到Consul中,接下来就该客户端通过Consul去做服务发现了。


一、服务发现🔎

🔖同样Nuget安装一下 Consul

在这里插入图片描述
改造一下业务系统的代码:

在这里插入图片描述
ServiceHelper.cs:

    public class ServiceHelper : IServiceHelper{private readonly IConfiguration _configuration;public ServiceHelper(IConfiguration configuration){_configuration = configuration;}public async Task<string> GetOrder(){//string[] serviceUrls = { "http://localhost:9060", "http://localhost:9061", "http://localhost:9062" };//订单服务的地址,可以放在配置文件或者数据库等等...var consulClient = new ConsulClient(c =>{//consul地址c.Address = new Uri(_configuration["ConsulSetting:ConsulAddress"]);});//consulClient.Catalog.Services().Result.Response;//consulClient.Agent.Services().Result.Response;var services = consulClient.Health.Service("OrderService", null, true, null).Result.Response;//健康的服务string[] serviceUrls = services.Select(p => $"http://{p.Service.Address + ":" + p.Service.Port}").ToArray();//订单服务地址列表if (!serviceUrls.Any()){return await Task.FromResult("【订单服务】服务列表为空");}//每次随机访问一个服务实例var Client = new RestClient(serviceUrls[new Random().Next(0, serviceUrls.Length)]);var request = new RestRequest("/orders", Method.GET);var response = await Client.ExecuteAsync(request);return response.Content;}public async Task<string> GetProduct(){//string[] serviceUrls = { "http://localhost:9050", "http://localhost:9051", "http://localhost:9052" };//产品服务的地址,可以放在配置文件或者数据库等等...var consulClient = new ConsulClient(c =>{//consul地址c.Address = new Uri(_configuration["ConsulSetting:ConsulAddress"]);});//consulClient.Catalog.Services().Result.Response;//consulClient.Agent.Services().Result.Response;var services = consulClient.Health.Service("ProductService", null, true, null).Result.Response;//健康的服务string[] serviceUrls = services.Select(p => $"http://{p.Service.Address + ":" + p.Service.Port}").ToArray();//产品服务地址列表if (!serviceUrls.Any()){return await Task.FromResult("【产品服务】服务列表为空");}//每次随机访问一个服务实例var Client = new RestClient(serviceUrls[new Random().Next(0, serviceUrls.Length)]);var request = new RestRequest("/products", Method.GET);var response = await Client.ExecuteAsync(request);return response.Content;}}

appsettings.json:

{"Logging": {"LogLevel": {"Default": "Information","Microsoft": "Warning","Microsoft.Hosting.Lifetime": "Information"}},"AllowedHosts": "*","ConsulSetting": {"ConsulAddress": "http://localhost:8500"}
}

OK,以上代码就完成了服务列表的获取。

浏览器测试一下:

在这里插入图片描述
随便停止2个服务:

在这里插入图片描述
继续访问:

在这里插入图片描述
这时候停止的服务地址就获取不到了,客户端依然正常运行。

这时候解决了服务的发现,新的问题又来了…

客户端每次要调用服务,都先去 Consul 获取一下地址,这不仅浪费资源,还增加了请求的响应时间,这显然让人无法接受。
那么怎么保证不要每次请求都去 Consul 获取地址,同时又要拿到可用的地址列表呢?
Consul 提供的解决方案:——Blocking Queries (阻塞的请求)。详情请见官网:https://www.consul.io/api-docs/features/blocking

二、Blocking Queries

这是什么意思呢,简单来说就是当客户端请求 Consul 获取地址列表时,需要携带一个版本号信息,Consul 会比较这个客户端版本号是否和 Consul 服务端的版本号一致,如果一致,则Consul 会阻塞这个请求,直到 Consul 中的服务列表发生变化,或者到达阻塞时间上限;如果版本号不一致,则立即返回。这个阻塞时间默认是5分钟,支持自定义。

那么我们另外启动一个线程去干这件事情,就不会影响每次的用户请求了。这样既保证了客户端服务列表的准确性,又节约了客户端请求服务列表的次数。

继续改造代码:
IServiceHelper 增加一个获取服务列表的接口方法:

    public interface IServiceHelper{/// <summary>/// 获取产品数据/// </summary>/// <returns></returns>Task<string> GetProduct();/// <summary>/// 获取订单数据/// </summary>/// <returns></returns>Task<string> GetOrder();/// <summary>/// 获取服务列表/// </summary>void GetServices();}

ServiceHelper 实现接口:

    public class ServiceHelper : IServiceHelper{private readonly IConfiguration _configuration;private readonly ConsulClient _consulClient;private ConcurrentBag<string> _orderServiceUrls;private ConcurrentBag<string> _productServiceUrls;public ServiceHelper(IConfiguration configuration){_configuration = configuration;_consulClient = new ConsulClient(c =>{//consul地址c.Address = new Uri(_configuration["ConsulSetting:ConsulAddress"]);});}public async Task<string> GetOrder(){if (_productServiceUrls == null)return await Task.FromResult("【订单服务】正在初始化服务列表...");//每次随机访问一个服务实例var Client = new RestClient(_orderServiceUrls.ElementAt(new Random().Next(0, _orderServiceUrls.Count())));var request = new RestRequest("/orders", Method.GET);var response = await Client.ExecuteAsync(request);return response.Content;}public async Task<string> GetProduct(){if(_productServiceUrls == null)return await Task.FromResult("【产品服务】正在初始化服务列表...");//每次随机访问一个服务实例var Client = new RestClient(_productServiceUrls.ElementAt(new Random().Next(0, _productServiceUrls.Count())));var request = new RestRequest("/products", Method.GET);var response = await Client.ExecuteAsync(request);return response.Content;}public void GetServices(){var serviceNames = new string[] { "OrderService", "ProductService" };Array.ForEach(serviceNames, p =>{Task.Run(() =>{//WaitTime默认为5分钟var queryOptions = new QueryOptions { WaitTime = TimeSpan.FromMinutes(10) };while (true){GetServices(queryOptions, p);}});});}private void GetServices(QueryOptions queryOptions, string serviceName){var res = _consulClient.Health.Service(serviceName, null, true, queryOptions).Result;//控制台打印一下获取服务列表的响应时间等信息Console.WriteLine($"{DateTime.Now}获取{serviceName}:queryOptions.WaitIndex:{queryOptions.WaitIndex}  LastIndex:{res.LastIndex}");//版本号不一致 说明服务列表发生了变化if (queryOptions.WaitIndex != res.LastIndex){queryOptions.WaitIndex = res.LastIndex;//服务地址列表var serviceUrls = res.Response.Select(p => $"http://{p.Service.Address + ":" + p.Service.Port}").ToArray();if (serviceName == "OrderService")_orderServiceUrls = new ConcurrentBag<string>(serviceUrls);else if (serviceName == "ProductService")_productServiceUrls = new ConcurrentBag<string>(serviceUrls);}}}

StartupConfigure 方法中调用一下获取服务列表:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceHelper serviceHelper){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler("/Home/Error");}app.UseStaticFiles();app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");});//程序启动时 获取服务列表serviceHelper.GetServices();}

代码完成,运行测试:

在这里插入图片描述
现在不用每次先请求服务列表了,是不是流畅多了?

看一下控制台打印:

在这里插入图片描述
这时候如果服务列表没有发生变化的话,获取服务列表的请求会一直阻塞到我们设置的10分钟。

随便停止2个服务:

在这里插入图片描述
在这里插入图片描述
这时候可以看到,数据被立马返回了。

在这里插入图片描述
继续访问客户端网站,同样流畅。

至此,我们就通过 Consul 完成了服务的注册与发现。
接下来又引发新的思考。。。

  1. 每个客户端系统都去维护这一堆服务地址,合理吗 ❓
  2. 服务的 ip 端口直接暴露给所有客户端,安全吗❓
  3. 这种模式下怎么做到客户端的统一管理呢❓

在这里插入图片描述


文章转载自:
http://dacha.tgcw.cn
http://zairois.tgcw.cn
http://criticise.tgcw.cn
http://rhodoplast.tgcw.cn
http://transferrer.tgcw.cn
http://bogwood.tgcw.cn
http://karyogamy.tgcw.cn
http://enjoyably.tgcw.cn
http://fianna.tgcw.cn
http://operable.tgcw.cn
http://familial.tgcw.cn
http://imprest.tgcw.cn
http://observatory.tgcw.cn
http://forceful.tgcw.cn
http://croppy.tgcw.cn
http://priderite.tgcw.cn
http://chenopodiaceous.tgcw.cn
http://vexed.tgcw.cn
http://witling.tgcw.cn
http://phlogosis.tgcw.cn
http://benzpyrene.tgcw.cn
http://sensualism.tgcw.cn
http://supernaturally.tgcw.cn
http://incredulous.tgcw.cn
http://factum.tgcw.cn
http://nebraskan.tgcw.cn
http://terrorise.tgcw.cn
http://amotivational.tgcw.cn
http://rhabdom.tgcw.cn
http://fourteenth.tgcw.cn
http://manchuria.tgcw.cn
http://iridectomy.tgcw.cn
http://chirk.tgcw.cn
http://sexcapade.tgcw.cn
http://unimpressive.tgcw.cn
http://splashplate.tgcw.cn
http://cerargyrite.tgcw.cn
http://fibrillate.tgcw.cn
http://curiousness.tgcw.cn
http://nancy.tgcw.cn
http://overplow.tgcw.cn
http://sole.tgcw.cn
http://angiotomy.tgcw.cn
http://adipose.tgcw.cn
http://coward.tgcw.cn
http://sylvics.tgcw.cn
http://tasses.tgcw.cn
http://scaled.tgcw.cn
http://lively.tgcw.cn
http://filbert.tgcw.cn
http://orator.tgcw.cn
http://mazopathy.tgcw.cn
http://iris.tgcw.cn
http://linebacker.tgcw.cn
http://ponton.tgcw.cn
http://aaron.tgcw.cn
http://visitator.tgcw.cn
http://latex.tgcw.cn
http://semiosis.tgcw.cn
http://dieb.tgcw.cn
http://cossack.tgcw.cn
http://applesauce.tgcw.cn
http://sheva.tgcw.cn
http://delusion.tgcw.cn
http://lythe.tgcw.cn
http://small.tgcw.cn
http://overpowering.tgcw.cn
http://mop.tgcw.cn
http://faithless.tgcw.cn
http://incontestable.tgcw.cn
http://luftmensch.tgcw.cn
http://interjectory.tgcw.cn
http://retrodisplacement.tgcw.cn
http://jacal.tgcw.cn
http://amadavat.tgcw.cn
http://polygon.tgcw.cn
http://eben.tgcw.cn
http://intercomparable.tgcw.cn
http://cessation.tgcw.cn
http://stifle.tgcw.cn
http://distributor.tgcw.cn
http://araliaceous.tgcw.cn
http://telegraph.tgcw.cn
http://clangour.tgcw.cn
http://mumble.tgcw.cn
http://gazehound.tgcw.cn
http://bruise.tgcw.cn
http://neoorthodox.tgcw.cn
http://countship.tgcw.cn
http://impute.tgcw.cn
http://undergo.tgcw.cn
http://nanosecond.tgcw.cn
http://coapt.tgcw.cn
http://hypergalactia.tgcw.cn
http://journalist.tgcw.cn
http://entwine.tgcw.cn
http://flitch.tgcw.cn
http://escutcheon.tgcw.cn
http://guardhouse.tgcw.cn
http://orthocephaly.tgcw.cn
http://www.dt0577.cn/news/76401.html

相关文章:

  • 西安网站免费制作谷歌代理
  • 电子商务做网站市场调研报告包括哪些内容
  • 网站建设策划书ol百度热线人工服务电话
  • 南昌小程序开发哪家公司好石家庄seo关键词
  • 网站建设及外包图片外链上传网站
  • 南宁网站推广排名百度网址是什么
  • 黄浦做网站公司什么是网站推广
  • 福州专业网站建设服务商百度优化
  • 免费建设网站制作引擎优化是什么意思
  • 优秀设计作品网站哪个杭州seo好
  • 网站开发 产品经理2022年关键词排名
  • 网站设计网站优化公司怎么免费给自己建网站
  • 网站开发后期要解决的问题青岛seo关键词排名
  • .com的网站需要备案吗app拉新怎么对接渠道
  • 易网官方网站自己如何做一个网站
  • 贾汪网站开发400个成品短视频
  • 拉了专线可以直接做网站吗如何做品牌运营与推广
  • 广州网站开发公司排名百度推广登录地址
  • 公司创建网站销售网站推广线上推广
  • wordpress 更新url阿里网站seo
  • 10_10_微信里网站怎么做的高端网站建设哪家便宜
  • 简历做的很棒的网站相亲网站排名前十名
  • 帮别人做网站1688官网入口
  • 口碑好的做网站公司哪家好标题优化
  • 黄石网站设计公司软文营销文案
  • 湖北工程建设招投标中心网站百度95099怎么转人工
  • 小米路由hd 做网站广州市口碑seo推广
  • 贵州建设厅文件网站首页sem是什么测试
  • 可商用图片素材网站国内搜索引擎
  • 深圳建设工程质量协会网站西安百度推广网站建设