RPC is a remote procedure call, suitable for cluster management, the cluster node is rpcserver, and the Web server we initiated the remote call is rpcclient. Therefore is a few rpcclient (possibly one) to multiple rpcserver (cluster nodes).
The RPC development that is being described today wants to achieve the effect of executing a shell command on rpcclient (that is, the Web server) that requires the specified remote host to execute the specified command . The format of the command is as follows
Rpc_client Address Command
Like what
192.168. 1.1 Vmstat
The result of the remote execution of this command (on Rpcserver) is expected to be directly output to the Web server (rpcclient). you can see an execution effect first.
So to do this, you need to use the RPC framework, where you choose Apache Thrift, and the C + + language, because there is no guarantee that the node is loaded with PHP or the Java environment, I want to finally get an environment-independent executable file.
Here you can see the official Apache C + + tutorials First
Https://thrift.apache.org/tutorial/cpp
Start implementing RPC below.
Install thrift environment Download related files
Yum-Y Groupinstall"Development Tools"wgethttp//ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gzwgethttp//ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gzwgethttp//ftp.gnu.org/gnu/automake/automake-1.14.tar.gzwgethttp//www.mirrorservice.org/sites/dl.sourceforge.net/pub/sourceforge/b/bo/boost/boost/1.55.0/boost_1_55_0.tar.gz git clone https://Git-wip-us.apache.org/repos/asf/thrift.git
Install in sequence
TarZXF autoconf-2.69.Tar. GZCD autoconf-2.69./configure--prefix=/usr Makesudo Make InstallCD..TarXVF bison-2.5.1.Tar. GZCD Bison-2.5.1./configure--prefix=/usr Make Make InstallCD..TarZXF automake-1.14.Tar. GZCD Automake-1.14./configure--prefix=/usr Make Make InstallCD..TarZXF Boost_1_55_0.Tar. gz./bootstrap.SH./Configure Makesudo Make InstallCD../Thrift./bootstrap.SHYum InstallOpenSSL Openssl-devel-y./Configure Make Make Install
Here in install thrift is Yum installed Openssl-dev, is to solve this error
After the installation is complete
Writing Thrift Files
Writing Ssan.thrift files
CPP ssanservice ssanagent { string Run (1:string command)}
Using Thrift to generate
CPP Ssan.thrift
Writing Rpcserver
Thrift has generated a template for the server for US
CP Ssanagent_server.skeleton. CPP Ssanserver. CPP
Previously defined interface methods are also primarily defined in this class
Modify the SSANServer.cpp file
//This autogenerated skeleton file illustrates how to build a server.//You should copy it to another filename to avoid overwriting it.#include"SSANAgent.h"#include<sstream>#include<thrift/protocol/TBinaryProtocol.h>#include<thrift/server/TSimpleServer.h>#include<thrift/transport/TServerSocket.h>#include<thrift/transport/TBufferTransports.h>using namespace:: Apache::thrift;using namespace:: Apache::thrift::p rotocol;using namespace:: Apache::thrift:: Transport;using namespace:: Apache::thrift::server;using boost::shared_ptr;using Namespace:: Ssan;using namespace Std ; class Ssanagenthandler:virtual public Ssanagentif {Public:ssanagenthandler () {//Your initialization goes here} void Run (std::string& _return, const std::string&command) {Std::ostringstream oss; FILE* pp = Popen (Command.c_str (),"R"); if(PP) {Charbuf[4096]; while(Fgets (buf,sizeof (BUF), pp)) {OSS<<buf; } pclose (PP); } Else{OSS<<"error:no such command:"<<command; } _return=Oss.str (); }};intMainintargcChar**argv) { intPort =9090; shared_ptr<SSANAgentHandler>Handler (new Ssanagenthandler ()); shared_ptr<TProcessor>Processor (new Ssanagentprocessor (handler)); shared_ptr<TServerTransport>Servertransport (New Tserversocket (port)); shared_ptr<TTransportFactory>transportfactory (New Tbufferedtransportfactory ()); shared_ptr<TProtocolFactory>protocolfactory (New Tbinaryprotocolfactory ()); Tsimpleserver Server (processor, Servertransport, Transportfactory, protocolfactory); cout<<"Start Ssan Server ..."<<Endl; Server.serve (); cout<<" Done"<<Endl; Return0;}
Mainly in the Run method to handle the business
Generate Rpcserver
Do not use makefile first, then execute the following command to compile
g++-wall-i/usr/local/include/thrift-c ssanagent. CPP g+ +-wall-i/usr/local/include/thrift-c ssanserver. CPP g+ +-wall-i/usr/local/include/thrift-c ssan_constants. CPP g+ +-wall-i/usr/local/include/thrift-c ssan_types. CPP
Link file generation Rpcserver, note this is a dynamic link
g++-l/usr/local/lib *.o-o Ssan_server–lthrift
This error occurs after the link is run
This file already exists, but in the directory/usr/local/lib/, while the program runs and then/usr/lib under the search for dynamic library files, so create a soft link
LN -s/usr/local/lib/libthrift-1.0. 0-dev.so/usr/lib/libthrift-1.0. 0-dev.so
And then the execution started successfully.
Writing rpcclient
The rpcclient code needs to be created on its own, and the header file is copied elsewhere except the server section.
#include"SSANAgent.h"#include<ostream>#include<sstream>#include<thrift/protocol/TBinaryProtocol.h>#include<thrift/transport/TServerSocket.h>#include<thrift/transport/TBufferTransports.h>#include<thrift/transport/TSocket.h>using namespace:: Apache::thrift;using namespace:: Apache::thrift::p rotocol;using namespace:: Apache::thrift:: Transport;using namespace ssan;using namespace std;intMainintargcChar**argv) { if(ARGC <3) {printf ("Usage:%s ip-address command ... \ n", argv[0]); Return-1; } //working with input parametersOstringstream command,address; Address<< argv[1]; if(Argc >2) {Command<< argv[2]; for(intI=3; I < argc;i++) {Command<<" "<<Argv[i]; } } //To access the RPC Server execution commandBoost::shared_ptr<tsocket> socket (New Tsocket (Address.str (). C_STR (),9090)); Boost::shared_ptr<TTransport>Transport (new Tbufferedtransport (socket)); Boost::shared_ptr<TProtocol>Protocol (new Tbinaryprotocol (transport)); Ssanagentclient Client (protocol); stringoutput; Transport-open (); Client.run (Output,command.str ()); cout<< Output <<Endl; Transport-Close (); Return0;}
Processing command line arguments here
Generate Rpcclient
Compile
g++-wall-i/usr/local/include/thrift-c ssanclient. CPP
Link
g++-l/usr/local/lib ssanclient.o ssanagent.o ssan_constants.o ssan_types.o-o ssan_client-lthrift
This part is basically not a big problem.
Writing makefile Compilation
Why is it more convenient to debug the code, or to write a makefile file. Time is limited, this is a temporary write, will continue to improve
Lib_inc =-l/usr/local/libshare_obj =ssan_constants.o ssan_types.o all:server clientserver: SSANAGENT.O SSANSERVER.O $ (share_obj) g+ + $ (lib_inc) $^-o ssan_server-lthrift @echo Ssan_server CREATED.CLIENT:SSANAGENT.O SSANCLIENT.O $ (share_obj) g+ + $ (lib_inc) $^-o ssan_client-lthrift @echo Ssan_client created. #default: Server Clientclean: -RM *. o ssan_client ssan_server done;
Perform make to see the effect
At this point, both Rpcserver and rpcclient have been generated, and the next step is to solve the problem of static compilation. Because of the limited time, this will be in the next blog in the PHP call to the RPC service package to write
RPC Remote Call development