kratos-service-layer
Generates gRPC/HTTP service handlers for go-kratos microservices. Creates service structs, handler methods, and integrates with protobuf definitions. Use when implementing RPC handlers, adding API endpoints, creating dual transport (HTTP/gRPC) handlers, or connecting transport layer to business logic in kratos services.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
<essential_principles>
How Kratos Service Layer Works
Service Layer Role
Connects transport (gRPC/HTTP) to business logic:
Responsibilities:
- Handle gRPC/HTTP requests
- Map proto DTOs to business models (via mapper)
- Call use case methods
- Map results back to proto responses
- Convert business errors to gRPC status codes
Service Struct Pattern
type {Entity}Service struct {
pb.Unimplemented{Entity}ServiceServer // Embed unimplemented server
uc domain.{Entity}UseCase // Use case dependency (from domain package)
}
Import: import "{service}/internal/biz/domain"
Handler Method Pattern
func (s *{Entity}Service) Create{Entity}(ctx context.Context, in *pb.Request) (*pb.Response, error) {
// 1. Map request to business model
entity := {Entity}FromRequest(in)
// 2. Call use case
result, err := s.uc.Create{Entity}(ctx, entity)
if err != nil {
return nil, toServiceError(err) // Map business error to gRPC error
}
// 3. Map result to response
return &pb.Response{Entity: toProto{Entity}(result)}, nil
}
</essential_principles>
- Create service handlers for new entity
- Add handler methods to existing service
- View handler patterns and examples
Wait for response before proceeding.
After reading the workflow, follow it exactly.
<reference_index> Core: handler-pattern.md, service-structure.md, error-mapping.md Mappers: mapper-conventions.md </reference_index>
<workflows_index>
| Workflow | Purpose |
|---|---|
| create-service.md | Generate service handlers and mappers |
| add-handlers.md | Add methods to existing service |
| view-examples.md | Show patterns |
| </workflows_index> |
<success_criteria> Service layer code is correct when:
- Service struct embeds Unimplemented{Entity}ServiceServer
- Constructor added to service.ProviderSet
- Handlers map DTOs via mapper functions
- Errors converted to gRPC status codes
- All methods have godoc comments
- User reminded to run
make generate</success_criteria>
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!