Building a Service
Building a service destined for deployment via Nex is just like building any other service. We don't make any demands of your application or its dependencies, only that it meet the sandboxing requirements of the node on which it will run. There's no proprietary SDK, no custom build tools, no complicated dependency tree.
If you're building "cloud native" or 12 factor services as single-binary deployments, you should have no trouble running them in Nex.
A service, as understood by Nex, is nothing more than a long-running process. A service needs to start (e.g. it has a main
function/entrypoint) and it needs to continue running until the host environment tells it to shut down.
Creating a NATS Service
For this example, we're going to use Go to create a simple executable. We'll use the NATS services framework to expose an endpoint for service discovery.
Open up your favorite editor and edit a main.go
file with the following content:
However you choose to satisfy the dependencies (e.g. with a go.mod
file), you'll need the Go NATS client SDK:
The code is pretty straightforward if you're already used to building applications atop NATS infrastructure. We establish a connection to a NATS server as indicated by the NATS_URL
environment variable.
Next, we create a discoverable service called EchoService
with an endpoint on the svc.echo
subject. This means that the handler we've defined (echoHandler
) will respond to requests on that subject.
Running our Service
To make sure that there are no cards up our proverbial sleeves, let's run this service without using Nex at all. In one terminal, issue a go run main.go
command, while in another terminal, make a request on the svc.echo
subject:
Finally, let's also make sure that this service is behaving properly according to the NATS services specification:
Perfect! Our service is doing everything it's supposed to, and we don't need Nex to test it. This might seem like a subtle point, but it's incredibly powerful that Nex services are not tightly coupled to their means of deployment or scheduling runtime.
Static Compilation
While it's easy enough to test our service locally via go run ...
, in order for our service to be deployable via Nex, it needs to be a statically linked executable. Thankfully, Go makes this easy.
In the same directory as your main.go
, run the following Go command:
If everything went well, you should see no output, but you'll be able to verify that your service is the right kind of file:
In addition to Go, many other languages are just as capable of producing statically linked binaries. For example, if you're using Rust you can just set your target to x86_64-unknown-linux-musl
or aarch64-unknown-linux-musl
for the same effect.
With our statically compiled service in hand, we have one more thing to do before we can start deploying, and that's starting a Nex node process.
Last updated