Lets look at how we expand our
faasd server to include a couple of
services. We'll first add a redis server and some simple ruby code to
add a counter, and then we'll build and add the
nats-connector to be
able publish and subscribe to events.
This looks like a
docker-compose file, and I believe that it actually
does use the same code that parses the file of that name, but it
docker to set up the containers. This is what
The biggest difference I've noticed is how it treats networking. The
short of it is that you need to use the ip address of the main faasd
gateway to access the servers, not the name of the service in the
file. So, instead of pointing your client to
point them to
Setting up Redis
First thing is we need to log into our faasd server, and create the storage directory with the right user.
Then we need to add the redis instance in
Note that if you get a "Background saving error" this is probably because you have an open redis port and someone is scanning you.
10.62.0.1 is the default network that
faasd sets up.
Then we restart:
We can look at the logs using:
Writing our function
Back on our machine, lets create a simple ruby function that increments and returns the value from redis.
Inside of the counter directory, we need to add the redis gem:
And now we can write our handler:
And inside of our
counter.yml file we can add the following
environment variable so we don't hard code the address of the redis
instance in our container.
You'll want to replace that
wschenk with your docker user.
Then we can load it:
And run it:
And you should see a number go up and up!
Publishing and subscribing to messages
The nats-connector is a way to have functions called when messages are sent to certain topics in your nats instance. Lets see how that works.
I couldn't find the latest version of
nats-connector on docker, so
here's how to build it:
Pretty simple. (Change
wschenk to be your username.)
Back on your
faads server, lets first open up
nats to our functions
and then add in the
This section should already be there:
And then add:
Be sure to change the image name to your build, if you don't want to use mine.
And then a simple handler
And then in
And inside of
stack.yml add an annotations section
We don't need to do anything particular with this handler, we just want to look at the logs to see if it gets triggered.
On your dev machine:
Then on the
(Or if you want to be more targeted:)
And on your client machine: