Language: English | 中文
Kratos Layout is a starter template for building Go microservices with Kratos. It provides a ready-to-extend service structure with HTTP, gRPC, service discovery, data clients, observability, and protobuf-based API generation.
The sample service exposes a Greeter API:
GET /helloworld/{name}- HTTP and gRPC servers powered by Kratos.
- Protobuf-first API definitions with generated HTTP/gRPC bindings.
- Wire-based dependency injection.
- Etcd service registration.
- MySQL and PostgreSQL clients through GORM.
- Redis client instrumentation.
- Kafka producer and consumer group setup.
- OpenTelemetry tracing and metrics.
- Prometheus metrics endpoint.
- pprof server for runtime profiling.
- Docker Compose environment for local infrastructure.
api/ Protobuf API definitions and generated API code
cmd/server/ Application entrypoint and Wire assembly
configs/ Runtime configuration
internal/biz/ Business use cases and domain interfaces
internal/data/ Database, Redis, Kafka, and repository implementations
internal/server/ HTTP, gRPC, and pprof server setup
internal/service/ Transport-facing service implementations
internal/conf/ Configuration protobuf and generated Go code
internal/middleware/ Shared middleware
internal/registry/ Service registry setup
internal/trace/ OpenTelemetry setup
pkg/ Reusable helper packages
third_party/ Protobuf dependencies
The application follows this dependency flow:
repo -> biz -> service -> server -> app
- Go 1.25 or later, matching
go.mod makeprotoc, when regenerating protobuf code- Docker and Docker Compose, when running the full local stack
Start the infrastructure and service:
docker compose up -dThe Compose file starts etcd, Jaeger, MySQL, PostgreSQL, Redis, Kafka, and the
helloworld service image.
Default host ports:
| Service | Host Port | Container Port |
|---|---|---|
| HTTP API | 48000 |
8000 |
| gRPC API | 49000 |
9000 |
| pprof | 46060 |
6060 |
| Jaeger UI | 16686 |
16686 |
| Etcd | 42379 |
2379 |
| MySQL | 43306 |
3306 |
| PostgreSQL | 45432 |
5432 |
| Redis | 46379 |
6379 |
| Kafka | 49092 |
9094 |
Test the HTTP API:
curl http://localhost:48000/helloworld/kratosExpected response:
{"message":"Hello kratos"}Install development tools:
make initBuild the project:
make buildRun the server:
./bin/server -conf configsThe default configs/config.yaml uses Docker service hostnames such as
etcd, mysql, postgresql, redis, kafka, and jaeger. If you run the
binary directly on your host machine, update the config to use localhost and
the mapped ports from docker-compose.yaml, or run the binary inside the
Compose network.
# Install code generation tools
make init
# Generate API protobuf code and OpenAPI output
make api
# Generate internal config protobuf code
make config
# Run go generate and tidy modules
make generate
# Run all generation tasks
make all
# Build all packages
make build
# Run tests
go test ./...Edit API definitions under api/helloworld/v1/*.proto, then regenerate:
make apiEdit runtime configuration schema in internal/conf/conf.proto, then
regenerate:
make configIf provider sets or dependency constructors change, regenerate Wire output:
make generateRuntime configuration is loaded from configs/config.yaml.
Important sections:
server: HTTP, gRPC, and pprof addresses.registry: etcd endpoints for service registration.data.database: MySQL and PostgreSQL connection settings.data.redis: Redis clients and shards.data.kafka: Kafka brokers and consumer group.otel: tracing and metrics configuration.log: log file and rotation settings.bbr: rate limiting and overload protection settings.
- Metrics are exposed through the HTTP server at
/metrics. - Traces are exported to the OTLP endpoint configured in
configs/config.yaml. - The default Docker Compose stack includes Jaeger at
http://localhost:16686. - pprof is exposed on the configured pprof port.
Build the image locally:
docker build -t kratos-layout .The Dockerfile builds the Go binary in a golang:1.25 builder image and runs
it from a slim Debian runtime image.
This project is licensed under the MIT License. See LICENSE for details.