当前位置:网站首页>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": {
}
}
边栏推荐
- ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
- 项目连接数据库遇到的问题及解决
- Advanced Computer Network Review(5)——COPE
- Pytest's collection use case rules and running specified use cases
- [OC-Foundation框架]---【集合数组】
- I-BERT
- Reids之缓存预热、雪崩、穿透
- UML diagram memory skills
- Implement window blocking on QWidget
- BMINF的后训练量化实现
猜你喜欢

甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求

Redis之核心配置

An article takes you to understand the working principle of selenium in detail

Parameterization of postman

Selenium+Pytest自动化测试框架实战

UML圖記憶技巧

MYSQL卸载方法与安装方法
![[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born](/img/70/d275009134fcbf9ae984c0f278659e.jpg)
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born

Post training quantification of bminf

Redis之cluster集群
随机推荐
Detailed explanation of dynamic planning
Advanced Computer Network Review(3)——BBR
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
SimCLR:NLP中的对比学习
Improved deep embedded clustering with local structure preservation (Idec)
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
LeetCode:39. 组合总和
Basic usage of xargs command
Redis之五大基础数据结构深入、应用场景
CSP student queue
BMINF的后训练量化实现
Seven layer network architecture
Advance Computer Network Review(1)——FatTree
Intel Distiller工具包-量化实现1
Alibaba cloud server mining virus solution (practiced)
注意力机制的一种卷积替代方式
QDialog
LeetCode:26. Remove duplicates from an ordered array
pytorch查看张量占用内存大小
xargs命令的基本用法