In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article draws lessons from
Http://www.jianshu.com/p/0f4113d6ec4b
(hereinafter referred to as the brief book tutorial)
First, go to the official website to download the code
Https://thrift.apache.org/download
Download source code thrift-0.9.3.tar.gz
After decompression, put it in the path C:\ thrift-0.9.3\ thrift-0.9.3
And download the windows executive version of thrift-0.9.3.exe
Put it under the path C:\ thrift-0.9.3
Download the apache ant project for typing jar packages
Download path
Http://ant.apache.org/bindownload.cgi
After decompression, put it in the path C:\ apache-ant-1.9.7-bin\ apache-ant-1.9.7
Configure environment variables
ANT_HOME: C:\ apache-ant-1.9.7-bin\ apache-ant-1.9.7
Copy C:\ apache-ant-1.9.7-bin\ apache-ant-1.9.7\ bin\ ant.bat to the path C:\ thrift-0.9.3\ thrift-0.9.3\ lib\ java
Running ant.bat in cmd generates the jar package libthrift-0.9.3 at the path C:\ thrift-0.9.3\ thrift-0.9.3\ lib\ java\ build
Create a text file in the path C:\ thrift-0.9.3
Copy the code
Namespace java com.winwill.thrift
Enum RequestType {
SAY_HELLO, / / say hello
QUERY_TIME, / / ask for time} struct Request {
1: type of required RequestType type; / / request (required)
2: required string name; / / the name of the person who initiated the request (required)
3: optional i32 age; / / the age of the person who initiated the request, optional
}
Exception RequestException {
1: required i32 code
2: optional string reason
}
/ / Service name
Service HelloWordService {
String doAction (1:Request request) throws (1:RequestException qe)
/ / an exception may be thrown.
}
Save as Test.thrift
Enter the path C:\ thrift-0.9.3 in cmd
Execute the command thrift-0.9.3-gen java Test.thrift
A folder gen-java is generated under the path C:\ thrift-0.9.3
Create a project TestThrift in Eclipse
Generate package according to the brief book tutorial
Com.winwill.thrift
Copy the code from the gen-java generated above into package
And create the code in package. Because it may be different from the version used in the simplified book tutorial, some of the writing methods in the simplified book tutorial cannot be compiled.
After modification, use the following code
1. Server side
Package com.winwill.thrift
Import org.apache.commons.lang3.StringUtils
Import org.apache.thrift.TException
Import java.util.Date
Public class HelloWordServiceImpl implements com.winwill.thrift.HelloWordService.Iface {
/ / implement this method to complete the specific logic.
Public String doAction (com.winwill.thrift.Request request) throws com.winwill.thrift.RequestException, TException {
System.out.println ("Get request:" + request)
If (StringUtils.isBlank (request.getName ()) | | request.getType () = = null) {
Throw new com.winwill.thrift.RequestException ()
}
String result = "Hello," + request.getName ()
If (request.getType ()) = = com.winwill.thrift.RequestType.SAY_HELLO) {
Result + = ", Welcome!"
} else {
Result + = ", Now is" + new Date () .toLocaleString ()
}
Return result
}
}
two。 Start the service
Package com.winwill.thrift
Import org.apache.thrift.protocol.TBinaryProtocol
Import org.apache.thrift.protocol.TCompactProtocol
Import org.apache.thrift.protocol.TJSONProtocol
Import org.apache.thrift.protocol.TProtocolFactory
Import org.apache.thrift.server.TServer
Import org.apache.thrift.server.TSimpleServer
Import org.apache.thrift.transport.TFastFramedTransport
Import org.apache.thrift.transport.TFramedTransport
Import org.apache.thrift.transport.TServerSocket
Import org.apache.thrift.transport.TTransportFactory
Import org.slf4j.*
Import java.net.ServerSocket
Public class HelloWordServer {
Public static void main (String [] args) throws Exception {
Int port = 7912
String transport_type = "buffered"
String protocol_type = "binary"
String server_type = "thread-pool"
String domain_socket = ""
/ / ServerSocket socket = new ServerSocket (7912)
/ / Protocol factory
TProtocolFactory tProtocolFactory = null
If (protocol_type.equals ("json")) {
TProtocolFactory = new TJSONProtocol.Factory ()
} else if (protocol_type.equals ("compact")) {
TProtocolFactory = new TCompactProtocol.Factory ()
} else {
TProtocolFactory = new TBinaryProtocol.Factory ()
}
TTransportFactory tTransportFactory = null
If (transport_type.equals ("framed")) {
TTransportFactory = new TFramedTransport.Factory ()
} else if (transport_type.equals ("fastframed")) {
TTransportFactory = new TFastFramedTransport.Factory ()
} else {/ / .equals ("buffered") = > default value
TTransportFactory = new TTransportFactory ()
}
TServerSocket serverTransport = new TServerSocket (new TServerSocket.ServerSocketTransportArgs () .port (port))
Com.winwill.thrift.HelloWordService.Processor processor = new com.winwill.thrift.HelloWordService.Processor (new HelloWordServiceImpl ())
TServer.Args tServerArgs = new TServer.Args (serverTransport)
TServerArgs.processor (processor)
TServerArgs.protocolFactory (tProtocolFactory)
TServerArgs.transportFactory (tTransportFactory)
TServer server = new TSimpleServer (tServerArgs)
System.out.println ("Running server...")
Server.serve ()
}
}
3. Client request
Package com.winwill.thrift
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 HelloWordClient {
Public static void main (String [] args) throws Exception {
TTransport transport = new TSocket ("[server ip address here]", 7912)
TProtocol protocol = new TBinaryProtocol (transport)
/ / create a client
Com.winwill.thrift.HelloWordService.Client client = new com.winwill.thrift.HelloWordService.Client (protocol)
Transport.open (); / / establish a connection
/ / the first request type
Com.winwill.thrift.Request request = new com.winwill.thrift.Request ()
.setType (com.winwill.thrift.RequestType.SAY_HELLO) .setName ("winwill2012") .setAge (24)
System.out.println (client.doAction (request))
/ / the second request type
Request.setType (com.winwill.thrift.RequestType.QUERY_TIME) .setName ("winwill2012")
System.out.println (client.doAction (request))
Transport.close (); / / request ends, disconnect
}
}
Test the startup service on a server
Output Running server...
Start the client on another machine
Output
Hello, winwill2012, Welcome!
Hello, winwill2012, Now is 2016-10-13 17:06:47
Server-side output at this time
Get request: Request (type:SAY_HELLO, name:winwill2012, age:24)
Get request: Request (type:QUERY_TIME, name:winwill2012, age:24)
It means that the connection has been successfully made.
The tools and projects used in this article have been packaged and uploaded to the cloud disk. You are welcome to download.
Link: http://pan.baidu.com/s/1jHQXSma password: 65uh
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.