-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
# golang-rpc-example | ||
Example of how to make an RPC using golang | ||
Simple server and client that communicate using Go's RPC library. | ||
|
||
Start the server with: | ||
|
||
go run server.go | ||
|
||
Then start the client with: | ||
|
||
go run client.go --factor1=12 --factor2=13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/rpc" | ||
|
||
"github.com/zeptonaut/golang-rpc-example/service" | ||
) | ||
|
||
var server = flag.String("server_port", "localhost:9999", "Address at which to reach the server.") | ||
var factor1 = flag.Int("factor1", 3, "First factor to multiply.") | ||
var factor2 = flag.Int("factor2", 4, "Second factor to multiply.") | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
client, err := rpc.DialHTTP("tcp", *server) | ||
if err != nil { | ||
log.Fatal("dialing: ", err) | ||
} | ||
|
||
request := &service.MultiplicationRequest{*factor1, *factor2} | ||
response := new(service.MultiplicationResponse) | ||
|
||
err = client.Call("MultiplicationService.Multiply", request, &response) | ||
if err != nil { | ||
log.Fatal("MultiplicationService error:", err) | ||
} | ||
|
||
fmt.Println(response.Product) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
"net" | ||
"net/http" | ||
"net/rpc" | ||
|
||
"github.com/zeptonaut/golang-rpc-example/service" | ||
) | ||
|
||
var port = flag.Int("port", 9999, "Port on which to start the server.") | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
server := new(service.MultiplicationService) | ||
rpc.Register(server) | ||
rpc.HandleHTTP() | ||
|
||
l, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) | ||
if err != nil { | ||
log.Fatal("Listen error:", err) | ||
} | ||
|
||
fmt.Printf("Serving on localhost:%d\n", *port) | ||
http.Serve(l, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package service | ||
|
||
type MultiplicationRequest struct { | ||
Factor1, Factor2 int | ||
} | ||
|
||
type MultiplicationResponse struct { | ||
Product int | ||
} | ||
|
||
type MultiplicationService struct{} | ||
|
||
func (t *MultiplicationService) Multiply(request *MultiplicationRequest, response *MultiplicationResponse) error { | ||
response.Product = request.Factor1 * request.Factor2 | ||
return nil | ||
} |