github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/proxy/go/rpc_server.go (about) 1 // +build main4 2 3 package main 4 5 import ( 6 "fmt" 7 "net" 8 "net/http" 9 "os" 10 "os/signal" 11 "syscall" 12 13 "github.com/gorilla/rpc" 14 "github.com/gorilla/rpc/json" 15 "github.com/micro/go-micro/v2/registry" 16 "github.com/pborman/uuid" 17 ) 18 19 var ( 20 service = ®istry.Service{ 21 Name: "go.micro.srv.greeter", 22 Nodes: []*registry.Node{ 23 { 24 Id: "go.micro.srv.greeter-" + uuid.NewUUID().String(), 25 Address: "localhost", 26 Port: 4000, 27 }, 28 }, 29 } 30 ) 31 32 type Say struct{} 33 34 type Request map[string]interface{} 35 type Response string 36 37 func (s *Say) Hello(r *http.Request, req *Request, rsp *Response) error { 38 *rsp = Response(fmt.Sprintf("Hello %s!", (*req)["name"])) 39 return nil 40 } 41 42 func main() { 43 l, err := net.Listen("tcp", "localhost:4000") 44 if err != nil { 45 fmt.Println(err) 46 } 47 48 s := rpc.NewServer() 49 s.RegisterCodec(json.NewCodec(), "application/json") 50 s.RegisterService(new(Say), "") 51 http.Handle("/", s) 52 go http.Serve(l, http.DefaultServeMux) 53 54 register(service) 55 56 notify := make(chan os.Signal, 1) 57 signal.Notify(notify, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) 58 <-notify 59 60 deregister(service) 61 l.Close() 62 }