Quick Start
This guide will help you create and run your first Kratos service in just a few minutes.Prerequisites
Before you begin, ensure you have the following installed:- Go 1.22 or higher - Download Go
- Protocol Buffers compiler - Required for code generation
- Git - For cloning templates
If you haven’t installed the Kratos CLI yet, see the Installation Guide.
Create Your First Service
Create a new project
Use the This command clones the kratos-layout template and sets up a complete project structure.
kratos new command to create a new service from the official template:The CLI will prompt you to select a template. Choose “Service” for a standard microservice template, or “Admin” for an admin backend template.
Install dependencies
github.com/go-kratos/kratos/v2- Core frameworkgoogle.golang.org/grpc- gRPC supportgoogle.golang.org/protobuf- Protobuf runtime
Run your service
Start the service using the Kratos CLI:Or use the standard Go command:You should see output similar to:The service now exposes:
- HTTP server on port
8000 - gRPC server on port
9000
Test your service
Open a new terminal and test the HTTP endpoint:Expected response:
The same endpoint is available via gRPC on port 9000. You can use tools like grpcurl to test gRPC endpoints.
Understanding the Application Structure
Application Bootstrap
The main application is bootstrapped incmd/helloworld/main.go. Here’s the key initialization code:
kratos.New() function creates an application lifecycle manager that:
- Manages server startup and graceful shutdown
- Handles OS signals (SIGTERM, SIGINT, SIGQUIT)
- Registers services with service registry
- Coordinates all application components
Service Implementation
Services are implemented ininternal/service/. The greeter service looks like:
API Definition
APIs are defined using Protocol Buffers inapi/helloworld/v1/greeter.proto:
google.api.http annotation maps gRPC methods to HTTP endpoints, enabling dual-protocol support.
Next Steps
Add New Endpoints
Learn how to add new API endpoints to your service
Configure Middleware
Add logging, tracing, and metrics to your service
Database Integration
Connect to databases and implement data persistence
Service Discovery
Set up service registration and discovery
Common Commands
Here are some useful commands for Kratos development:Troubleshooting
What You’ve Learned
In this quick start, you:- Created a new Kratos service using the CLI
- Ran HTTP and gRPC servers simultaneously
- Made HTTP requests to your service
- Understood the basic project structure
- Learned about application lifecycle management