Building a Nex 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:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "github.com/nats-io/nats.go"
  8. services "github.com/nats-io/nats.go/micro"
  9. )
  10. func main() {
  11. ctx := context.Background()
  12. natsUrl := os.Getenv("NATS_URL")
  13. if len(strings.TrimSpace(natsUrl)) == 0 {
  14. natsUrl = nats.DefaultURL
  15. }
  16. fmt.Printf("Echo service using NATS url '%s'\n", natsUrl)
  17. nc, err := nats.Connect(natsUrl)
  18. if err != nil {
  19. panic(err)
  20. }
  21. // request handler
  22. echoHandler := func(req services.Request) {
  23. req.Respond(req.Data())
  24. }
  25. fmt.Println("Starting echo service")
  26. _, err = services.AddService(nc, services.Config{
  27. Name: "EchoService",
  28. Version: "1.0.0",
  29. // base handler
  30. Endpoint: &services.EndpointConfig{
  31. Subject: "svc.echo",
  32. Handler: services.HandlerFunc(echoHandler),
  33. },
  34. })
  35. if err != nil {
  36. panic(err)
  37. }
  38. <-ctx.Done()
  39. }

However you choose to satisfy the dependencies (e.g. with a go.mod file), you’ll need the Go NATS client SDK:

  1. $ go get github.com/nats-io/nats.go

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:

  1. $ nats req svc.echo 'this is a test'
  2. 17:10:53 Sending request on "svc.echo"
  3. 17:10:53 Received with rtt 567.537µs
  4. this is a test

Finally, let’s also make sure that this service is behaving properly according to the NATS services specification:

  1. $ nats micro ls
  2. ╭──────────────────────────────────────────────────────────────╮
  3. All Micro Services
  4. ├─────────────┬─────────┬────────────────────────┬─────────────┤
  5. Name Version ID Description
  6. ├─────────────┼─────────┼────────────────────────┼─────────────┤
  7. EchoService 1.0.0 FKIuiivKgSB8VWyjDYBpEc
  8. ╰─────────────┴─────────┴────────────────────────┴─────────────╯

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:

  1. $ CGO_ENABLED=0 go build -tags netgo -ldflags '-extldflags "-static"'

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:

  1. $ file echoservice
  2. echoservice: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=XXFUNOXfjIEOepi2cW-o/gnDMAXpM9aha9OAEsvLi/XPLmCOsZsF3NATJ_-Zkt/71yl5VaZNDY-jsIhJkcc, with debug_info, not stripped

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.