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. redis缓存雪崩,缓存穿透,缓存击穿的解决方法

    一.缓存雪崩 缓存雪崩表示在某一时间段,缓存集中失效,导致请求全部走数据库,有可能搞垮数据库,使整个服务瘫痪. 使缓存集中失效的原因: 1.redis服务器挂掉了. 2.对缓存数据设置了相同的过期时间 ...

  2. python 聊天程序(基于UDP)

    from threading import Thread from socket import * updSocket = socket(AF_INET,SOCK_DGRAM) updSocket.b ...

  3. 7I - 数塔

    在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题目 ...

  4. Git merge 不同的branch

    Git的优势是可以创建不同的branch,然后在每个branch上开发.那么问题是:如果不同的branch之间需要做同步,比如sourceBranch上做的修改也需要同步到targetBranch,改 ...

  5. sqlite基本用法

    DDL-数据定义语言 CREATE 创建一个新的表,一个表的视图,或者数据库中的其他对象. ALTER 修改数据库中的某个已有的数据库对象,比如一个表. DROP 删除整个表,或者表的视图,或者数据库 ...

  6. 知识阅读的好处你都了解吗?芒果xo来告诉你答案

    阅读www.mangoxo.com让人才思敏捷,杜甫曾说:读书破万卷,下笔如有神:阅读让人心情愉悦,蒙台居曾说过:再没有比读书更廉价的娱乐,更持久的满足了:阅读让人思维灵活,狄德罗曾说过:不读书的人, ...

  7. 将bean转换成XML字符串

    package com.sinoservices.bms.bbl.rest.bean; import javax.xml.bind.annotation.XmlAccessType; import j ...

  8. Python3创建项目时创建了一个叫做“keyword"的包,运行项目时报ImportError: cannot import name 'iskeyword'错误

    导致该问题的原因为在Python3中keyword是python的关键字包,所以在给包命名时应避免使用关键字进行命名.解决方法,将keword包名称修改为'keywords'就可以了.

  9. Context 解析

    ·  ContextWrapper比较有意思,其在SDK中的说明为“Proxying implementation ofContext that simply delegates all of its ...

  10. PackageManagerService 学习记录 基于7.1.1源码

    参考: http://blog.csdn.net/innost/article/details/47253179 http://blog.csdn.net/gaugamela/article/deta ...