当前位置:网站首页>Kratos ares microservice framework (I)

Kratos ares microservice framework (I)

2022-07-06 09:13:00 ~Pompeii

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

kratos architecture

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

kratos ddd

  .
├── 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": {
    }
}
原网站

版权声明
本文为[~Pompeii]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060859159176.html