MCPcopy
hub / github.com/go-kratos/kratos

github.com/go-kratos/kratos @v3.0.0 sqlite

repository ↗ · DeepWiki ↗ · release v3.0.0 ↗
2,930 symbols 10,329 edges 321 files 1,002 documented · 34%
README

Build Status GoDoc DeepWiki codeCov Go Report Card License Awesome Go Discord

go-kratos%2Fkratos | Trendshift Go Kratos - A Go framework for microservices. | Product Hunt

Translations: English | 简体中文

Kratos

Kratos 是一套轻量级 Go 微服务框架,围绕传输、Middleware、注册发现、配置、日志、编码和代码生成提供清晰的基础能力,让业务代码保持聚焦。

功能特性

  • 以 Protobuf 为中心定义 API,并生成 HTTP/gRPC 代码。
  • 统一的 Transport 抽象,支持 HTTPgRPC
  • 可组合的 Middleware,覆盖 Recovery、Logging、Validation、Tracing、Metrics、Auth 等场景。
  • 插件化的 RegistryConfigEncoding 能力。
  • 基于标准库 log/slog 的日志能力,OpenTelemetry 扩展由 contrib 包提供。
  • 统一的 Metadata、Errors、Validation、OpenAPI 和代码生成工作流。
  • contrib 生态提供注册中心、配置中心、Middleware、编码和可观测性等可选集成。

安装

环境要求

安装 CLI

go install github.com/go-kratos/kratos/cmd/kratos/v3@latest
kratos upgrade

创建服务

kratos new helloworld
cd helloworld
go mod tidy
kratos run

服务启动后访问 http://localhost:8000/helloworld/kratos

如果需要从 proto 开始生成服务代码:

kratos proto add api/helloworld/helloworld.proto
kratos proto client api/helloworld/helloworld.proto
kratos proto server api/helloworld/helloworld.proto -t internal/service
go generate ./...
kratos run

使用示例

package main

import (
    "github.com/go-kratos/kratos/v3"
    "github.com/go-kratos/kratos/v3/transport/grpc"
    "github.com/go-kratos/kratos/v3/transport/http"
)

func main() {
    httpSrv := http.NewServer(http.Address(":8000"))
    grpcSrv := grpc.NewServer(grpc.Address(":9000"))

    app := kratos.New(
        kratos.Name("helloworld"),
        kratos.Version("v1.0.0"),
        kratos.Server(httpSrv, grpcSrv),
    )
    if err := app.Run(); err != nil {
        panic(err)
    }
}

v3 迁移

Kratos v3 进一步降低核心依赖,并将历史上隐式的行为显式化。升级生产服务前请先阅读 v2 到 v3 迁移指南

扩展阅读

开发

make test
make lint

社区

安全

如果你发现 Kratos 存在安全漏洞,请发送邮件到 go-kratos@googlegroups.com。安全问题会在公开披露前以私密方式处理。

贡献者

感谢所有为 Kratos 做出贡献的开发者。贡献流程请参考 Kratos 贡献指南

Kratos contributors

致谢

以下项目对 Kratos 的设计有重要影响:

License

Kratos 基于 MIT license 开源。

Extension points exported contracts — how you extend this code

Codec (Interface)
Codec defines the interface Transport uses to encode and decode messages. Note that implementations of this interface mu [13 …
encoding/encoding.go
Registrar (Interface)
Registrar is service registrar. [15 implementers]
registry/registry.go
Endpointer (Interface)
Endpointer is registry endpoint. [17 implementers]
transport/transport.go
Limiter (Interface)
Limiter is a rate limiter. [6 implementers]
internal/ratelimit/ratelimit.go
Balancer (Interface)
Balancer is balancer interface [10 implementers]
selector/balancer.go
Builder (Interface)
Builder build selector [13 implementers]
selector/selector.go
Source (Interface)
Source is config source. [11 implementers]
config/source.go
AppInfo (Interface)
AppInfo is application context value. [3 implementers]
app.go

Core symbols most depended-on inside this repo

String
called by 187
config/value.go
Error
called by 153
contrib/registry/kubernetes/registry.go
New
called by 136
cmd/kratos/internal/project/new.go
Run
called by 124
app.go
Set
called by 84
transport/transport.go
Unmarshal
called by 77
encoding/encoding.go
Close
called by 74
transport/http/stream.go
Is
called by 60
errors/errors.go

Shape

Function 1,249
Method 1,153
Struct 387
FuncType 75
Interface 47
TypeAlias 19

Languages

Go100%

Modules by API surface

transport/http/stream.go76 symbols
internal/testdata/complex/complex.pb.go51 symbols
transport/http/context.go50 symbols
contrib/registry/eureka/client.go46 symbols
contrib/middleware/validate/internal/testdata/test.pb.validate.go39 symbols
transport/http/client_test.go36 symbols
transport/http/server.go34 symbols
internal/testdata/helloworld/helloworld_grpc.pb.go32 symbols
config/value.go32 symbols
transport/http/client.go30 symbols
internal/ratelimit/bbr.go30 symbols
contrib/registry/nacos/mock_nacos_test.go30 symbols

Dependencies from manifests, versioned

buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/gov1.36.11-20260415201 · 1×
buf.build/go/protovalidatev1.2.0 · 1×
cel.dev/exprv0.25.2 · 1×
github.com/AlecAivazis/survey/v2v2.3.7 · 1×
github.com/alibabacloud-go/darabonba-arrayv0.1.0 · 1×
github.com/alibabacloud-go/darabonba-encode-utilv0.0.2 · 1×
github.com/alibabacloud-go/darabonba-mapv0.0.2 · 1×
github.com/alibabacloud-go/darabonba-stringv1.0.2 · 1×
github.com/alibabacloud-go/debugv1.0.1 · 1×
github.com/alibabacloud-go/openapi-utilv0.1.2 · 1×

For agents

$ claude mcp add kratos \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact