趁着刚过完年,还没有开始做业务的时候,学习下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的更多相关文章

  1. 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现

    原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...

  2. .NET Core微服务之基于Consul实现服务治理

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.Consul基础介绍 Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发 ...

  3. 微服务学习笔记(1)——使用MagicOnion实现gRPC

    原文:微服务学习笔记(1)--使用MagicOnion实现gRPC 1.什么是gRPC 官方文档:https://grpc.io/docs/guides/index.html 2.什么是MagicOn ...

  4. .NET Core 微服务学习与实践系列文章目录索引(2019版)

    参考网址: https://archy.blog.csdn.net/article/details/103659692 2018年,我开始学习和实践.NET Core,并开始了微服务的学习,以及通过各 ...

  5. SpringCloud微服务学习笔记

    SpringCloud微服务学习笔记 项目地址: https://github.com/taoweidong/Micro-service-learning 单体架构(Monolithic架构) Mon ...

  6. .NET Core微服务一:Consul服务中心

    本文的项目代码,在文章结尾处可以下载. 防爬虫,本文的网址是:https://www.cnblogs.com/shousiji/p/12253295.html 本文使用的环境:Windows10 64 ...

  7. Spring Cloud微服务学习笔记

    Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...

  8. .NET Core微服务之基于Consul实现服务治理(续)

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 上一篇发布之后,很多人点赞和评论,不胜惶恐,这一篇把上一篇没有弄到的东西补一下,也算是给各位前来询问的朋友的一些回复吧. 一.Consul ...

  9. 最全的.NET Core跨平台微服务学习资源

    一.Asp.net Core基础 微软中文官网:https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/ 微软英文官网:https:/ ...

随机推荐

  1. H3C交换机端口聚合配置

    1.接入交换机: interface Ten-GigabitEthernet1/0/21 port link-mode bridge port link-type trunk port trunk p ...

  2. Linux Bash编程

    在Linux系统介绍中,介绍了shell的多个版本,现在的Linux发行版基本都默认使用bash(Bourne Again shell),兼容Bourne shell (sh),本文将简要介绍Bash ...

  3. vue、element-ui 依权限动态导航 v-for、v-if

    在管理后台需要按用户权限展示不同导航菜单时,我们需要从后端请求数据来进行菜单渲染. 但由于v-for与v-if不能在同一标签内使用(存在性能问题:v-for 具有比 v-if 更高的优先级,意味着 v ...

  4. 查看文件MD5值

    Windows 打开命令窗口(Win+R),然后输入cmd ·输入命令certutil -hashfile 文件绝对路径 MD5 Linux MD5算法常常被用来验证网络文件传输的完整性,防止文件被人 ...

  5. B 明码

    B 明码 :汉字的字形存在于字库中,即便在今天,16点阵的字库也仍然使用广泛. 16点阵的字库把每个汉字看成是16x16个像素信息.并把这些信息记录在字节中. 一个字节可以存储8位信息,用32个字节就 ...

  6. 吉哥系列故事——完美队形II(马拉车算法)

    吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求 ...

  7. Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree (dfs计数,树)

    题意:给你含有\(n\)个节点,\(n-1\)条边的树,以及\(m\)个质数和\(1\),你需要在这\(m\)个质数和一个\(1\)选择数(质数只能选一次,\(1\)可以多选)给\(n-1\)条边赋值 ...

  8. HDU 3416 Marriage Match IV (最短路径&&最大流)

    /*题意: 有 n 个城市,知道了起点和终点,有 m 条有向边,问从起点到终点的最短路一共有多少条.这是一个有向图,建边的时候要注意!!解题思路:这题的关键就是找到哪些边可以构成最短路,其实之前做最短 ...

  9. 创建java文件和注释

    创建java文件和注释 一 创建java文件 在文件夹里创建txt文本文件,后将格式改为.java, 输入 1 public class Hello{ 2 public static void mai ...

  10. codeforces 1019B The hat 【交互题+二分搜索】

    题目链接:戳这里 学习题解:戳这里