项目文件夹

文件
Asim Aslam 550033dcce Optimize image formats and fix lease re-registration issue (#2959)
* perf: convert generated PNGs to optimized JPEGs (12MB -> 1.5MB)

The landing and docs loaded 18 AI-generated PNGs at 0.5-1MB each. They're
1200x800 RGB illustrations with no transparency, so they recompress ~8x
as progressive JPEG (quality 82) with no visible loss. Convert all,
update every reference (.png -> .jpg), and drop the originals (including
the unused hero.png). Generated images: 12.3MB -> 1.5MB.

* fix(registry/etcd): re-register when a lease silently expires (#2956)

The keepalive rework (long-lived KeepAlive instead of KeepAliveOnce)
moved lease renewal entirely onto the keepalive goroutine; the 30s
periodic Register now skips on the 'unchanged' check. The goroutine only
reacted to the keepalive channel closing, so a lease that expired
server-side without a prompt channel close (e.g. a partition that
outlasted the 90s TTL) left the node de-registered from etcd while the
cache still believed it was registered — and nothing re-registered it.
That is the hidden-failure mode reported in #2956.

React to a non-positive TTL keepalive response the same as a channel
close: drop the cached lease/hash so the next Register performs a full
re-registration. Extract the loop into keepAliveLoop and unit-test the
TTL-expired, channel-closed, and healthy paths (no etcd required).

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-06-10 08:24:49 +01:00

131 行
4.6 KiB
Markdown

---
layout: default
---
# Configuration
<img src="/images/generated/config.jpg" alt="Configuration" style="width: 100%; border-radius: 8px; margin: 1rem 0 1.5rem;" />
Go Micro follows a progressive configuration model so you can start with zero setup and layer in complexity only when needed.
## Levels of Configuration
1. Zero Config (Defaults)
- mDNS registry, HTTP transport, in-memory broker/store
2. Environment Variables
- Override core components without code changes
3. Code Options
- Fine-grained control via functional options
4. External Sources (Future / Plugins)
- Configuration loaded from files, vaults, or remote services
## Core Environment Variables
| Component | Variable | Example | Purpose |
|-----------|----------|---------|---------|
| Registry | `MICRO_REGISTRY` | `MICRO_REGISTRY=consul` | Select registry implementation |
| Registry Address | `MICRO_REGISTRY_ADDRESS` | `MICRO_REGISTRY_ADDRESS=127.0.0.1:8500` | Point to registry service |
| Broker | `MICRO_BROKER` | `MICRO_BROKER=nats` | Select broker implementation |
| Broker Address | `MICRO_BROKER_ADDRESS` | `MICRO_BROKER_ADDRESS=nats://localhost:4222` | Broker endpoint |
| Transport | `MICRO_TRANSPORT` | `MICRO_TRANSPORT=nats` | Select transport implementation |
| Transport Address | `MICRO_TRANSPORT_ADDRESS` | `MICRO_TRANSPORT_ADDRESS=nats://localhost:4222` | Transport endpoint |
| Store | `MICRO_STORE` | `MICRO_STORE=postgres` | Select store implementation |
| Store Database | `MICRO_STORE_DATABASE` | `MICRO_STORE_DATABASE=app` | Logical database name |
| Store Table | `MICRO_STORE_TABLE` | `MICRO_STORE_TABLE=records` | Default table/collection |
| Store Address | `MICRO_STORE_ADDRESS` | `MICRO_STORE_ADDRESS=postgres://user:pass@localhost:5432/app?sslmode=disable` | Connection string |
| Server Address | `MICRO_SERVER_ADDRESS` | `MICRO_SERVER_ADDRESS=:8080` | Bind address for RPC server |
## Example: Switching Components via Env Vars
```bash
# Use NATS for broker and transport, Consul for registry
export MICRO_BROKER=nats
export MICRO_TRANSPORT=nats
export MICRO_REGISTRY=consul
export MICRO_REGISTRY_ADDRESS=127.0.0.1:8500
# Run your service
go run main.go
```
No code changes required. The framework internally wires the selected implementations.
## Equivalent Code Configuration
```go
service := micro.NewService(
micro.Name("helloworld"),
micro.Broker(nats.NewBroker()),
micro.Transport(natstransport.NewTransport()),
micro.Registry(consul.NewRegistry(registry.Addrs("127.0.0.1:8500"))),
)
service.Init()
```
Use env vars for deployment level overrides; use code options for explicit control or when composing advanced setups.
## Precedence Rules
1. Explicit code options always win
2. If not set in code, env vars are applied
3. If neither code nor env vars set, defaults are used
## Discoverability Strategy
Defaults allow local development with zero friction. As teams scale:
- Introduce env vars for staging/production parity
- Consolidate secrets (e.g. store passwords) using external secret managers (future guide)
- Move to service mesh aware registry (Consul/NATS JetStream)
## Validating Configuration
Enable debug logging to confirm selected components:
```bash
MICRO_LOG_LEVEL=debug go run main.go
```
You will see lines like:
```text
Registry [consul] Initialised
Broker [nats] Connected
Transport [nats] Listening on nats://localhost:4222
Store [postgres] Connected to app/records
```
## Patterns
### Twelve-Factor Alignment
Environment variables map directly to deploy-time configuration. Avoid hardcoding component choices so services remain portable.
### Multi-Environment Setup
Use a simple env file per environment:
```bash
# .env.staging
MICRO_REGISTRY=consul
MICRO_REGISTRY_ADDRESS=consul.staging.internal:8500
MICRO_BROKER=nats
MICRO_BROKER_ADDRESS=nats.staging.internal:4222
MICRO_STORE=postgres
MICRO_STORE_ADDRESS=postgres://staging:pass@pg.staging.internal:5432/app?sslmode=disable
```
Load with your process manager or container orchestrator.
## Troubleshooting
| Symptom | Cause | Fix |
|---------|-------|-----|
| Service starts with memory store unexpectedly | Env vars not exported | `env | grep MICRO_STORE` to verify |
| Consul errors about connection refused | Wrong address/port | Check `MICRO_REGISTRY_ADDRESS` value |
| NATS connection timeout | Server not running | Start NATS or change address |
| Postgres SSL errors | Missing sslmode param | Append `?sslmode=disable` locally |
## Related
- [ADR-009: Progressive Configuration](architecture/adr-009-progressive-configuration.md)
- [Getting Started](getting-started.html)
- [Plugins](plugins.html)