1. 程式人生 > >微服務學習筆記(1)——使用MagicOnion實現gRPC

微服務學習筆記(1)——使用MagicOnion實現gRPC

tco cred time sharp ref 服務端 eve rpo 實現接口

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