欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-28 13:54:04
1.什么是gRPC 官方文档:https://grpc.io/docs/guides/index.html 2.什么是MagicOnion MagicOnion开源地址:https://github.com/Cysharp/MagicOnion 3.服务端代码 新建一个WebAPI项目 4.客户端 ......
1.什么是grpc

官方文档:

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