趁着刚过完年,还没有开始做业务的时候,学习下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. 将字符串进行md5加密

    import java.security.MessageDigest; public class MD5Tools { /** * 将字符串进行md5加密 */ public static Strin ...

  2. WAMP3.1.3自定义根目录

    1.首先找到httpd.conf 文件,搜索documentroot 修改前:DocumentRoot "${INSTALL_DIR}/www" <Directory &qu ...

  3. Calendar 日期判断 等于 。小于。大于

    public static void main(String[] args) throws Exception { String startTime = "2012-12-12 12:45: ...

  4. netty写Echo Server & Client完整步骤教程(图文)

    1.创建Maven工程 1.1 父节点的pom.xml代码(root pom文件) 1 <?xml version="1.0" encoding="UTF-8&qu ...

  5. Scala集合库、模式匹配和样例类

    package com.yz8 import org.junit.Test class test { @Test def test: Unit = { val ints = List(1,5,7,6, ...

  6. B 等差素数列

    B 等差素数列:2,3,5,7,11,13,....是素数序列.类似:7,37,67,97,127,157 这样完全由素数组成的等差数列,叫等差素数数列.上边的数列公差为30,长度为6.2004年,格 ...

  7. Codeforces Round #177 (Div. 2) B. Polo the Penguin and Matrix (贪心,数学)

    题意:给你一个\(n\)x\(m\)的矩阵,可以对矩阵的所有元素进行\(\pm d\),问能否使得所有元素相等. 题解:我们可以直接记录一个\(n*m\)的数组存入所有数,所以\((a_1+xd)=( ...

  8. Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math (数学)

    题意:给你一个正整数\(n\),求两个正整数\(a\)和\(b\),使得\(a+b=n\),并且\(LCM(a,b)\)要尽可能的小. 题解:首先对于偶数,构造\(\frac{n}{2}\)和\(\f ...

  9. Gome 高性能撮合引擎微服务

    Gome 高性能撮合引擎微服务 使用 Golang 做计算,gRPC 做服务,ProtoBuf 做数据交换,RabbitMQ 做队列,Redis 做缓存实现的高性能撮合引擎微服务 依赖 具体依赖信息可 ...

  10. codeforces 1077D Cutting Out 【二分】

    题目:戳这里 题意:给n个数的数组,要求找k个数满足,这k个数在数组中出现的次数最多. 解题思路:k个数每个数出现次数都要最大化,可以想到二分下限,主要是正确的二分不好写. 附ac代码: 1 #inc ...