微服务学习.net5+consul
趁着刚过完年,还没有开始做业务的时候,学习下consul 概念自己去官网看,这里只讲下具体实现
官网下载https://www.consul.io/downloads 我下载的是Windows版本
启动consul命令
consul agent -dev -client 0.0.0.0

新建.net5的api项目,.net5直接默认是swagger

4.新建扩展服务注册到Consul方法,ConsulExtenions.cs
public static void ConsulRegist(this IConfiguration configuration)
{
try
{
string ip = configuration["ip"];
string port = configuration["port"];
string weight = configuration["weight"];
string consulAddress = configuration["ConsulAddress"];
string consulCenter = configuration["ConsulCenter"]; ConsulClient client = new ConsulClient(c =>
{
c.Address = new Uri(consulAddress);
c.Datacenter = consulCenter;
}); client.Agent.ServiceRegister(new AgentServiceRegistration()
{
ID = "CommService" + Guid.NewGuid(),//--唯一的
Name = "CommService",//分组---根据Service
Address = ip,
Port = int.Parse(port),
Tags = new string[] { weight.ToString() },//额外标签信息
Check = new AgentServiceCheck()
{
Interval = TimeSpan.FromSeconds(12),
HTTP = $"http://{ip}:{port}/Api/Health/Index", // 给到200
Timeout = TimeSpan.FromSeconds(5),
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(20)
}//配置心跳
});
Console.WriteLine($"{ip}:{port}--weight:{weight}"); //命令行参数获取
}
catch (Exception ex)
{
Console.WriteLine($"Consul注册:{ex.Message}");
}
}
ip port 配置在appsettings.json里面
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ip": "localhost",
"port": 5000,
"weight": 1,
"ConsulAddress": "http://127.0.0.1:8500",
"ConsulCenter": "dc1"
}
Startup注册ConsulRegist方法
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//if (env.IsDevelopment())
//{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "comm.api v1"));
//} // app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// Consul注册
this.Configuration.ConsulRegist();
}
可以用dotnet 启动2个comm.api.dll 端口号自己随便取,我取的5000和6000 HealthController为配置的心跳api返回的结果

再来看下Consul 会发现多了CommService

2 Instances为端口号5000和6000的api

注册完成了,接下来实现调用看效果图


