当前位置:网站首页>Kratos ares microservice framework (I)
Kratos ares microservice framework (I)
2022-07-06 09:13:00 【~Pompeii】
Catalog
Kratos Ares microservice framework
brief introduction
Kratos A lightweight Go Microservice framework , It contains a large number of microservice related frameworks and tools .
Name from :《 God of war 》 The game is set in Greek mythology , Tell Kratos (Kratos) The adventure of becoming a god of war by mortals and launching the killing of gods .
The goal is
We are committed to providing a complete R & D experience of microservices , After integrating relevant frameworks and tools , The relevant parts of microservice governance can be insensitive to the overall business development cycle , So more focus on business delivery . For every developer , A complete set of Kratos Framework is also a good learning warehouse , You can understand and refer to the technical accumulation and experience in microservices .
principle
- Simple : Don't over design , The code is plain and simple ;
- Universal : Functions of basic library needed by general business development ;
- Efficient : Improve the efficiency of business iterations ;
- Stable : The basic library has high testability , High coverage , It's safe and reliable to practice on the wire ;
- robust : Through good basic library design , Reduce misuse ;
- High performance : High performance , But not specifically for performance hack Optimize , introduce unsafe ;
- Extensibility : Good interface design , To extend the implementation , Or expand the function by adding the basic library directory ;
- Fault tolerance : Design for failure , The introduction of a large number of SRE The understanding of the , High robustness ;
- Tool chain : Contains a lot of tool chains , such as cache Code generation ,lint Tools, etc ;
characteristic
- APIs: Protocol communication to HTTP/gRPC Based on , adopt Protobuf Define ;
- Errors: adopt Protobuf Of Enum Define as error code , And tool generation decision interface ;
- Metadata: In protocol communication HTTP/gRPC in , adopt Middleware Standardized service meta information transmission ;
- Config: Support multiple data sources , Make configuration merge , adopt Atomic Mode supports dynamic configuration ;
- Logger: Standard log interface , It is convenient to integrate three parties log library , And through fluentd Collect the logs ;
- Metrics: Unified index interface , Various index systems can be realized , Default Integration Prometheus;
- Tracing: follow OpenTelemetry Specification definition , To achieve microservice link tracking ;
- Encoding: Support Accept and Content-Type Automatically select content encoding ;
- Transport: General purpose HTTP/gRPC Transport layer , unified Middleware Plug in support ;
- Registry: Implement the unified registry interface , Plug in connection with various registration centers ;
framework
CLI Tools
install
go install github.com/go-kratos/kratos/cmd/kratos/[email protected]
Create project
adopt kratos Command to create a project template :
kratos new helloworld
Use --nomod
Add service , share go.mod
, Large warehouse mode
kratos new helloworld
cd helloworld
kratos new app/user --nomod
Project structure
.
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── api // The following maintains the usage of microservices proto Files and the files generated from them go file
│ └── helloworld
│ └── v1
│ ├── error_reason.pb.go
│ ├── error_reason.proto
│ ├── error_reason.swagger.json
│ ├── greeter.pb.go
│ ├── greeter.proto
│ ├── greeter.swagger.json
│ ├── greeter_grpc.pb.go
│ └── greeter_http.pb.go
├── cmd // The entry file for the start of the whole project
│ └── server
│ ├── main.go
│ ├── wire.go // We use wire To maintain dependency injection
│ └── wire_gen.go
├── configs // Some sample configuration files for local debugging are usually maintained here
│ └── config.yaml
├── generate.go
├── go.mod
├── go.sum
├── internal // All the code of the service that is not exposed to the public , The usual business logic is down here , Use internal Avoid misreferencing
│ ├── biz // The assembly layer of business logic , similar DDD Of domain layer ,data similar DDD Of repo, and repo The interface is defined here , Use the principle of dependency inversion .
│ │ ├── README.md
│ │ ├── biz.go
│ │ └── greeter.go
│ ├── conf // For internal use config Structure definition of , Use proto Format generation
│ │ ├── conf.pb.go
│ │ └── conf.proto
│ ├── data // Business data access , contain cache、db Equal package , Realized biz Of repo Interface . We may put data And dao Mix it up ,data Focus on the meaning of business , All it has to do is bring out the domain objects again , We took it out DDD Of infra layer .
│ │ ├── README.md
│ │ ├── data.go
│ │ └── greeter.go
│ ├── server // http and grpc Instance creation and configuration
│ │ ├── grpc.go
│ │ ├── http.go
│ │ └── server.go
│ └── service // Realized api Defined service layer , similar DDD Of application layer , Handle DTO To biz Transformation of domain entities (DTO -> DO), At the same time, cooperate with all kinds of biz Interaction , But complex logic should not be dealt with
│ ├── README.md
│ ├── greeter.go
│ └── service.go
└── third_party // api Relying on a third party proto
├── README.md
├── google
│ └── api
│ ├── annotations.proto
│ ├── http.proto
│ └── httpbody.proto
└── validate
├── README.md
└── validate.proto
Code generation and running
Generate
# Generate all proto Source code 、wire wait
go generate ./...
function
# Run the project
kratos run
# Output
INFO msg=config loaded: config.yaml format: yaml # Default load configs/config.yaml The configuration file
INFO msg=[gRPC] server listening on: [::]:9000 # gRPC Service monitoring 9000 port
INFO msg=[HTTP] server listening on: [::]:8000 # HTTP Service monitoring 8000 port
Test interface
test HTTP Interface
The relevant logic code is located in internal/service/greeter.go
curl 'http://127.0.0.1:8000/helloworld/kratos'
# Output :
{
"message": "Hello kratos"
}
curl 'http://127.0.0.1:8000/helloworld/error'
# Output
{
"code": 404,
"reason": "USER_NOT_FOUND",
"message": "user not found: error",
"metadata": {
}
}
边栏推荐
- Alibaba cloud server mining virus solution (practiced)
- Mongodb installation and basic operation
- [Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
- 数学建模2004B题(输电问题)
- Parameterization of postman
- Mise en œuvre de la quantification post - formation du bminf
- 自定义卷积注意力算子的CUDA实现
- Redis之Bitmap
- 【剑指offer】序列化二叉树
- Super efficient! The secret of swagger Yapi
猜你喜欢
After reading the programmer's story, I can't help covering my chest...
Selenium+pytest automated test framework practice
BMINF的后训练量化实现
[MySQL] multi table query
[OC]-<UI入门>--常用控件-UIButton
Advanced Computer Network Review(4)——Congestion Control of MPTCP
Intel distiller Toolkit - Quantitative implementation 2
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
Parameterization of postman
Opencv+dlib realizes "matching" glasses for Mona Lisa
随机推荐
IJCAI2022论文合集(持续更新中)
I-BERT
Intel distiller Toolkit - Quantitative implementation 2
七层网络体系结构
xargs命令的基本用法
LeetCode41——First Missing Positive——hashing in place & swap
LeetCode:34. Find the first and last positions of elements in a sorted array
甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
After reading the programmer's story, I can't help covering my chest...
go-redis之初始化連接
Multivariate cluster analysis
[sword finger offer] serialized binary tree
MySQL uninstallation and installation methods
数学建模2004B题(输电问题)
[MySQL] limit implements paging
Advanced Computer Network Review(4)——Congestion Control of MPTCP
Nacos installation and service registration
【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
Intel Distiller工具包-量化实现3
I-BERT