Specific Rpc


# Specific Rpc
> * 使用avro-tools 对protocol文件编译生成java源码文件
> * 使用http 或者 netty 方式传输


协议文件为mail.avpr


```json
{"namespace": "avro.mail",
 "protocol": "Mail",


 "types": [
     {"name": "Message", "type": "record",
      "fields": [
          {"name": "to",   "type": "string"},
          {"name": "from", "type": "string"},
          {"name": "body", "type": "string"}
      ]
     },{
     "name":"RetMessage","type":"record",
        "fields":[
        {"name":"title","type": "string"},
        {"name":"content","type": "string"}
        ]
     }
 ],


 "messages": {
     "send": {
         "request": [{"name": "message", "type": "Message"}],
         "response": "string"
     },
     "send2": {
              "request": [{"name": "message", "type": "Message"}],
              "response": "RetMessage"
          }
 }
}
```


如果是协议,使用avro-tools 编译协议文件生成代码
`java -jar avro-tools-1.8.2.jar compile protocol  mail.avpr  destination-path`
生成的文件如下
* Mail.java
* Message.java
* RetMessage


Message 和 RetMessage 为定义的schema,类型为record
Mail为接口接口类的名称由`"protocol": "Mail"`决定


```java
public class Main {
    public static class MailImpl implements Mail {
        public Utf8 send(Message message) {
            return new Utf8("Sending message to " + message.getTo().toString()
                    + " from " + message.getFrom().toString()
                    + " with body " + message.getBody().toString());
        }
        public RetMessage send2(Message message) {
            return new RetMessage("hello","word");
        }
    }
    private static Server server;
    private static void startServer() throws IOException {
        //server = new NettyServer(new SpecificResponder(Mail.class, new MailImpl()), new InetSocketAddress(65111));
        server=new HttpServer(new SpecificResponder(Mail.class, new MailImpl()),65111);
        server.start();
    }


    public static void main(String[] args) throws IOException {
        startServer();
        System.out.println("Server started");


        //NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(65111));
        HttpTransceiver client=new HttpTransceiver(new URL("http://127.0.0.1:65111"));
        Mail proxy = SpecificRequestor.getClient(Mail.class, client);


        Message message = new Message();
        message.setTo(new Utf8("beijing"));
        message.setFrom(new Utf8("shanghai"));
        message.setBody(new Utf8("play"));


        System.out.println("Result: " + proxy.send(message));


        RetMessage retMessage=proxy.send2(message);
        System.out.println("Result: " + retMessage);


        client.close();
        server.close();
    }
}
```
执行结果如下
```
Server started
Result: Sending message to beijing from shanghai with body play
Result: {"title": "hello", "content": "word"}
Disconnected from the target VM, address: '127.0.0.1:52314', transport: 'socket'


Process finished with exit code 0
```


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值