基于上一篇博客,安装thrift complier之后,就需要进行跑跑程序,来看看是否如同预期的那种效果。

 

前面的thrift compiler的主要作用,其实就是为了IDL的,就是防止客户端和服务端的接口定义不同,基于IDL操作,最大限度的满足高效准确的实现服务的定义和实现。

 

1. 首先定义.thrift扩展名的文件,有tutorial.thrift和shared.thrift,其内容如下:

shared.thrift

 1 /*
 2  * Licensed to the Apache Software Foundation (ASF) under one
 3  * or more contributor license agreements. See the NOTICE file
 4  * distributed with this work for additional information
 5  * regarding copyright ownership. The ASF licenses this file
 6  * to you under the Apache License, Version 2.0 (the
 7  * "License"); you may not use this file except in compliance
 8  * with the License. You may obtain a copy of the License at
 9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /**
21  * This Thrift file can be included by other Thrift files that want to share
22  * these definitions.
23  */
24 
25 namespace cpp shared
26 namespace d share // "shared" would collide with the eponymous D keyword.
27 namespace dart shared
28 namespace java shared
29 namespace perl shared
30 namespace php shared
31 namespace haxe shared
32 
33 struct SharedStruct {
34   1: i32 key
35   2: string value
36 }
37 
38 service SharedService {
39   SharedStruct getStruct(1: i32 key)
40 }
View Code

相关文章: