微服务学习笔记(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 ...
随机推荐
- solr索引创建流程
solr索引创建流程: 分词组件Tokenizer 分词组件(Tokenizer)会做以下几件事情(这个过程称为:Tokenize),处理得到的结果是词汇单元(Token). 1.将文档分成一个一个单 ...
- uiautomator_python使用汇总
1.判断按钮状态 if d(resourceId="id",enabled=False): #判断当前按钮是否为未激活状态,为True则为激活状态2.获取toast提示语 d.to ...
- C# 映射
public class Myclass1 { private int m_Count = 100; public string love{get;set;} public int Count { g ...
- leetcode笔记:Word Break
一. 题目描写叙述 Given a string s and a dictionary of words dict, determine if s can be segmented into a sp ...
- js进阶 14-1 jquery的ajax系列中的load方法的作用是什么
js进阶 14-1 jquery的ajax系列中的load方法的作用是什么 一.总结 一句话总结:jQuery load()方法作用是从服务器加载数据,是一个简单但强大的AJAX方法. 1.load函 ...
- JavaScript字符串替换replace方法
在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...
- 发布一个stl标准库容器类(vector/list)的安全删除方法
话不多说,看代码. #include <functional> #ifndef ASSERT #include <cassert> #define ASSERT assert ...
- flask的使用(一)
1.程序基本的说明 #-*-encoding=utf--*- 从flask中引入类 from flask import Flask ,render_template import config 初始化 ...
- HDU 1212 Big Number 大数模小数
http://acm.hdu.edu.cn/showproblem.php?pid=1212 题目大意: 给你一个长度不超过1000的大数A,还有一个不超过100000的B,让你快速求A % B. 什 ...
- smack capable(CAP_MAC_OVERRIDE)
https://blog.csdn.net/ning_wei/article/details/9670947 LINUX中的capable int smk_curacc(char *obj_label ...