使用rpc(facebook thrift)与tomcat 端通信

一、下载 thrift-0.10.0.exe

  下载地址:https://www.apache.org/dist/thrift/

二、编写接口

  1.基本类型

bool: 布尔类型,占一个字节

byte: 有符号字节

i16:16位有符号整型

i32:32位有符号整型

i64:64位有符号整型

double:64位浮点数

string:字符串

2.容器类型(和Java中的差不多)

List<A>:一系列A类型的元素组成的有序列表,元素可以重复

Set<B>:一些t1类型的元素组成的无序集合,元素唯一不重复

Map<A,B>:键值对对,key唯一

3.结构体

thrift中使用struct 来声明一个结构体,这等同与面向对象语言中的类定义。

struct User{  //声明一个类(在java中称为类)
    1:string userName,
    2:string userPwd,
}

4.异常

异常使用关键字exception来声明

5.服务

使用service关键字来声明

service LoginManger{
    bool loginCheck(1:User user),  //这里可以对上面声明的类进行使用了
    bool register(1:User user),
}

6.类型定义

typedef double mydouble
typedef i32 myi32

  7.名字空间

  使用namespace声明,在Java中产生相应的包

  编写一个login  service ,将文件命名为Test.thrift

  

 1 namespace java com.javabull.u.remote.conmunication.entities  //声明在java下的包,当然你可以按照你自己的意愿写
 2 
 3 struct User{  //声明一个类(在java中称为类)
 4     1:string userName,
 5     2:string userPwd,
 6 }
 7 
 8 
 9 service LoginManger{
10     bool loginCheck(1:User user),  //这里可以对上面声明的类进行使用了
11     bool register(1:User user),
12 }

   使用thrift-0.10.0.exe -gen java Test.thrift生成Java代码

  

   

  在这里,命令行报错,是由于参数要定义顺序,所以加上顺序就可以了

   

  

  

  

  最后生成的Java代码

      

  

  三、服务器端的接口的实现

  类实现LoginManger.Iface即可。
  
服务端使用maven引入thrift
<dependencies>
<!--        引入thrift依赖-->
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

    </dependencies>
  
  四、服务器端的使用
  编写Servlet,继承自TServlet
 1 import org.apache.thrift.protocol.TBinaryProtocol;
 2 import org.apache.thrift.server.TServlet;
 3 
 4 import com.javabull.u.remote.conmunication.LoginManager;
 5 import com.javabull.u.remote.conmunication.LoginManagerImpl;
 6 
 7 public class RemoteServlet extends TServlet{
 8 
 9     private static final long serialVersionUID = 1L;
10 
11     public RemoteServlet() {//LoginManagerImpl是对LoginManger.Iface的实现
12 super(new LoginManager.Processor<>(new LoginManagerImpl()), new TBinaryProtocol.Factory());  }  }

  

  五、Android端的引用

  

 //引入thrift依赖
    implementation 'org.apache.thrift:libthrift:0.12.0'
    implementation 'org.glassfish.main:javax.annotation:4.0-b33'

  发现报错,在build.gradle加入packagingOptions即可

 

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.javabull.android_thrift"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

//加入packagingOptions即可
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

}

   

  

1         THttpClient httpClient = new THttpClient("http://localhost:8080/server.do"); //访问Tomcat中Servlet
2         TProtocol protocol = new TBinaryProtocol(httpClient);
3         LoginManager.Client client = new InterManager.Client(protocol);
4           //之后可以通过client调用之前编写好的接口

  ok,大功告成

猜你喜欢

转载自www.cnblogs.com/javabull/p/12115630.html