Java通过Thrift调用php

Thrift的跨语言特性

Thrift通过一个中间语言IDL(接口定义语言)来定义RPC的数据类型和接口,这些内容写在以.thrift结尾的文件中,然后通过特殊的编译器来生成不同语言的代码,以满足不同需要的开发者,生成的代码中不但包含目标语言的接口定义、方法、数据类型,还包含有RPC协议层和传输层的实现代码。

Java通过Thrift调用php

创建.thrift后缀的文件HelloThrift.thrift,在这里是用IDL定义服务的接口。

namespace java Services
namespace php Services
service HelloThrift{
  string hello(1:string name)
}

安装Thrift, 终端进入HelloThrift.thrift所在目录,执行命令

thrift –r –gen java HelloThrift.thrift

发现在当前目录下多了一个gen-java的目录,里面有一个HelloThrift.java文件,然后把这个文件放到项目里。

导入libthrift依赖

<dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.10.0</version>
    </dependency>

创建HelloServiceImpl实现HelloThrift.Iface接口

package Services;

import org.apache.thrift.TException;


public class HelloServiceImpl implements HelloThrift.Iface {
    @Override
    public String hello(String name) throws TException {
        System.out.println("hello");
        return "hello,"+name;
    }
}

编写服务端代码

package com.wlj.thrift_server;

import Services.HelloServiceImpl;
import Services.HelloThrift;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThriftServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThriftServerApplication.class, args);
        try {
            System.out.println("server start....");
            TProcessor tprocessor = new HelloThrift.Processor<HelloThrift.Iface>(new HelloServiceImpl());


            TServerSocket serverTransport = new TServerSocket(9898);
            TServer.Args tArgs = new TServer.Args(serverTransport);

            tArgs.processor(tprocessor);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            TServer server = new TSimpleServer(tArgs);
            server.serve();
        }catch (TTransportException e) {
            e.printStackTrace();
        }
    }
}


编写客户端代码

package com.wlj.thrift_client;

import Services.HelloWorld;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
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.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ThriftClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ThriftClientApplication.class, args);
        System.out.println("start client...");
        TTransport transport = null;
        try {
            //111.231.98.150
            transport = new TSocket("111.231.98.150", 9090);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);

            //     TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol,"HelloWorld");
            HelloWorld.Client helloClient = new HelloWorld.Client(protocol);

            transport.open();

            String result = helloClient.sayHello("Tom");
            System.out.println(result);
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }
}

坚持原创技术分享,您的支持将鼓励我继续创作!