1.下载打开Consul

笔者是windows下面开发的(也可以使用Docker)。

官网下载windows的Consul

https://www.consul.io/

使用cmd窗口打开,输入consul agent -dev

访问默认127.0.0.1:8500就可以看到界面化的Consul

2.在服务端注册

接着上一篇

using Consul;
using Grpc.Core;
using GRPCServer.Entity;
using MagicOnion.Server;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System; namespace GRPCServer
{
public class Startup
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); MagicOnionServiceDefinition service = MagicOnionEngine.BuildServerServiceDefinition(new MagicOnionOptions(true)
{
MagicOnionLogger = new MagicOnionLogToGrpcLogger()
});
Server server = new Server
{
Services = { service },
Ports = { new ServerPort(this.Configuration["Service:LocalIPAddress"], Convert.ToInt32(this.Configuration["Service:Port"]), ServerCredentials.Insecure) }
};
server.Start(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc(); ServiceEntity serviceEntity = new ServiceEntity
{
IP = this.Configuration["Service:LocalIPAddress"],
Port = Convert.ToInt32(this.Configuration["Service:Port"]),
ServiceName = this.Configuration["Service:Name"],
ConsulIP = this.Configuration["Consul:IP"],
ConsulPort = Convert.ToInt32(this.Configuration["Consul:Port"])
};
var consulClient = new ConsulClient(x => x.Address = new Uri($"http://{serviceEntity.ConsulIP}:{serviceEntity.ConsulPort}"));//请求注册的 Consul 地址
var httpCheck = new AgentServiceCheck()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务启动多久后注册
Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔,或者称为心跳间隔
HTTP = this.Configuration["Service:Examination"],//健康检查地址
Timeout = TimeSpan.FromSeconds(5)
};
var registration = new AgentServiceRegistration()
{
Checks = new[] { httpCheck },
ID = Guid.NewGuid().ToString(),
Name = serviceEntity.ServiceName,
Address = serviceEntity.IP,
Port = serviceEntity.Port,
Tags = new[] { $"urlprefix-/{serviceEntity.ServiceName}" }//添加 urlprefix-/servicename 格式的 tag 标签,以便 Fabio 识别
};
consulClient.Agent.ServiceRegister(registration).Wait();//服务启动时注册,内部实现其实就是使用 Consul API 进行注册(HttpClient发起)
lifetime.ApplicationStopping.Register(() =>
{
consulClient.Agent.ServiceDeregister(registration.ID).Wait();//服务停止时取消注册
});
}
}
}

appsettings.json

{
"Service": {
"Name": "Test3",
"Port": "8083",
"LocalIPAddress": "192.168.1.8",
"Examination": "http://192.168.1.8:5000/api/Values"
},
"Consul": {
"IP": "127.0.0.1",
"Port": "8500"
}
}
3.客户端调用
using Consul;
using Grpc.Core;
using MagicOnion.Client;
using ServerDefinition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; var aaa= AvaliableServices("Test3","").Result; public static async Task<ServiceEntry[]> AvaliableServices(string name, string tags)
{
var services = new List<ServiceEntry>();
using (var client = new ConsulClient())
{
foreach (var tag in tags.Split(','))
{
var result = await client.Health.Service(name, !string.IsNullOrEmpty(tag) ? tag : null, true).ConfigureAwait(false);
foreach (var item in result.Response)
{
if (!services.Any(service => service.Node.Address == item.Node.Address
&& service.Service.Port == item.Service.Port))
{
services.Add(item);
}
}
}
//交集处理,仅取出完全匹配服务
foreach (var tag in tags.Split(','))
{
if (string.IsNullOrEmpty(tag))
{
continue;
}
var alsorans = services.Where(service => !service.Service.Tags.Contains(tag)).ToList();
foreach (var alsoran in alsorans)
{
services.Remove(alsoran);
}
}
}
return services.ToArray();
}
4.思考

这个时候我就能通过'Test3'来获得Test3的服务和接口。

但是我是使用的MagicOnion,还是没办法拿到我定义的方法SumAsync

怎么办?

1.引用ITest (让微服务之间有引用,不太好)

2.使用网关

5.预告

下一篇我会想法办法使他们能相互通讯(其实我还不知道怎么搞)

