Installation
To get started with , you’ll need to make sure you haveGo
and the protoc
compiler installed. Then, you’ll need to install
the protoc-gen-go-frpc
which we will use to generate the server and client code.
Prerequisites
- Go - fRPC works with
Go
version 1.18 or later. For installation instructions see Go’s Getting Started Guide. - Protocol Buffer Compiler (protoc) - fRPC works with
protoc
version 3. For installation instructions see the Protoc Getting Started Guide.
brew install go
to install Golang, and brew install protoc
to install the protoc compiler.
Install the fRPC Plugin
To install theprotoc-gen-go-frpc
plugin, you’ll first need to make sure that your $GOBIN
environment variable is set and available in
your system path. See the Go Environment Variables page
for more information, but in general, you can do this by adding the following to
your ~/.bashrc
file:
.bashrc
protoc-gen-go-frpc
plugin itself, you’ll need to run the following command:
protoc-gen-go-frpc
plugin into your $GOBIN
directory
where it will be available for use by the protoc
compiler.
You can check that the plugin is installed and available by running the following command:
Create a Proto3 File
Now that we have the prerequisites and theprotoc-gen-go-frpc
plugin installed,
we can start writing our echo service. Let’s start by creating a directory to house our project:
echo.proto
file and define our message types:
echo.proto
Request
and one for the Response
.
Next, we will define a new EchoService
in our proto3
file. This tells the compiler that we want to generate a server and client for this service.
echo.proto
protoc
compiler to generate
our fRPC server and client.
Generate the Server and Client
Let’s run the following command to generate the server and client code:protoc
compiler to generate the server and client code for us and
by specifying the --go-frpc_out
flag, we’re implicitly specifying that we want to use the protoc-gen-go-frpc
plugin.
If we wanted to be more explicit, we could have run the following command:
~/frpc/echo
, which
contains an echo.frpc.go
file containing the server and client code. Within
that file, you’ll find the following interface:
echo.frpc.go
EchoService
interface with our server-side logic,
and pass that into the server. The generated library will then be able to handle everything else for us.
Setting up the Server
To set up our server, we simply need to implement theEchoService
interface and then start
the server. We’ll start by creating a new server/main.go
file in our ~/frpc
directory:
server/main.go
svc
and implemented the EchoService
interface by
creating a new function called Echo
which takes a context.Context
and an *echo.Request
object.
We aren’t really using the context in this example so we just ignore that and instead return an *echo.Response
object with the
same message as the request.
Now we can implement the server itself:
server/server.go
main
function runs when the server starts up, and passes in our svc
struct to the
generated echo.NewServer()
function. It then binds the server to port :8080
and starts listening for connections.
We’re passing in nil
for both the *tls.Config
and logging
parameters in the generated echo.NewServer()
function because
we don’t want to use TLS or logging in this example.
Setting up the Client
To set up our client, we don’t need to implement any additional logic, but we do need to create a newclient/main.go
file
in our ~/frpc
directory:
client/main.go
echo.NewClient()
function.
Then, we’re passing in the address of the server we want to connect to. But we’re not actually sending any
requests to the server yet.
To do that, we can write a simple look to send a request to the server every second and then print out the response:
echo/client/client.go
stop
channel to receive a signal when the user hits Ctrl+C
,
and then starts sending a request to the server every second.
And that’s it! We’ve now set up a simple echo client that can send requests to our server and print out the response.
We were able to use a simple proto3
file to define our request and response objects, and all we had to do was
implement the EchoService
interface. Everything else was handled for us by fRPC.
The complete code for this example is available in the frpc-echo-example
repository on Github.