The project is in a healthy, maintained state
Ruby implementation of the Service Mesh API Specification over NATS
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 2.5
~> 0.5, < 0.7
 Project Readme

service-mesh-nats-ruby

service-mesh-nats-ruby is the Ruby implementation of the Service Mesh API Specification over NATS. It implements the Ruby contract in the service_mesh gem, from service-mesh-ruby, on top of the nats-pure client.

The ServiceMeshNats module provides:

  • Client, which implements the contract's Client over one NATS connection
  • Runtime, which implements the contract's Runtime on a Client's connection
  • the configuration keys it reads
  • the errors it defines

The contract types it works with, such as ServiceMesh::Target, ServiceMesh::Message, ServiceMesh::Endpoint, and ServiceMesh::Subscriber, come from the service_mesh gem.

Install

# Gemfile
gem "service_mesh_nats"

Requires Ruby 3.3 or newer and a reachable NATS server. The gem depends on service_mesh and nats-pure.

Usage

require "service_mesh_nats"

echo    = ServiceMesh::Target.new(segments: %w[demo echo], kind: :route)
created = ServiceMesh::Target.new(segments: %w[demo created], kind: :topic)
map     = ServiceMesh::ServiceMap.new(targets: [echo, created])

config = {
  "url" => "nats://127.0.0.1:4222",
  "deployment_group" => "demo"
}

client = ServiceMeshNats::Client.new(config, map)
# Connects. Raises BadConfig or the connection failure.

runtime = ServiceMeshNats::Runtime.new(client, config,
  endpoints: [
    ServiceMesh::Endpoint.new(target: echo, handler: ->(m) { ServiceMesh::Message.new(target: echo, payload: m.payload) })
  ],
  subscribers: [
    ServiceMesh::Subscriber.new(target: created, handler: ->(m) { puts "created: #{m.payload}" })
  ])
# Raises NoDeploymentGroup, BadConfig, KindMismatch, or InvalidTarget, and
# ArgumentError for a handler that does not respond to call.

runtime.start
at_exit { runtime.stop(10) }

reply = client.request(ServiceMesh::Message.new(target: echo, payload: "hi"))
client.publish(ServiceMesh::Message.new(target: created, payload: "order 42"))

The client is the runtime's connection. runtime.client returns it in every state, and runtime.stop closes it. A process that only requests and publishes builds a client itself and calls close when done. runtime.service_map and client.service_map return the map the client was built with.

Documentation

  • Examples: a server process, a call-only client process, and consumer groups
  • Public API: every public constant and method in ServiceMeshNats
  • Transport Specific Implementation: subjects, configuration, metadata, delivery, handler failure, timeouts, concurrency, lifecycle, and errors
  • Development: the recipes and the tests