trpc.group/trpc-go/trpc-go@v1.0.3/server/README.md (about) 1 English | [中文](README.zh_CN.md) 2 3 # tRPC-Go Server Package 4 5 6 ## Introduction 7 8 A service process may listen on multiple ports, providing different business services on different ports. Therefore, the server package introduces the concepts of `Server`, `Service`, and `Proto service`. Typically, one process contains one `Server`, and each `Server` can contain one or more `Service`. `Services` are used for name registration, and clients use it's names for routing and sending network requests. Upon receiving a request, the server executes the business logic based on the specified `Protos service`. 9 10 - `Server`: Represents a tRPC server instance, i.e., one process. 11 - `Service`: Represents a logical service, i.e., a real external service that listens on a port. It corresponds one-to-one with services defined in the configuration file, with one `Server` possibly containing multiple `Service`, one for each port. 12 - `Proto service`: Represents a protocol service defined in a protobuf protocol file. Typically, a `Service` corresponds one-to-one with a `Proto service`, but users can also combine them arbitrarily using the `Register` method. 13 14 ```golang 15 // Server is a tRPC server. One process, one server. 16 // A server may offer one or more services. 17 type Server struct { 18 MaxCloseWaitTime time.Duration 19 } 20 21 // Service is the interface that provides services. 22 type Service interface { 23 // Register registers a proto service. 24 Register(serviceDesc interface{}, serviceImpl interface{}) error 25 // Serve starts serving. 26 Serve() error 27 // Close stops serving. 28 Close(chan struct{}) error 29 } 30 ``` 31 32 ## Service Mapping Relationships 33 34 Suppose a protocol file provides a `hello service` as follows: 35 36 ```protobuf 37 service hello { 38 rpc SayHello(HelloRequest) returns (HelloReply) {}; 39 } 40 ``` 41 42 And a configuration file specifies multiple services, each providing `trpc` and `http` protocol services: 43 44 ```yaml 45 server: # Server configuration 46 app: test # Application name 47 server: helloworld # Process service name 48 close_wait_time: 5000 # Minimum waiting time for service unregistration when closing, in milliseconds 49 max_close_wait_time: 60000 # Maximum waiting time when closing to allow pending requests to complete, in milliseconds 50 service: # Business services providing two services, listening on different ports and offering different protocols 51 - name: trpc.test.helloworld.HelloTrpc # Name for the first service 52 ip: 127.0.0.1 # IP address the service listens on 53 port: 8000 # Port the service listens on (8000) 54 protocol: trpc # Provides tRPC protocol service 55 - name: trpc.test.helloworld.HelloHttp # Name for the second service 56 ip: 127.0.0.1 # IP address the service listens on 57 port: 8080 # Port the service listens on (8080) 58 protocol: http # Provides HTTP protocol service 59 ``` 60 61 To register protocol services for different logical services: 62 63 ```golang 64 type helloImpl struct{} 65 66 func (s *helloImpl) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) { 67 rsp := &pb.HelloReply{} 68 // implement business logic here ... 69 return rsp, nil 70 } 71 72 func main() { 73 s := trpc.NewServer() 74 75 // Recommended: Register a proto service for each service separately 76 pb.RegisterHiServer(s.Service("trpc.test.helloworld.HelloTrpc"), helloImpl) 77 pb.RegisterHiServer(s.Service("trpc.test.helloworld.HelloHttp"), helloImpl) 78 79 // Alternatively, register the same proto service for all services in the server 80 pb.RegisterHelloServer(s, helloImpl) 81 } 82 ``` 83 84 ## Server Execution Flow 85 86 1. The transport layer accepts a new connection and starts a goroutine to handle the connection's data. 87 2. Upon receiving a complete data packet, unpack the entire request. 88 3. Locate the specific handling function based on the specific proto service name. 89 4. Decode the request body. 90 5. Set an overall message timeout. 91 6. Decompress and deserialize the request body. 92 7. Call pre-interceptors. 93 8. Enter the business handling function. 94 9. Exit the business handling function. 95 10. Call post-interceptors. 96 11. Serialize and compress the response body. 97 12. Package the entire response. 98 13. Send the response back to the upstream client.