• CentOS安装thrift


    1.安装jdk

    2.安装ant

    3.安装  ivy


    a、下载apache ivy:http://labs.renren.com/apache-mirror//ant/ivy/2.2.0/apache-ivy-2.2.0-bin.tar.gz
    b、tar xzvf apache-ivy-2.2.0-bin.tar.gz
    c、cp ivy-2.2.0.jar to ANT_HOME/lib

    d、goto apache-ivy-2.2.0/src/example/hello-ivy, and run ant,也就是在目录apache-ivy-2.2.0/src/example/hello-ivy,运行ant

    如果看到: 

    BUILD SUCCESSFUL

    Total time: 29 seconds

    就代表成了 

    在centos里再执行这一句应没问题:sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel 

     哈哈

    4. 安装thrift

    a>下载thrift:http://mirror.bjtu.edu.cn/apache//thrift/0.8.0/thrift-0.8.0.tar.gz

    b>tar -zxvf thrift-0.8.0.tar.gz

    c>cd  thrift-0.8.0

    d> ./configure --with-boost=/usr/local

    e>make 

    f>make install 

    g>在命今行内输入 thrift,如果有提示,应就可以了

    如果遇到"Error: libcrypto required" 错误,请安装libssl-dev

    5.构建thrift java 服务器端和客户端

     a>新建testJava.thrift文件:

    namespace java Test
    service Something{
        i32 ping()

      

    b>testJava.thrift目录执行如下命令:thrift –gen java testJava.thrift ,生成的源文件在./gen-java/目录下

    c>进入gen-java目录

    d>编写SomethingImpl.java

    package Test;

    import org.apache.thrift.TException;

    class SomethingImpl implements Something.Iface {
        public SomethingImpl() {
        }

        public int ping() throws TException {
            System.out.println("Recieve ping from client...");
            return 0;
        }

    e> Server.java

    package Test;

    import java.io.IOException;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.protocol.TBinaryProtocol.Factory;
    import org.apache.thrift.server.TServer;
    import org.apache.thrift.server.TSimpleServer;
    import org.apache.thrift.server.TThreadPoolServer;
    import org.apache.thrift.transport.TServerSocket;
    import org.apache.thrift.transport.TTransportException;

    public class Server {
        private void start() {
            try {
                TServerSocket serverTransport = new TServerSocket(7911);
                Something.Processor processor = new Something.Processor(new SomethingImpl());
                Factory protFactory = new TBinaryProtocol.Factory(truetrue);
                //TServer server = new TThreadPoolServer(processor, serverTransport,protFactory);
                
    //TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
                TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
                System.out.println("Starting server on port 7911 ...");
                server.serve();

            } catch (TTransportException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static void main(String args[]) {
            Server srv = new Server();
            srv.start();
        }

    f> Client.java

    package Test;

    import java.io.IOException;
    import org.apache.thrift.*;
    import org.apache.thrift.protocol.*;
    import org.apache.thrift.transport.*;

    public class Client {
          public static void main(String [] args) {
               try {
                        TTransport transport = new TSocket("localhost", 7911);
                        TProtocol protocol = new TBinaryProtocol(transport);
                        Something.Client client = new Something.Client(protocol);
                        transport.open();
                        System.out.println("Client calls ping()");
                        client.ping();
                        transport.close();
                   } catch (TException x) {
                        x.printStackTrace();
                   }   
            }   

    }  

    g>要求如下几个包支持libthrift-0.8.0.jar  log4j-1.2.14.jar  slf4j-api-1.5.8.jar  slf4j-log4j12-1.5.8.jar,所以可以如下命令编译:

     javac -classpath ./:../lib/libthrift-0.8.0.jar:../lib/log4j-1.2.14.jar:../lib/slf4j-api-1.5.8.jar:../lib/slf4j-log4j12-1.5.8.jar *.java

    h> 启动服务器。退到gen-java目录,输入java  -classpath ./:../lib/libthrift-0.8.0.jar:../lib/log4j-1.2.14.jar:../lib/slf4j-api-1.5.8.jar:../lib/slf4j-log4j12-1.5.8.jar Test/Server,屏幕显示如下:

    Starting server on port 7911 ...

    i> 启动客户端。在同一目录下输入java  -classpath ./:../lib/libthrift-0.8.0.jar:../lib/log4j-1.2.14.jar:../lib/slf4j-api-1.5.8.jar:../lib/slf4j-log4j12-1.5.8.jar  Test/Client,屏幕显示如下:

    Client calls ping()

    这时服务器端的输出多了一行:

    Recieve ping from client...

    成功了!!!!!!!!!!!!!!!!!!!!!!!! 

    问题:

    1.

    error: command 'gcc' failed with exit status 1

    install python-dev
     

  • 相关阅读:
    RecycleView的万能适配器
    Android Fragment
    BottomNavigationBar底部导航条用法
    Bundle的用法
    登录页面(动态地与数据库匹配用户信息)
    LitePal用法详解
    BaseAdapter的优化
    Bmob使用心得
    字符串格式化
    元素NULL判断
  • 原文地址:https://www.cnblogs.com/cerxp/p/2345535.html
Copyright © 2020-2023  润新知