单位推荐意见,化兽人,儒道仙缘
官方文档:
magiconion开源地址:https://github.com/cysharp/magiconion
新建一个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(); } } }
新建一个控制台程序
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); } } }
grpc项目创建多个之后,需要一个服务注册和发现的工具。
使用consul 实现 magiconion(grpc) 服务注册与发现
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论