Avro也是典型CS架构,与ICE、CORBA、Thrift相同,但大家一般不太用其做RPC框架,而多是使用其强大的序列化功能。即使如此,我们也可以将Avro用于RPC通讯,也只需要告诉Avro服务在哪里,需要哪个服务,调用参数是什么,然后就坐等处理结果就好咯。
使用Avro的时候,首先要先下载Avro的开发包,每种序言需要单独下载,都自带编译工具及语言支持包,本例主要用到C#和Java。
Avro
使用新版本Avro的时候,一般显示用IDL语言,定义一个接口描述文件,比如我自己写了一个很简单的接口。
JustATest.avdl
@namespace("com.neohope.avro.test")
protocol JustATest{
record Person {
string name;
int age;
string sex;
string address;
}
string SayHelloTo(Person person);
int Add(int a,int b);
}
然后用Avro自带工具,将其翻译为protocol文件
JustATest.avpr
set AVRO_HOME=D:\Build\Avro\avro-java-1.8.0 java -jar %AVRO_HOME%\avro-tools-1.8.0.jar idl JustATest.avdl JustATest.avpr
翻译得到的JustATest.avpr文件如下:
{
"protocol" : "JustATest",
"namespace" : "com.neohope.avro.test",
"types" : [ {
"type" : "record",
"name" : "Person",
"fields" : [ {
"name" : "name",
"type" : "string"
}, {
"name" : "age",
"type" : "int"
}, {
"name" : "sex",
"type" : "string"
}, {
"name" : "address",
"type" : "string"
} ]
} ],
"messages" : {
"SayHelloTo" : {
"request" : [ {
"name" : "person",
"type" : "Person"
} ],
"response" : "string"
},
"Add" : {
"request" : [ {
"name" : "a",
"type" : "int"
}, {
"name" : "b",
"type" : "int"
} ],
"response" : "int"
}
}
}
然后,用工具分别编译为java及c#语言
#java set AVRO_HOME=D:\Build\Avro\avro-java-1.8.0 java -jar %AVRO_HOME%\avro-tools-1.8.0.jar compile protocol JustATest.avpr avpr #c# avrogen -p JustATest.avpr avpr-cs