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) 服务注册与发现

使用MagicOnion实现gRPC的更多相关文章

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

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

  2. 一系列令人敬畏的.NET核心库,工具,框架和软件

    内容 一般 框架,库和工具 API 应用框架 应用模板 身份验证和授权 Blockchain 博特 构建自动化 捆绑和缩小 高速缓存 CMS 代码分析和指标 压缩 编译器,管道工和语言 加密 数据库 ...

  3. .NET Core 学习资料精选:入门

    开源跨平台的.NET Core,还没上车的赶紧的,来不及解释了-- 本系列文章,主要分享一些.NET Core比较优秀的社区资料和微软官方资料.我进行了知识点归类,让大家可以更清晰的学习.NET Co ...

  4. 入门干货之Grpc的.Net实现-MagicOnion

    此文章简单残暴,学习成本较低,你可以跟着我一起撸代码,一起吐槽,一起砸键盘.以下操作均为 core2.0 环境. 0x01.Grpc 1.介绍  Google主导开发的RPC框架,使用HTTP/2协议 ...

  5. 使用Consul 实现 MagicOnion(GRpc) 服务注册和发现

    1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载windows的Consul https://www.consul.io/ 使用cmd窗口打开,输入con ...

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

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

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

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

  8. Abp + gRpc 如何实现用户会话状态传递

    0.背景 在实际项目当中,我们采用的是 Abp 框架,但是 Abp 框架官方并没有针对 Grpc 进行模块封装.基于此我结合 Abp 与 MagicOnion 封装了一个 Abp.Grpc 模块,它包 ...

  9. gRPC源码分析1-SSL/TLS

    引子 前几天看到微信后台团队分享了TLS相关文章,正好gRPC里TLS数据加密是很重要的一块,于是整理出了这篇文章. 在gRPC里,如果仅仅是用来做后端微服务,可以考虑不加密.本文太长,先给个大纲. ...

随机推荐

  1. 面试简单整理之rabbitmq

    135.rabbitmq 的使用场景有哪些? 单反单收,单发多收,发布订阅,按路由发送,按主题发送 136.rabbitmq 有哪些重要的角色? Server,Consumer,Producer 13 ...

  2. 201621123002《Java程序设计》第八周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 从源代码中可以看出 ...

  3. 解决删除镜像时image is referenced in multiple repositories

    1.查看镜像 docker images rt@:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hours ago MB f8ab12e0 ...

  4. sqlserv 配置 CLR

    转载地址:http://www.cnblogs.com/Brambling/p/8000911.html   //clr 配置 https://docs.microsoft.com/zh-cn/sql ...

  5. 能量模型与softmax和RBM以及正态分布的关联

    上面一篇文章中探讨了玻尔兹曼分布的起源: 在不清楚目标的真实分布,也不知道样本分布的时候,假设任意输入与输出组合都是同样可能发生的,这样是最公平,最无偏的先验. 因为无法直接统计出给定任意一种输入x, ...

  6. 用pandas库修改excel文件里的内容,并把excel文件格式存为csv格式,再将csv格式改为html格式

    假设有Excel文件data.xlsx,其中内容为: ID  age  height     sex  weight张三   1   39     181  female      85李四   2  ...

  7. C++智能指针 auto_ptr、shared_ptr、weak_ptr和unique_ptr

    手写代码是理解C++的最好办法,以几个例子说明C++四个智能指针的用法,转载请注明出处. 一.auto_ptr auto_ptr这是C++98标准下的智能指针,现在常常已经被C++标准的其他智能指针取 ...

  8. WebApi零碎总结

    1.如果Content-Type是application/json,而POST和PUT的参数是[FromBody] string value,那么如果curl -d的值是'{"Name&qu ...

  9. JavaScript基础视频教程总结(081-090章)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. Unity3D编辑器扩展(三)——使用GUI绘制窗口

    前两篇分别讲解了创建菜单https://www.cnblogs.com/xiaoyulong/p/10115053.html和创建窗口https://www.cnblogs.com/xiaoyulon ...