当前位置:网站首页>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": {
}
}
边栏推荐
- Reids之删除策略
- Advanced Computer Network Review(4)——Congestion Control of MPTCP
- 【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
- LeetCode:41. Missing first positive number
- Selenium+pytest automated test framework practice (Part 2)
- Leetcode problem solving 2.1.1
- Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
- Opencv+dlib realizes "matching" glasses for Mona Lisa
- The carousel component of ant design calls prev and next methods in TS (typescript) environment
- 【shell脚本】——归档文件脚本
猜你喜欢
CUDA实现focal_loss
Nacos installation and service registration
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
[OC foundation framework] - string and date and time >
Improved deep embedded clustering with local structure preservation (Idec)
Advanced Computer Network Review(3)——BBR
[MySQL] multi table query
KDD 2022 paper collection (under continuous update)
[OC]-<UI入门>--常用控件的学习
随机推荐
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
An article takes you to understand the working principle of selenium in detail
[OC]-<UI入门>--常用控件-UIButton
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
随手记01
Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
项目连接数据库遇到的问题及解决
五层网络体系结构
KDD 2022 paper collection (under continuous update)
Compétences en mémoire des graphiques UML
Kratos战神微服务框架(二)
go-redis之初始化连接
Leetcode: Jianzhi offer 04 Search in two-dimensional array
CUDA实现focal_loss
Redis之哨兵模式
Ijcai2022 collection of papers (continuously updated)
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
[OC-Foundation框架]---【集合数组】
[MySQL] limit implements paging
IJCAI2022论文合集(持续更新中)