使用Consul 实现 MagicOnion(GRpc) 服务注册和发现的更多相关文章

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

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

  2. 基于 Consul 实现 MagicOnion(GRpc) 服务注册与发现

    0.简介 0.1 什么是 Consul Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置. 这里所谓的服务,不仅仅包括常用的 Api 这些服务,也包括软件开发过程 ...

  3. C#使用Consul集群进行服务注册与发现

    前言 我个人觉得,中间件的部署与使用是非常难记忆的:也就是说,如果两次使用中间件的时间间隔比较长,那基本上等于要重新学习使用. 所以,我觉得学习中间件的文章,越详细越好:因为,这对作者而言也是一份珍贵 ...

  4. (8)ASP.NET Core3.1 Ocelot Consul服务注册与发现

    1.服务注册与发现(Service Discovery) ●服务注册:我们通过在每个服务实例写入注册代码,实例在启动的时候会先去注册中心(例如Consul.ZooKeeper.etcd.Eureka) ...

  5. python与consul 实现gRPC服务注册-发现

    背景 通过对gRPC的介绍我们知道,当正常启动服务后,我们只需要知道ip,port就可以进行gRPC的连接.可以想到,这种方式并不适合用于线上环境,因为这样直连的话就失去了扩展性,当需要多机部署的时候 ...

  6. Docker+Consul+Registrator 实现服务注册与发现

    Docker+Consul+Registrator实现服务注册与发现 逻辑图 实现nginx节点自动化加入容器IP代理 1.三台Consul agent server作为高可用通过Consul Tem ...

  7. 分布式服务注册和发现consul 简要介绍

    Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,Consul的方案更"一站式",内置了服务注册与发现框 架 ...

  8. Spring Cloud Consul 实现服务注册和发现

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...

  9. Spring Boot + Spring Cloud 构建微服务系统(一):服务注册和发现(Consul)

    使用Consul提供注册和发现服务 什么是 Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul ...

随机推荐

  1. linux-kernel-4.4 移植 (2)解决上部遗留DMA-PL330的问题

    查看drivers/tty/serial/samsung.c文件发现,当传输数据量小于ourport->min_dma_size时,不使用DMA,大于等于min_mda_size时才是使用DMA ...

  2. Skipping acquire of configured file ···doesn't support architecture 'i386' acquire of configured file

    系统更新的时候报错: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'http://rep ...

  3. jsp页面的html代码显示不出来,提示Uncaught SyntaxError: Unexpected token <

    jsp页面的html代码显示不出来,提示Uncaught SyntaxError: Unexpected token < <input type="hidden" na ...

  4. Maven 的这 7 个问题你思考过没有?

    在如今的互联网项目开发当中,特别是Java领域,可以说Maven随处可见.Maven的仓库管理.依赖管理.继承和聚合等特性为项目的构建提供了一整套完善的解决方案,可以说如果你搞不懂Maven,那么一个 ...

  5. spring jar包解读(转)

    作者:http://www.cnblogs.com/leehongee/archive/2012/10/01/2709541.html spring.jar 是包含有完整发布模块的单个jar 包.但是 ...

  6. asp.net core web 项目附加进程调试

    之前asp.net web项目在部署IIS站点的时候可以直接选择项目目录,不用发布,然后附加进程的时候,找到w3wp.exe开头的进程,再根据用户名找到要附加的进程,就可以附加进程调试了.但asp.n ...

  7. maven学习四:maven集成jetty插件发布web项目 标签: maven

    http://blog.csdn.net/u014079773/article/details/50167833

  8. 使用kbmmw smarthttpservice 简单返回数据库结果

    这个很简单,直接上码. 服务器端声明过程 [kbmMW_Rest('method:get, path:querytable')] [kbmMW_Method] function querytable( ...

  9. 解决ORA-30036:无法按8扩展段(在还原表空间‘XXXX’中)

    在update一数据量很大的表时,提示“ORA-30036:无法按8扩展段” 度娘了下原因与解决办法:   1.查询了一下undo表空间的使用,发现已经超过了80% SELECT a.tablespa ...

  10. 实现PHP服务端和c#客户端数据交换

    服务端实现功能1,数据库的访问dbhelper.php包括执行语句返回多行,返回json数据,返回单条记录,返回第一行第一列的整数,返回第一行第一列的浮点数,返回第一行第一列的双精度数,返回第一行第一 ...