Facebook远程调用框架thrift入门示例

和谷歌的gRPC类似,Facebook的thrift也是个优秀的远程调用框架,来入个门。

安装thrift

mac

brew install thrift

安装完成检查

thrift --version

新建maven项目

pom.xml

    <dependencies>	
        <dependency>	
            <groupId>org.apache.thrift</groupId>	
            <artifactId>libthrift</artifactId>	
            <version>0.11.0</version>	
        </dependency>	
    </dependencies>	
    <build>	
        <plugins>	
            <plugin>	
                <groupId>org.apache.thrift</groupId>	
                <artifactId>thrift-maven-plugin</artifactId>	
                <version>0.10.0</version>	
                <configuration>	
                    <thriftExecutable>/usr/local/bin/thrift</thriftExecutable> <!--thrift安装路径-->	
                    <thriftSourceRoot>src/main/resources</thriftSourceRoot> <!--thrift配置文件路径-->	
                </configuration>	
                <executions>	
                    <execution>	
                        <id>thrift-sources</id>	
                        <phase>generate-sources</phase>	
                        <goals>	
                            <goal>compile</goal>	
                        </goals>	
                    </execution>	
                </executions>	
            </plugin>	
        </plugins>	
    </build>

定义服务

新建文件 src/main/resources/service.thrift

namespace java com.acupt.thritf.service	
service HelloService{	
    string hello(1:string name)	
}

构建

使用maven插件根据.proto文件生成Java代码,插件已在pom.xml中配置,只需执行命令:

mvn install

构建完成后可以在target中找到生成的Java代码,用这些代码可以实现thrift远程调用。

target/generated-sources/thrift/com/acupt/thritf/service/HelloService.java

如果在项目中无法直接引用上面的类,IDEA右键thrift文件夹 -> Mark Directory as -> Generated Sources Root

现在就可以在项目中引用了

代码

3个类

package com.acupt.thrift;	
import com.acupt.thritf.service.HelloService;	
import org.apache.thrift.TException;	
/**	
 * 服务实现类	
 */	
public class HelloServiceImpl implements HelloService.Iface {	
    @Override	
    public String hello(String name) throws TException {	
        return "hello," + name;	
    }	
}
package com.acupt.thrift;	
import com.acupt.thritf.service.HelloService;	
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;	
/**	
 * 服务提供方	
 */	
public class MyServer {	
    public static void main(String args[]) {	
        try {	
            TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl());	
            TServerSocket serverTransport = new TServerSocket(50005);	
            TServer.Args tArgs = new TServer.Args(serverTransport);	
            tArgs.processor(tprocessor);	
            tArgs.protocolFactory(new TBinaryProtocol.Factory());	
            TServer server = new TSimpleServer(tArgs);	
            System.out.println("server starting");	
            //定时关闭	
            new Thread(() -> {	
                try {	
                    System.out.println("server wait stop");	
                    Thread.sleep(30000);	
                } catch (InterruptedException e) {	
                    e.printStackTrace();	
                }	
                System.out.println("server stopping");	
                server.stop();	
                System.out.println("server stop");	
            }).start();	
            server.serve();//会阻塞	
            System.out.println("server finish");	
        } catch (Exception e) {	
            e.printStackTrace();	
        }	
    }	
}
package com.acupt.thrift;	
import com.acupt.thritf.service.HelloService;	
import org.apache.thrift.protocol.TBinaryProtocol;	
import org.apache.thrift.protocol.TProtocol;	
import org.apache.thrift.transport.TSocket;	
import org.apache.thrift.transport.TTransport;	
/**	
 * 服务调用方	
 */	
public class MyClient {	
    public static void main(String[] args) {	
        TTransport transport = null;	
        try {	
            transport = new TSocket("localhost", 50005);	
            TProtocol protocol = new TBinaryProtocol(transport);	
            HelloService.Client client = new HelloService.Client(protocol);	
            transport.open();	
            String result = client.hello("tom");	
            System.out.println(result);	
        } catch (Exception e) {	
            e.printStackTrace();	
        } finally {	
            if (null != transport) {	
                transport.close();	
            }	
        }	
    }	
}

先启动MyServer,成功启动后再启动MyClient。

和grpc用法差不多,gRPC-Java 示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值