Use rpc (facebook thrift) and the end of the communication tomcat

First, download the thrift-0.10.0.exe

  Download: https://www.apache.org/dist/thrift/

Second, write interfaces

  1. basic types

bool: Boolean type, accounting for a byte

byte: Signed Byte

i16: 16-bit signed integer

i32: 32-bit signed integer

i64: 64-bit signed integer

double: 64-bit floating point

string: String

2. (and almost in Java) container type

List <A>: A type of a series of elements of an ordered list of the element can be repeated

Set <B>: Some types of disordered t1 set of elements, the elements will not be repeated unique

Map <A, B>: pair of key-value pairs, key unique

3. structure

struct thrift used to declare a structure, which is equivalent to the object-oriented class definition language.

the User {struct   // declare a class (called class in java) 
    . 1 : String the userName,
     2 : String UserPwd, 
}

 

4. Abnormal

Abnormal use keyword exception to declare

5. Service

Keyword to declare the use of service

{LoginManger-Service 
    BOOL loginCheck ( . 1: the User User),   // where the class can be declared above using 
    BOOL Register (. 1 : the User User), 
}

6. Type Definition

typedef double mydouble
typedef i32 myi32

  7. namespace

  Using namespace declaration, produces a corresponding package in Java

  Write a login service, name the file Test.thrift

  

1 namespace java com.javabull.u.remote.conmunication.entities   // declare a package in java, of course, you can write according to your own wishes 
2  
3 struct the User {   // declare a class (called class in java) 
. 4      . 1 : String the userName,
 . 5      2 : String UserPwd,
 . 6  }
 . 7  
. 8  
. 9  -Service LoginManger {
 10      BOOL loginCheck (. 1: the User User),   // here above class declaration is used 
. 11      BOOL Register (. 1 : the User User ),
 12 }

   Use thrift-0.10.0.exe -gen java Test.thrift generate Java code

  

 

 

   

  Here, given the command line, is due to define the order parameter, the sequence can be a plus

 

 

   

  

  

 

  

  Finally, the generated Java code

 

 

      

 

  

 

 

  Third, to achieve server-side interface

  LoginManger.Iface class can achieve.
  
服务端使用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,大功告成

Guess you like

Origin www.cnblogs.com/javabull/p/12115630.html