Top Related Projects
A Go agent harness and service framework
Gin is a high-performance HTTP web framework written in Go. It provides a Martini-like API but with significantly better performance—up to 40 times faster—thanks to httprouter. Gin is designed for building REST APIs, web applications, and microservices.
High performance, minimalist Go web framework
⚡️ Express inspired web framework written in Go
beego is an open-source, high-performance web framework for the Go programming language.
Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
Quick Overview
Kratos is a Go framework for building microservices, providing a comprehensive set of tools and libraries for developing robust, scalable, and maintainable applications. It offers features like service discovery, load balancing, middleware, and observability out of the box, making it easier for developers to focus on business logic.
Pros
- Comprehensive ecosystem with built-in components for common microservice needs
- Strong focus on performance and scalability
- Well-documented with extensive examples and guides
- Active community and regular updates
Cons
- Steeper learning curve compared to simpler Go frameworks
- May be overkill for small projects or monolithic applications
- Some users report occasional breaking changes between versions
- Limited third-party plugin ecosystem compared to more established frameworks
Code Examples
- Creating a new Kratos application:
package main
import (
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/http"
)
func main() {
app := kratos.New(
kratos.Name("myapp"),
kratos.Version("1.0.0"),
kratos.Server(
http.NewServer(":8000"),
),
)
if err := app.Run(); err != nil {
panic(err)
}
}
- Defining a gRPC service:
package service
import (
"context"
pb "myapp/api/helloworld/v1"
)
type GreeterService struct {
pb.UnimplementedGreeterServer
}
func (s *GreeterService) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + req.Name}, nil
}
- Configuring middleware:
import (
"github.com/go-kratos/kratos/v2/middleware/recovery"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/transport/http"
)
srv := http.NewServer(
http.Address(":8000"),
http.Middleware(
recovery.Recovery(),
tracing.Server(),
),
)
Getting Started
To start using Kratos, follow these steps:
-
Install Kratos CLI:
go install github.com/go-kratos/kratos/cmd/kratos/v2@latest -
Create a new project:
kratos new myapp -
Navigate to the project directory and run the application:
cd myapp go mod tidy kratos run
This will create a new Kratos project with a basic structure and run it on the default port. You can now start building your microservices using the Kratos framework.
Competitor Comparisons
A Go agent harness and service framework
Pros of go-micro
- More mature and established project with a larger community
- Offers a wider range of plugins and extensions
- Provides a complete ecosystem for microservices development
Cons of go-micro
- Can be more complex to set up and configure
- May have a steeper learning curve for beginners
- Less focus on performance optimization compared to Kratos
Code Comparison
go-micro example:
service := micro.NewService(
micro.Name("greeter"),
micro.Version("latest"),
)
service.Init()
pb.RegisterGreeterHandler(service.Server(), new(Greeter))
if err := service.Run(); err != nil {
fmt.Println(err)
}
Kratos example:
app := kratos.New(
kratos.Name("greeter"),
kratos.Version("latest"),
kratos.Server(
http.NewServer(),
grpc.NewServer(),
),
)
if err := app.Run(); err != nil {
log.Fatal(err)
}
Both frameworks provide similar functionality for creating microservices, but Kratos offers a more streamlined approach with built-in support for multiple protocols. go-micro requires additional configuration for multiple servers, while Kratos includes them by default. The syntax and structure of the code are different, reflecting the design philosophies of each framework.
Gin is a high-performance HTTP web framework written in Go. It provides a Martini-like API but with significantly better performance—up to 40 times faster—thanks to httprouter. Gin is designed for building REST APIs, web applications, and microservices.
Pros of Gin
- Lightweight and minimalist framework, offering high performance
- Easy to learn and use, with a simple API and extensive documentation
- Large community and ecosystem with numerous third-party middleware options
Cons of Gin
- Limited built-in features compared to more comprehensive frameworks
- Lacks built-in support for some advanced features like service discovery or circuit breaking
Code Comparison
Gin:
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
r.Run()
Kratos:
app := kratos.New(
kratos.Name("myapp"),
kratos.Version("1.0.0"),
kratos.Server(
http.NewServer(":8000"),
),
)
app.Run()
Gin focuses on simplicity and ease of use, making it ideal for smaller projects or microservices. Kratos, on the other hand, provides a more comprehensive framework with built-in support for microservices architecture, making it suitable for larger, more complex applications. While Gin excels in performance and simplicity, Kratos offers more out-of-the-box features for building robust, scalable microservices.
High performance, minimalist Go web framework
Pros of Echo
- Lightweight and minimalist design, focusing on core HTTP functionality
- Excellent performance and low memory footprint
- Simple and intuitive API, making it easy to learn and use
Cons of Echo
- Less comprehensive feature set compared to Kratos
- Fewer built-in middleware options and extensions
- Limited support for microservices architecture out of the box
Code Comparison
Echo:
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
Kratos:
app := kratos.New(
kratos.Name("helloworld"),
kratos.Version("1.0.0"),
kratos.Server(
http.NewServer(":8000"),
),
)
app.Run()
Echo is more focused on simplicity and ease of use, with a straightforward API for handling HTTP requests. Kratos, on the other hand, provides a more comprehensive framework for building microservices, including features like service discovery, load balancing, and distributed tracing.
While Echo excels in lightweight web applications and APIs, Kratos is better suited for complex, distributed systems. Echo's simplicity makes it easier to get started, but Kratos offers more built-in tools for scalable and resilient microservices architectures.
⚡️ Express inspired web framework written in Go
Pros of Fiber
- Extremely fast performance due to its lightweight design and use of the fasthttp library
- Simple and expressive API, making it easy for developers to quickly build web applications
- Extensive middleware ecosystem, offering a wide range of pre-built solutions for common tasks
Cons of Fiber
- Less comprehensive than Kratos, focusing primarily on HTTP routing and middleware
- Smaller community and ecosystem compared to more established frameworks like Kratos
- Limited built-in support for microservices architecture and service discovery
Code Comparison
Fiber:
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
Kratos:
app := kratos.New(
kratos.Name("helloworld"),
kratos.Version("1.0.0"),
kratos.Server(
http.NewServer(":8000"),
),
)
app.Run()
While Fiber focuses on simplicity and speed for HTTP routing, Kratos provides a more comprehensive framework for building microservices with additional features like service discovery, tracing, and metrics out of the box. Fiber's API is more concise and easier to get started with, while Kratos offers a more structured approach suitable for larger, more complex applications.
beego is an open-source, high-performance web framework for the Go programming language.
Pros of Beego
- More mature and established framework with a larger community
- Comprehensive documentation and extensive examples
- Built-in ORM for database operations
Cons of Beego
- Heavier and more opinionated framework, which may limit flexibility
- Less focus on microservices architecture compared to Kratos
- Slower development cycle and fewer recent updates
Code Comparison
Beego routing example:
beego.Router("/", &controllers.MainController{})
beego.Router("/api/list", &controllers.APIController{}, "get:List")
beego.Run()
Kratos routing example:
s := http.NewServer()
v1 := s.Group("/v1")
v1.GET("/hello", HelloWorld)
s.Run()
Both frameworks offer easy-to-use routing, but Kratos provides a more modern and flexible approach with its group-based routing system. Beego's routing is more traditional and closely tied to its MVC architecture.
Kratos is designed with microservices in mind, offering features like service discovery, load balancing, and distributed tracing out of the box. Beego, while capable of building microservices, is more suited for monolithic applications and requires additional setup for microservices architecture.
Overall, Beego is a solid choice for developers familiar with traditional MVC frameworks, while Kratos is better suited for those building modern, cloud-native applications and microservices.
Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
Pros of mux
- Lightweight and focused solely on HTTP routing
- Extensive documentation and widespread community adoption
- Easy to integrate with existing Go web applications
Cons of mux
- Limited built-in features compared to full-fledged frameworks
- Requires additional libraries for more complex functionality
- Less opinionated, which may lead to inconsistent project structures
Code Comparison
mux:
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler)
http.ListenAndServe(":8000", r)
kratos:
app := kratos.New(
kratos.Name("myapp"),
kratos.Version("1.0.0"),
kratos.Server(
http.NewServer(),
grpc.NewServer(),
),
)
app.Run()
Key Differences
- mux focuses on HTTP routing, while kratos is a full-featured microservices framework
- kratos provides built-in support for gRPC, while mux requires additional libraries
- kratos offers more opinionated project structure and configuration
- mux is easier to integrate into existing projects, while kratos is better suited for building microservices from scratch
- kratos includes features like service discovery, tracing, and metrics out of the box
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Translations: English | ç®ä½ä¸æ
Kratos
Kratos is a lightweight Go framework for building cloud-native microservices. It provides small, explicit APIs for transport, middleware, registry, configuration, logging, encoding, and code generation so applications can focus on business logic.
Features
- API-first development with Protobuf and generated HTTP/gRPC code.
- Unified transport layer for HTTP and gRPC.
- Composable middleware for recovery, logging, validation, tracing, metrics, auth, and more.
- Pluggable registry, configuration, and encoding components.
- Standard-library
log/slogbased logging with OpenTelemetry extensions in contrib packages. - Consistent metadata, errors, validation, OpenAPI, and code-generation workflows.
- A contrib ecosystem for optional integrations such as registries, config stores, middleware, encodings, and observability.
Installation
Requirements
- Go 1.25 or later
- protoc
- protoc-gen-go
Install the CLI
go install github.com/go-kratos/kratos/cmd/kratos/v3@latest
kratos upgrade
Create a Service
kratos new helloworld
cd helloworld
go mod tidy
kratos run
Visit http://localhost:8000/helloworld/kratos after the service starts.
For a fuller generated service flow:
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
Usage Example
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 Migration
Kratos v3 reduces core dependencies and makes previously implicit behavior explicit. Review the v2 to v3 migration guide before upgrading production services.
Further Reading
Development
make test
make lint
Community
Security
If you discover a security vulnerability in Kratos, please contact go-kratos@googlegroups.com. Security reports are handled privately before disclosure.
Contributors
Thank you for contributing to Kratos. The contribution guide is available in the Kratos documentation.
Acknowledgments
The following projects influenced Kratos design:
License
Kratos is open-sourced software licensed under the MIT license.
Top Related Projects
A Go agent harness and service framework
Gin is a high-performance HTTP web framework written in Go. It provides a Martini-like API but with significantly better performance—up to 40 times faster—thanks to httprouter. Gin is designed for building REST APIs, web applications, and microservices.
High performance, minimalist Go web framework
⚡️ Express inspired web framework written in Go
beego is an open-source, high-performance web framework for the Go programming language.
Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot