micro--go-micro
310cee61f9
Run Tests / Unit Tests (push) Has been cancelled
Run Tests / Etcd Integration Tests (push) Has been cancelled
- Add README for multi-service example explaining modular monolith pattern - Add pubsub-events example with broker and event streaming demos - Add grpc-integration example showing gRPC server/client with JSON codec - Update examples/README.md to replace "Coming Soon" with real examples - Add tests for core packages: micro.go and service/service.go - Add micro doctor diagnostic command (Go, registry, ports, NATS, config) - Fix micro gen templates: replace TODO stubs with real implementation logic - Add consul and etcd registry support to micro mcp serve/test commands - Make file watcher configurable: extensions, excludes, go.mod watching - Add watcher tests https://claude.ai/code/session_01VwPw7hMaVhFfT69oCE6x1D
72 行
1.8 KiB
Markdown
72 行
1.8 KiB
Markdown
# Multi-Service Example
|
|
|
|
Run multiple services in a single binary — the **modular monolith** pattern.
|
|
|
|
## What It Shows
|
|
|
|
- Two independent services (`users` and `orders`) in one process
|
|
- Each service gets isolated server, client, store, and cache
|
|
- Shared registry and broker for inter-service communication
|
|
- Coordinated lifecycle with `micro.NewGroup()`
|
|
|
|
## Run It
|
|
|
|
```bash
|
|
go run .
|
|
```
|
|
|
|
This starts both services:
|
|
- **Users** on `:9001` — provides `Users.Lookup`
|
|
- **Orders** on `:9002` — provides `Orders.Create`
|
|
|
|
## Call the Services
|
|
|
|
From another terminal:
|
|
|
|
```bash
|
|
# Look up a user
|
|
micro call users Users.Lookup '{"id": "1"}'
|
|
|
|
# Create an order
|
|
micro call orders Orders.Create '{"user_id": "1"}'
|
|
```
|
|
|
|
## How It Works
|
|
|
|
```go
|
|
// Create isolated services
|
|
users := micro.New("users", micro.Address(":9001"))
|
|
orders := micro.New("orders", micro.Address(":9002"))
|
|
|
|
// Register handlers
|
|
users.Handle(new(Users))
|
|
orders.Handle(new(Orders))
|
|
|
|
// Run together — stops all when one exits
|
|
g := micro.NewGroup(users, orders)
|
|
g.Run()
|
|
```
|
|
|
|
Each service gets its own server and client, so they can be split into separate binaries later without code changes. The group handles coordinated startup and graceful shutdown.
|
|
|
|
## When to Use This Pattern
|
|
|
|
- **Early development** — run everything locally in one process
|
|
- **Testing** — integration tests without Docker or networking
|
|
- **Small teams** — fewer moving parts until you need to scale independently
|
|
- **Gradual migration** — start monolith, split services one at a time
|
|
|
|
## Splitting Later
|
|
|
|
When you're ready to run services independently, just move each into its own `main.go`:
|
|
|
|
```go
|
|
func main() {
|
|
svc := micro.New("users", micro.Address(":9001"))
|
|
svc.Handle(new(Users))
|
|
svc.Run()
|
|
}
|
|
```
|
|
|
|
No handler code changes needed — the registry handles discovery automatically.
|