Files
go-micro/examples/multi-service/main.go
T
Asim AslamandClaude d2036b880d Claude/update docs roadmap f zd2 j (#2873)
* docs: update all four documentation guides and mark Q2 complete

- ai-native-services: add WithMCP one-liner, standalone gateway,
  WebSocket client example, and OpenTelemetry observability section
- mcp-security: add OTel distributed tracing, WebSocket authentication
  (connection-level and per-message), DeniedReason audit field
- tool-descriptions: add manual overrides with WithEndpointDocs and
  export formats section
- agent-patterns: add LangChain/LlamaIndex SDK pattern and standalone
  gateway production pattern with Docker example
- Update roadmap: mark Q2 documentation as complete, Q2 at 100%
- Update status: reflect all recent completions, shift priorities

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

* feat: add agent demo example and blog post

Add examples/agent-demo with a multi-service project management app
(projects, tasks, team) that demonstrates AI agents interacting with
Go Micro services through MCP. Includes seed data and example prompts.

Add blog post 4 "Agents Meet Microservices: A Hands-On Demo" walking
through the example code and showing cross-service agent workflows.

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

* feat: enable multiple services in a single binary

Remove global state mutations from service and cmd option functions so
that configuring one service no longer overwrites another's settings.

Key changes:
- service/options.go: remove all DefaultXxx global writes from option
  functions; newOptions() now creates fresh Server, Client, Store, and
  Cache per service while sharing Registry, Broker, and Transport
- cmd/cmd.go: newCmd() uses local copies instead of pointers to package
  globals; Before() no longer mutates DefaultXxx vars
- cmd/options.go: remove global mutations from all option functions
- service/service.go: export ServiceImpl type for cross-package use
- service/group.go: new Group type for multi-service lifecycle
- micro.go: add Start/Stop to Service interface, expose Group and
  NewGroup convenience function
- examples/multi-service: working example with two services

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

* docs: highlight multi-service binary support

Add multi-service section to README with code example, update features
list, add to examples index, and note in status summary.

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

* feat: unify service API and clean up developer experience

- Unified service creation: micro.New("name", opts...) as canonical API
- Clean handler registration: service.Handle(handler, opts...) accepts
  server.HandlerOption args directly, no need to reach through Server()
- Unexported serviceImpl: users interact through Service interface only
- Service groups use Service interface (not concrete type)
- Fixed Stop() to properly propagate BeforeStop/AfterStop errors
- Fixed store init: error-level log instead of fatal on init failure
- Updated all examples to use consistent patterns
- Updated README, getting-started, MCP docs, and guides
- Added blog post about the DX cleanup

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-04 11:13:27 +00:00

79 lines
1.8 KiB
Go

// Multi-service example: run multiple services in a single binary.
//
// Each service gets its own server, client, store, and cache while
// sharing the registry, broker, and transport — so they can
// discover and call each other within the same process.
package main
import (
"context"
"fmt"
"log"
"go-micro.dev/v5"
)
// -- Users service --
type UserRequest struct {
Id string `json:"id"`
}
type UserResponse struct {
Name string `json:"name"`
Email string `json:"email"`
}
type Users struct{}
func (u *Users) Lookup(ctx context.Context, req *UserRequest, rsp *UserResponse) error {
log.Printf("[users] Lookup id=%s", req.Id)
rsp.Name = "Alice"
rsp.Email = "alice@example.com"
return nil
}
// -- Orders service --
type OrderRequest struct {
UserId string `json:"user_id"`
}
type OrderResponse struct {
OrderId string `json:"order_id"`
Status string `json:"status"`
}
type Orders struct{}
func (o *Orders) Create(ctx context.Context, req *OrderRequest, rsp *OrderResponse) error {
log.Printf("[orders] Create for user=%s", req.UserId)
rsp.OrderId = "ORD-001"
rsp.Status = "created"
return nil
}
func main() {
// Create two services — each gets isolated server, client,
// store, and cache instances automatically.
users := micro.New("users", micro.Address(":9001"))
orders := micro.New("orders", micro.Address(":9002"))
// Register handlers
if err := users.Handle(new(Users)); err != nil {
log.Fatal(err)
}
if err := orders.Handle(new(Orders)); err != nil {
log.Fatal(err)
}
// Run both services together. The group handles signals
// and stops all services when one exits.
g := micro.NewGroup(users, orders)
fmt.Println("Starting users (:9001) and orders (:9002) in a single binary")
if err := g.Run(); err != nil {
log.Fatal(err)
}
}