Linux下使用thrift完成RPC服务

      本文主要讲解如何使用thrift完成RPC服务,Windows下和Linux下均已实现,有相关的Java代码。废话不多说,直接开始~


一、thrift的安装

参考官方文档:thrift在CentOS下的安装

安装到“Build and Install the Apache Thrift IDL Compiler”时,直接上传thrift的tar包,解压。此时先不要安装thrift。

安装thrift之前,先上传安装libevent(libevent官网:http://libevent.org/),然后到

/home/hadoop/thrift-0.11.0/lib/cpp/src/thrift/transport/THeaderTransport.h下修改:


找到“explicit THeaderTransport(..........)”这个地方,修改下面的“TVirtualTransport(transport)”,

改成:“: TVirtualTransport<THeaderTransport,TFramedTransport>(transport)


 

然后再修改“THeaderTransport(......)”下面的:“:TVirtualTransport(inTransport)”,

改成:“ :TVirtualTransport<THeaderTransport, TFramedTransport>(inTransport)


此处修改完成之后再进行thrift的安装。

查看thrift,如下所示说明安装成功



二、编写thrift文件




三、使用thrift -gen生成对应的Java代码




四、在本地编写代码,实现相应的业务逻辑

首先将在Linux下生成的HelloService.java文件导入到项目中(项目开发可以用IDEA、myeclipse、eclipse均可,这里使用myeclipse)

使用Maven配置,结构如下:


对应的pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.xzw</groupId>
  <artifactId>xzw</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>xzw</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.11.0</version>
    </dependency>
  </dependencies>
</project>

实现业务逻辑接口的代码:

package com.xzw.xzw;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.thrift.TException;

/**
 * 
 * @author xzw
 *
 */
public class HelloServiceImpl implements HelloService.Iface{

	public String hello(String name) throws TException {
		// TODO Auto-generated method stub
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(df.format(date) + ":Hello, " + name);
		return name;
	}
}

服务器端代码:

package com.xzw.xzw;

import org.apache.thrift.protocol.TBinaryProtocol.Factory;
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 com.xzw.wjl.HelloService.Iface;
import com.xzw.wjl.HelloService.Processor;

/**
 * 
 * @author xzw
 *
 */
public class HelloServer {
    public static void main(String[] args) {
        try {
			//设置服务器的端口
			TServerSocket ts = new TServerSocket(9090);
			//设置二进制协议工厂
			Factory protocolFactory = new Factory();
			//处理器关联业务实现
			Processor<HelloService.Iface> processor = new HelloService.Processor<Iface>(new HelloServiceImpl());
			//单线程标准阻塞I/O模型
			TServer.Args simpleArgs = new TServer.Args(ts);
            simpleArgs.processor(processor);
            simpleArgs.protocolFactory(protocolFactory);
            TServer server = new TSimpleServer(simpleArgs);
            System.out.println("Thrift服务开启");
            server.serve();
		} catch (TTransportException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

客户端代码:

package com.xzw.xzw;

import com.xzw.wjl.HelloService.*;
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 java.util.Scanner;
/**
 * 
 * @author xzw
 *
 */
public class HelloClient {
    public static void main(String[] args){
        //设置调用的服务地址-端口
        TTransport transport = new TSocket("localhost", 9090);
        //使用二进制协议
        TProtocol protocol = new TBinaryProtocol(transport);
        //调用thrift生成的接口
        Client client = new Client(protocol);
        //打开socket
        try {
            transport.open();
            while(true){
                System.out.println("input your name :");
                Scanner scan = new Scanner(System.in);
                String name = scan.nextLine();
                client.hello(name);
            }
        } catch (TTransportException e){
            e.printStackTrace();
        } catch (TException e){
            e.printStackTrace();
        }
        transport.close();
    }
}


五、Windows下测试结果



六、将项目打成jar包提交到Linux下进行测试




至此,Linux下使用thrift进行RPC服务就实现了。


你们在此过程中还遇到了什么问题,欢迎留言,让我看看你们都遇到了哪些问题。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

象在舞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值