微服务学习笔记(1)——使用MagicOnion实现gRPC
原文:微服务学习笔记(1)——使用MagicOnion实现gRPC
1.什么是gRPC
官方文档:https://grpc.io/docs/guides/index.html
2.什么是MagicOnion
MagicOnion开源地址:https://github.com/Cysharp/MagicOnion
3.服务端代码
新建一个WebAPI项目
using MagicOnion;
namespace ServerDefinition
{
// 定义接口和方法,IService,UnaryResult是MagicOnion自带
public interface ITest : IService<ITest>
{
UnaryResult<string> SumAsync(int x, int y);
}
}
using MagicOnion;
using MagicOnion.Server;
using ServerDefinition;
namespace GRPCServer
{
// 实现接口,ServiceBase是MagicOnion自带
public class Test : ServiceBase<ITest>, ITest
{
public async UnaryResult<string> SumAsync(int x, int y) => (x + y).ToString();
}
}
using Grpc.Core;
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();
}
}
}
4.客户端
新建一个控制台程序
using Consul;
using Grpc.Core;
using MagicOnion.Client;
using ServerDefinition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
// 然后你就可以根据IP和端口拿到对于的服务
var channel = new Channel("192.168.1.8", 8080, ChannelCredentials.Insecure);
var client = MagicOnionClient.Create<ITest>(channel);
// 调用
var result = client.SumAsync(100, 200).ResponseAsync.Result;
Console.WriteLine("Client Received:" + result);
var channel2 = new Channel("192.168.1.8", 8081, ChannelCredentials.Insecure);
var client2 = MagicOnionClient.Create<ITest>(channel2);
var result2 = client2.SumAsync(100, 200).ResponseAsync.Result;
Console.WriteLine("Client Received:" + result2);
}
}
}
5. 思考
GRPC项目创建多个之后,需要一个服务注册和发现的工具。
6.下一篇预告。
使用Consul 实现 MagicOnion(GRpc) 服务注册与发现
微服务学习笔记(1)——使用MagicOnion实现gRPC的更多相关文章
- 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现
原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...
- SpringCloud微服务学习笔记
SpringCloud微服务学习笔记 项目地址: https://github.com/taoweidong/Micro-service-learning 单体架构(Monolithic架构) Mon ...
- Spring Cloud微服务学习笔记
Spring Cloud微服务学习笔记 SOA->Dubbo 微服务架构->Spring Cloud提供了一个一站式的微服务解决方案 第一部分 微服务架构 1 互联网应用架构发展 那些迫使 ...
- docker入门与部署微服务--学习笔记
最近公司进一步去windows,走向 linux+云化. 原来的一大坨windows虚拟机服务器都要转向linux, 既然走向linux的话,那么docker肯定是要涉足的. 故学习了docker入门 ...
- 微服务学习笔记一:Spring Cloud简介
1.Spring Cloud是一个工具集:Spring Cloud是在Spring Boot的基础上构建的,用于简化分布式系统构建的工具集:使架构师在创建和发布微服务时极为便捷和有效. Sp ...
- 微服务学习笔记二:Eureka服务注册发现
Eureka服务注册发现 服务发现:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移. 1. Service Discovery: Eureka S ...
- 微服务学习笔记——Spring Boot特性
1. 创建独立的Spring应用程序 2. 嵌入的Tomcat,无需部署WAR文件 3. 简化Maven配置 4. 自动配置Spring 5. 提供生产就绪型功能,如指标,健康检查和外部配置 6. 开 ...
- .NET Core 微服务学习与实践系列文章目录索引(2019版)
参考网址: https://archy.blog.csdn.net/article/details/103659692 2018年,我开始学习和实践.NET Core,并开始了微服务的学习,以及通过各 ...
- 一起学习 微服务(MicroServices)-笔记
笔记 微服务特性: 1. 小 专注与做一件事(适合团队就是最好的) 2. 松耦合 独立部署 3. 进程独立 4. 轻量级通信机制 实践: 1. 微服务周边的一系列基础建设 Load Balancing ...
随机推荐
- graphicview和widgets没本质区别。它只是更轻量级,更灵活,性能更高的widgets
graphicview和widgets没本质区别.它只是更轻量级,更灵活,性能更高的widgets.核心就是把widgets变成了更轻量级的graphicitem,把QWidget的各种事件转换成了g ...
- 【例 7-12 UVA - 1343】The Rotation Game
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 每次抽动操作最多只会让中间那一块的区域离目标的"距离"减少1. 以这个作为剪枝. 枚举最大深度. ...
- [Angular] Using the Argon 2 Hashing Function In Our Sign Up Backend Service
Which hash algorithom to choose for new application: https://www.owasp.org/index.php/Password_Storag ...
- __INLINE
- Datasets for MachineLearning
Public datasets for machine learning http://homepages.inf.ed.ac.uk/rbf/IAPR/researchers/MLPAGES/mld ...
- C# AutoMapper
http://www.cnblogs.com/xlhblogs/p/3356748.html
- mysql常见故障诊断
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010230971/article/details/80335578 作为故障预警,应该尽量把问题扼 ...
- vue学习笔记二:v-if和v-show的区别
v-if vs v-show v-if 是“真正的”条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建. v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做—— ...
- ZOJ 2405 Specialized Four-Digit Numbers
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1405 要求找出4位数所有10进制.12进制.16进制他们各位数字之和相等. # ...
- item.imageInsets =
直接上代码: item.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);//设置图片居中 阅读全文 本文已收录于下面专栏: wanglixin1999 +关注 ...