Go
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.
Go
version 1.18 or later. For installation instructions see Go’s Getting Started Guide.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.
protoc-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:
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:
protoc-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:
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.
protoc
compiler to generate
our fRPC server and client.
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:
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.
EchoService
interface and then start
the server. We’ll start by creating a new server/main.go
file in our ~/frpc
directory:
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:
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.
client/main.go
file
in our ~/frpc
directory:
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:
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.