Recently, when combing the company's services, decided to pull the basic components out of service. Because the company has a variety of language development services, decided to use thrift to resolve cross-language communication.
Start a server
Thrift's support for Golang is good, and the official package provides a server base class and a simple service simple_server.go. You can implement the server to do authentication and other statistical operations.
Server defines a tserver interface and some methods, the source code is as follows:
Type Tserver Interface {
Processorfactory () tprocessorfactory//Service
Servertransport () Tservertransport//connection
Inputtransportfactory () ttransportfactory//input
Outputtransportfactory () ttransportfactory//Output
Inputprotocolfactory () Tprotocolfactory//protocol
Outputprotocolfactory () Tprotocolfactory//protocol
Starts the server
Serve () Error
Stops the server. This is optional on a per-implementation basis. Not
All servers are required is cleanly stoppable.
Stop () Error
}
The server method listens, accepts requests, and dispatches the processing. The following is the Simple_server.go server method.
Func (P *tsimpleserver) Serve () error {
ERR: = P.listen ()
If Err!= nil {
return err
}
P.acceptloop ()
return Nil
}
Func (P *tsimpleserver) Acceptloop () error {
for {
Client, Err: = P.servertransport.accept ()
If Err!= nil {
Select {
Case <-p.quit:
return Nil
Default
}
return err
}
If client!= nil {
Go func () {
If err: = p.processrequests (client); Err!= Nil {
Log. PRINTLN ("Error processing request:", err)
}
}()
}
}
}
As long as the implementation of the relevant interface methods, in the method can be related to the identification/statistics processing.
On the problem of multi-multiplexed IDL
Golang Multiplexing is to write service in an IDL file, and Java does not need. This is a little time delay at the beginning.