micro--go-micro
39484560ea
- Update main README with micro run quick start - Expand cmd/micro/README.md with configuration options - Add detailed guide at internal/website/docs/guides/micro-run.md Documents: - Hot reload with file watching - micro.mu DSL configuration - micro.json alternative - Dependency ordering - Environment management - Graceful shutdown Co-authored-by: Shelley <shelley@exe.dev>
4.0 KiB
4.0 KiB
layout
| layout |
|---|
| default |
micro run - Local Development
micro run provides a Rails/Spring-like development experience for Go microservices.
Quick Start
# Run services in current directory with hot reload
micro run
# Run from a specific directory
micro run ./myapp
# Clone and run from GitHub
micro run github.com/micro/blog
Features
Hot Reload
By default, micro run watches for .go file changes and automatically rebuilds and restarts affected services.
micro run # Hot reload enabled (default)
micro run --no-watch # Disable hot reload
Changes are debounced (300ms) to handle rapid saves from editors.
Configuration File
For multi-service projects, create a micro.mu file to define services, dependencies, and environments.
micro.mu (Recommended)
# Service definitions
service users
path ./users
port 8081
service posts
path ./posts
port 8082
depends users
service web
path ./web
port 8089
depends users posts
# Environment configurations
env development
STORE_ADDRESS file://./data
DEBUG true
env production
STORE_ADDRESS postgres://localhost/db
DEBUG false
micro.json (Alternative)
{
"services": {
"users": {
"path": "./users",
"port": 8081
},
"posts": {
"path": "./posts",
"port": 8082,
"depends": ["users"]
}
},
"env": {
"development": {
"STORE_ADDRESS": "file://./data"
}
}
}
Service Properties
| Property | Required | Description |
|---|---|---|
path |
Yes | Directory containing the service (with main.go) |
port |
No | Port the service listens on (enables health check waiting) |
depends |
No | Services that must start first (space-separated in .mu, array in .json) |
Dependency Ordering
When depends is specified, services start in topological order:
- Services with no dependencies start first
- Each service waits for its dependencies to be ready
- If a service has a
port, we wait for/healthto return 200 - Circular dependencies are detected and reported as errors
Environment Management
micro run # Uses 'development' (default)
micro run --env production # Uses 'production'
micro run --env staging # Uses 'staging'
MICRO_ENV=test micro run # Environment variable override
Environment variables from the config are injected into each service's environment.
Graceful Shutdown
On SIGINT (Ctrl+C) or SIGTERM:
- Services stop in reverse dependency order
- SIGTERM is sent first (graceful)
- After 5 seconds, SIGKILL if still running
- PID files are cleaned up
Without Configuration
If no micro.mu or micro.json exists:
- All
main.gofiles are discovered recursively - Each is built and run
- No dependency ordering
- Hot reload still works
Logs
Service logs are written to:
- Terminal: Colorized with service name prefix
- File:
~/micro/logs/{service}-{hash}.log
View logs:
micro logs # List available logs
micro logs users # Show logs for 'users' service
Process Management
micro status # Show running services
micro stop users # Stop a specific service
Example: micro/blog
The micro/blog project demonstrates a multi-service setup:
# micro.mu
service users
path ./users
port 8081
service posts
path ./posts
port 8082
depends users
service comments
path ./comments
port 8083
depends users posts
service web
path ./web
port 8089
depends users posts comments
Run it:
micro run github.com/micro/blog
Tips
- Port Configuration: Set
portfor services that expose HTTP to enable health check waiting - Health Endpoint: Implement
/healthreturning 200 for reliable startup sequencing - Environment Separation: Keep secrets in production env, use file:// paths for development
- Hot Reload Scope: Only
.gofiles trigger rebuilds; static assets don't