A microservice project template based on the Kratos framework, integrating common middleware and tools, providing a complete development process and best practices.
- Based on Kratos v2 framework, providing a complete microservice architecture
- Using Protocol Buffers to define APIs, supporting gRPC and HTTP protocols
- Integrated MySQL master-slave read-write separation
- Integrated Redis cache
- Using Wire for dependency injection
- Using sqlc to generate type-safe database access code
- Using buf tool to manage Protocol Buffers
- Support for database transactions and error handling
- Integrated logging system
- Docker deployment support
- Go 1.24+
- Kratos v2
- Protocol Buffers
- gRPC/HTTP
- MySQL
- Redis
- Wire
- sqlc
- buf
- Docker
.
├── api # API definitions
│ └── server # Service API definitions
├── cmd # Application entries
│ └── server # Service entry
├── internal # Internal code
│ ├── biz # Business logic layer
│ ├── conf # Configuration definitions
│ ├── data # Data access layer
│ │ ├── migration # Database migrations
│ │ ├── queries # Generated query code
│ │ └── query # SQL query definitions
│ ├── server # Server implementations
│ └── service # Service interface implementations
├── pkg # Public packages
├── Dockerfile # Docker build file
├── Makefile # Build scripts
├── config.yaml # Configuration file
└── sqlc.yaml # sqlc configuration
# Initialize development environment, install required tools
make initThis will install the following tools:
- kratos CLI
- protoc-gen-go-http
- wire
- sqlc
- buf
- protoc-gen-openapi
- protoc-gen-go
- protoc-gen-go-grpc
- migrate
# Generate API related code
make api
# Generate configuration related code
make config
# Generate all code
make all
# Generate dependency injection code
make wire
# Generate database access code
make sqlc# Create a new migration file
make new_migration name=migration_name# Run the service
make server# Build the project
make build# Build Docker image
docker build -t <your-docker-image-name> .
# Run Docker container
docker run --rm -p 8000:8000 -p 9000:9000 -v </path/to/your/configs>:/data/conf <your-docker-image-name>The project automatically generates an OpenAPI specification file openapi.yaml, which can be viewed using tools like Swagger UI.
The project includes the following database tables:
user: Basic user informationuser_detail: Detailed user information
The configuration file is located at config.yaml, containing the following main configurations:
- Service configuration (HTTP/gRPC)
- Database configuration (master-slave)
- Redis configuration
- Logging configuration