thrift 学习入门笔记

参考连接 :Thrift入门

安装thrift命令:

brew install thrift

引入maven依赖

        <!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift -->
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libthrift</artifactId>
            <version>0.16.0</version>
        </dependency>

编写thrift文件

namespace java thrift.generated

typedef i16 short
typedef i32 int
typedef i64 long
typedef string String

struct Person{
    1:optional String username,
    2:optional int age,
    3:optional bool married
}

exception DataException{
    1:optional String message,
    2:optional String callStack,
    3:optional String code
}

service PersonService{
    Person getPersonByName(1:required String name) throws (1:DataException e);

    void savePerson(1:required Person p) throws(1:DataException e);
}

生成代码

thrift --gen java ./resources/data.thrift 

<!--然后会在项目中生成的 gen-java 文件夹下生成java文件 -->

├── gen-java
│   └── thrift
│       └── generated
│           ├── DataException.java
│           ├── Person.java
│           └── PersonService.java

新建 PersonServiceImpl 实现 PersonService 接口。


public class PersonServiceImpl implements PersonService.Iface {
    @Override
    public Person getPersonByName(String name) throws DataException, TException {

        System.out.println("----->> getPersonByName <<-------");

        Person p1 = new Person();
        p1.setAge(23);
        p1.setMarried(Boolean.TRUE);
        p1.setUsername("zhang san");
        return p1;
    }

    @Override
    public void savePerson(Person p) throws DataException, TException {

        System.out.println("---- savePerson -----");

        System.out.println(p.getAge());
        System.out.println(p.getUsername());
        System.out.println(p.isMarried());
    }
}

编写服务端


import com.demo.thrift.PersonService;
import com.demo.thrift.PersonServiceImpl;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.layered.TFramedTransport;

public class MyServer {

    public static void main(String[] args) throws Exception{

        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);

        THsHaServer.Args args1 = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);

        PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl());

        args1.protocolFactory(new TCompactProtocol.Factory());
        args1.transportFactory(new TFramedTransport.Factory());
        args1.processorFactory(new TProcessorFactory(processor));

        THsHaServer tHsHaServer = new THsHaServer(args1);
        
        System.out.println("服务端已启动  :8899");
        tHsHaServer.serve();

    }
}

编写客户端


import com.demo.thrift.Person;
import com.demo.thrift.PersonService;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;

public class MyClient {

    public static void main(String[] args) throws TTransportException {

        TTransport transport = new TFramedTransport(new TSocket("localhost", 8899), 600);

        TProtocol protocol = new TCompactProtocol(transport);

        PersonService.Client client = new PersonService.Client(protocol);

        try {

            transport.open();
            Person zhang_san = client.getPersonByName("zhang san");
            System.out.println(zhang_san.getAge());
            System.out.println(zhang_san.getUsername());
            System.out.println(zhang_san.isMarried());

            Person p1 = new Person();
            p1.setAge(21);
            p1.setMarried(Boolean.FALSE);
            p1.setUsername("lisi");

            client.savePerson(p1);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            transport.close();
        }
    }
}

over


岁月峥嵘走过
34 声望2 粉丝