Machete.Rpc

Machete.Rpc

Machete.Rpc 是一个轻量级的Rpc(远程过程调用的)框架。底层代理使用了Emit提高了效率,底层通信采用DotNetty框架以提升通信的效率。目前正在逐步完善中。

简单使用

目前还没有放到Nuget上,稍后放上去

1.新建一个类库Machete.Rpc.Sample.Service,新建一个接口IChatService

 [RpcService]
    public interface IChatService
    {
        string Hi(string name);

        string Hi(string name, string content);

        string Hello(int age);

        string Hello(double age);
    }

2.在新建一个类库Machete.Rpc.Sample.Implement,在类库里面新建一个实现IChatService的类

 [RpcService]
    public class ChatService : IChatService
    {
        public string Hi(string name)
        {
            return name + ":你好 世界";
        }

        public string Hi(string name, string content)
        {
            return name + ":" + content;
        }

        public string Hello(int age)
        {
            return "int:" + age;
        }

        public string Hello(double age)
        {
            return "double:" + age;
        }
    }

3.新建一个控制台程序 Machete.Rpc.Sample.Server(或者其他的程序),添加对Machete.Rpc.Sample.Service,Machete.Rpc.Sample.Implement的引用,在app.config 中增加如下配置,

 <appSettings>
    <add key="rpc.service" value="Machete.Rpc.Sample.Implement.dll"/>
    <add key="rpc.server.port" value="12900"/>
  </appSettings>

在Main方法中开启一个rpc服务

  int port = Convert.ToInt32(ConfigurationManager.AppSettings["rpc.server.port"].ToString());
            RpcHub hub = new RpcHub();
            hub.Start(port);

4.新建一个wpf(或其他的客户端程序)Machete.Rpc.Sample.Client,添加对Machete.Rpc.Sample.Service的引用,在app.config 文件中增加如下配置,

  <appSettings>
    <add key="rpc.server.host" value="127.0.0.1"/>
    <add key="rpc.server.port" value="12900"/>
  </appSettings>

进行rpc调用代码很简单,如下:

  private void Button_Click(object sender, RoutedEventArgs e)
        {
            IChatService chatService = InterfaceProxy.Resolve<IChatService>();
            string result = chatService.Hi("张三");
            MessageBox.Show(result);
        }

5.先启动Machete.Rpc.Sample.Server,在启动Machete.Rpc.Sample.Client。点击按钮(Say Hi),会进行rpc调用得到结果。

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • 转自:http://blog.csdn.net/kesonyk/article/details/50924489 ...
    晴天哥_王志阅读 24,932评论 2 38
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • 优化要点 “网络优化”关注WiFi整体覆盖方式的合理部署,并通过信号侧和 数据侧的优化方法最大限度削弱无线干扰带来...
    guru200阅读 2,360评论 0 3