PhoenixMicro — a production-grade microservices toolkit for Elixir/Phoenix.
Phoenix Microservices, built natively for OTP and the BEAM VM.
Quick start
# 1. Configure in config/config.exs
config :phoenix_micro,
transport: :rabbitmq,
consumers: [MyApp.Payments.CreatedConsumer],
transports: [
rabbitmq: [url: "amqp://localhost"]
]
# 2. Define a consumer
defmodule MyApp.Payments.CreatedConsumer do
use PhoenixMicro.Consumer
topic "payments.created"
concurrency 5
retry max_attempts: 3
@impl PhoenixMicro.Consumer
def handle(message, _ctx) do
Logger.info("Received payload: #{inspect(message.payload)}")
:ok
end
end
# 3. Publish from anywhere
PhoenixMicro.publish("payments.created", %{amount: 100, currency: "USD"})
# 4. RPC — two calling conventions
{:ok, result} = PhoenixMicro.rpc("math.sum", [1, 2, 3])
{:ok, result} = PhoenixMicro.rpc("math", "sum", [1, 2, 3])Architecture
PhoenixMicro.Application
└── PhoenixMicro.Supervisor (one_for_one)
├── Registry
├── Transport.Memory
├── Transport.* (configured)
├── Producer
├── RPC
└── ConsumerManager (DynamicSupervisor)
└── Pipeline (Broadway) per consumer
Summary
Functions
Returns all currently running consumer modules.
Stops a running consumer.
Publishes a message to topic asynchronously (fire-and-forget).
Publishes a batch of [{topic, payload}] tuples.
Publishes to topic synchronously. Returns :ok or {:error, reason}.
Dynamically registers and starts a consumer module.
Synchronous RPC call. Supports two signatures
4-argument RPC: rpc(service, pattern, payload, opts).
Returns the active transport module.
Functions
@spec consumers() :: [module()]
Returns all currently running consumer modules.
@spec deregister_consumer(module()) :: :ok | {:error, :not_found}
Stops a running consumer.
Publishes a message to topic asynchronously (fire-and-forget).
Options: :transport, :headers, :correlation_id.
Publishes a batch of [{topic, payload}] tuples.
Publishes to topic synchronously. Returns :ok or {:error, reason}.
Dynamically registers and starts a consumer module.
Synchronous RPC call. Supports two signatures:
# topic + payload form
{:ok, result} = PhoenixMicro.rpc("math.sum", [1, 2, 3])
# service + pattern + payload form (spec-compliant)
{:ok, result} = PhoenixMicro.rpc("math", "sum", [1, 2, 3])
# with options
{:ok, result} = PhoenixMicro.rpc("math", "sum", [1, 2, 3], timeout: 3_000)Options: :timeout (ms, default 5000), :retry (attempts, default 0).
4-argument RPC: rpc(service, pattern, payload, opts).
{:ok, result} = PhoenixMicro.rpc("math", "sum", [1, 2, 3], timeout: 3_000)
@spec transport() :: module()
Returns the active transport module.