Building a Basic RPC Server and Client with Go

golang.jpg

Repository

https://github.com/golang/go

What Will I Learn?

  • You will learn about RPC (Remote Procedure Calls)
  • You will learn how to build an RPC server
  • You will learn how to build an RPC client
  • You will learn how to create a CRUD app with Go
  • You will learn how to use the net/rpc and net/http libraries in Go

Requirements

System Requirements:

Operating System:
  • FreeBSD 10.3 or later
  • Linux 2.6.23 or later with glibc
  • macOS 10.10 or later
  • Windows 7, Server 2008R2 or later

Required Knowledge

  • An understanding of APIs
  • A basic understanding of Go
  • Some understanding of micro service architecture.

Resources for Go and this Project:

Credits/Sources:

Difficulty

  • Advanced

Description

In this Go video tutorial, we work at building a basic RPC Server and Client. This includes building the basic app without using the RPC library and then building both the Client and Server applications to showcase some of the basic ideas of RPC with Go. The RPC format is talked about at length and shown off using a CRUD application. This tutorial is in service of being able to talk about Microservices, gRPC and Protobuf.

Building a Basic CRUD Application in Go

Before adding the RPC logic to the application, we need to scaffold out the basic features of the service. In this case, that means creating a CRUD application. CRUD stands for create, read, update, and delete as those are the basic operations that we want to perform on the data. For the data in this application, we create a simple slice data structure which is used in lieu of a Database. This slice of the custom item type is just positioned as a global variable in the main CRUD and RPC server application.

go-crud.png

In the above image, we have code that shows the application using the basic CRUD operations. First, we create a few items which will serve as the data that we want to manipulate. We then add that data to our "database" structure using the AddItem function; this is the Create operation. We then are able to print out the state of this new Database to show the items inside of it. The DeleteItem function is run to remove a item from the Database; this is the Delete operation. We then are able to modify an item using the EditItem function; the Edit operation. And finally, we are able to grab items from the database by name using GetByName; the Read operation.

Creating an RPC Server and Client

The basic idea of an RPC interface is to be able to call functions remotely as though they were local to the client application. In other words, unlike a standard REST API, an RPC doesn't require us to serialize and deserialize data to and from an intermediate datatype like JSON or XML. We don't need to worry about API endpoints and Domain Specific Languages with an RPC.

rpc-call.png

In Go, the net/rpc library requires that the functions in the RPC interface follow a specific format. Each function must be a Method on an Exported Datatype. In the case of this application, that Datatype is just a simple int type Aliased to the name API. Each of these methods must also be exported globally. The Methods must contain exactly two exported type arguments and the second argument must be a pointer. And the Methods must return an error type.

by-name.png

Above is an image containing the GetByName RPC method. The method is attached to the API type and it has an uppercase first letter which exports it globally. It also contains two arguments, a title string and a reply pointer Item type. The method also just returns an error type. This method can be called from an RPC client because it fits all of the RPC criteria. The RPC client passes in a string for the title that it wants to read from the database and a pointer to an Item type. This pointer is then used to populate the return type in the RPC client.

The Source Code for this video may be found here: https://github.com/tensor-programming/go-basic-rpc

Video Tutorial

Proof of Work Done

https://github.com/tensor-programming

H2
H3
H4
3 columns
2 columns
1 column
7 Comments