调用代码
public static AgentService ChooseService(string serviceName)
{
using (ConsulClient client = new ConsulClient(c => c.Address = new Uri("http://localhost:8500")))
{
var services = client.Agent.Services().Result.Response;
// 找出目标服务
var targetServices = services.Where(c => c.Value.Service.Equals(serviceName)).Select(s => s.Value);
// 实现随机负载均衡
var targetService = targetServices.ElementAt(new Random().Next(1, 1000) % targetServices.Count());
Console.WriteLine($"{DateTime.Now} 当前调用服务为:{targetService.Address}:{targetService.Port}"); return targetService;
}
}
public string GetCommUrl()
{
AgentService agentService = ConsulClientExtenions.ChooseService("CommService");
string url = $"http://{agentService.Address}:{agentService.Port}/api/Comm/GetProvince";
return url;
}
需要demo的留言
微服务学习.net5+consul的更多相关文章
- 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现
原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...
- .NET Core微服务之基于Consul实现服务治理
Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.Consul基础介绍 Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发 ...
- 微服务学习笔记(1)——使用MagicOnion实现gRPC
原文:微服务学习笔记(1)--使用MagicOnion实现gRPC 1.什么是gRPC 官方文档:https://grpc.io/docs/guides/index.html 2.什么是MagicOn ...
- .NET Core 微服务学习与实践系列文章目录索引(2019版)
参考网址: https://archy.blog.csdn.net/article/details/103659692 2018年,我开始学习和实践.NET Core,并开始了微服务的学习,以及通过各 ...
- SpringCloud微服务学习笔记
SpringCloud微服务学习笔记 项目地址: https://github.com/taoweidong/Micro-service-learning 单体架构(Monolithic架构) Mon ...
- .NET Core微服务一:Consul服务中心
本文的项目代码,在文章结尾处可以下载. 防爬虫,本文的网址是:https://www.cnblogs.com/shousiji/p/12253295.html 本文使用的环境:Windows10 64 ...
- Spring Cloud微服务学习笔记
Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...
- .NET Core微服务之基于Consul实现服务治理(续)
Tip: 此篇已加入.NET Core微服务基础系列文章索引 上一篇发布之后,很多人点赞和评论,不胜惶恐,这一篇把上一篇没有弄到的东西补一下,也算是给各位前来询问的朋友的一些回复吧. 一.Consul ...
- 最全的.NET Core跨平台微服务学习资源
一.Asp.net Core基础 微软中文官网:https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/ 微软英文官网:https:/ ...
随机推荐
- HanLP 下载和配置
方式一.Maven 为了方便用户,特提供内置了数据包的Portable版,只需在pom.xml加入: <dependency> <groupId>com.hankcs</ ...
- js实现购物车左滑删除
使用 js 和jquery动画实现购物车左滑,请引入jquery库,不然会报错哦! 首页编写页面,注意布局,滑动分成两部分,商品图片和信息放在一个布局,删除和移入收藏放在一起. 其中js用到了 tou ...
- CF 1326 D. Prefix-Suffix Palindrome
D. Prefix-Suffix Palindrome 题意 给一个字符串 s,求一个字符串 t,t 由 s 的某个前缀以及某个后缀拼接而成,且 t 是回文串,长度不能超过 s.输出最长的 t 分析 ...
- 配置VS2013 + opencv 2.4.10
其实我内心是极力反对装这么老的版本的,但是要交课堂作业~~好无奈 [注] : 如果按照本文配置不成功,可以试一下其他博客里面的配置(多试一试总能成功的) https://jingyan.baidu.c ...
- python+selenium+bs4爬取百度文库内文字 && selenium 元素可以定位到,但是无法点击问题 && pycharm多行缩进、左移
先说一下可能用到的一些python知识 一.python中使用的是unicode编码, 而日常文本使用各类编码如:gbk utf-8 等等所以使用python进行文字读写操作时候经常会出现各种错误, ...
- Codeforces 1144F Graph Without Long Directed Paths DFS染色
题意: 输入一张有向图,无自回路和重边,判断能否将它变为有向图,使得图中任意一条路径长度都小于2. 如果可以,按照输入的边的顺序输出构造的每条边的方向,构造的边与输入的方向一致就输出1,否则输出0. ...
- Codeforces Round #645 (Div. 2) D. The Best Vacation (贪心,二分)
题意:一年有\(n\)个月,每月有\(d_{i}\)天,找出连续的\(x\)天,使得这\(x\)天的日期总和最大,任意一年都能选. 题解:首先要先贪心,得到:连续的\(x\)天的最后一天一定是某个月的 ...
- nuoyanli 520 Let‘s play computer game
H题 描述 xxxxxxxxx在疫情期间迷上了一款游戏,这个游戏一共有nnn个地点(编号为1--n1--n1--n),他每次从一个地点移动到另外一个地点需要消耗 一定的能量,每一个地点都有一些珠宝,输 ...
- Codeforces Round #654 (Div. 2) B. Magical Calendar (结论)
题意:你需要在长度从\(1\)~\(k\),宽度无限的网格图中造图形(每个点四周必须连通),问最多能造出多少种不同的图形. 题解:感觉没什么好说的,就是画图找规律,如果\(r\ge n\)的话(即没有 ...
- Linux-进程管理命令
目录 进程管理命令-ps(静态) 进程管理命令-top(动态) 什么是中断 管理进程状态 kill命令 killall命令 pkill命令 pidof 和 pgrep 补充 关于优先级PR和NI 后台 ...