Kratos is a microservice-oriented governance framework implements by golang,

kratos

Language Build Status GoDoc Go Report Card Discord

Translations: English | 简体中文

Kratos

Kratos is a microservice-oriented governance framework implements by golang, which offers convenient capabilities to help you quickly build a bulletproof application from scratch.

The name is inspired by the game God of War which is based on Greek myths, tells the Kratos from mortals to become a God of War and launches the adventure of killing god.

Goals

Kratos boosts your productivity. With the integration of excellent resources and further support, programmers can get rid of most issues might encounter in the field of distributed systems and software engineering such that they are allowed to focus on the release of businesses only. Additionally, for each programmer, Kratos is also an ideal one learning warehouse for many aspects of microservices to enrich their experiences and skills.

Principles

  • Simple: Appropriate design, plain and easy code.
  • General: Cover the various utilities for business development.
  • Highly efficient: Speeding up the efficiency of businesses upgrading.
  • Stable: The base libs validated in the production environment which have the characters of the high testability, high coverage as well as high security and reliability.
  • Robust: Eliminating misusing through high quality of the base libs.
  • High-performance: Optimal performance excluding the optimization of hacking in case of unsafe
  • Expandability: Properly designed interfaces, you can expand utilities such as base libs to meet your further requirements.
  • Fault-tolerance: Designed against failure, enhance the understanding and exercising of SRE within Kratos to achieve more robustness.
  • Toolchain: Includes an extensive toolchain, such as the code generation of cache, the lint tool, and so forth.

Features

  • APIs: The communication protocol is based on the HTTP/gRPC through the definition of Protobuf.
  • Errors: Both the definitions of error code and the handle interfaces of code generation for tools are defined by the Enum of the Protobuf.
  • Metadata: In the protocol of HTTP/gRPC, the transmission of service atomic information are formalized by the Middleware.
  • Config: Multiple data sources are supported for configurations and integrations such that dynamic configurations are offered through the manner of Atomic operations.
  • Logger: The standard log interfaces ease the integration of the third-party log libs and logs are collected through the Fluentd.
  • Metrics: Prometheus integrated by default. Furthermore, with the uniform metric interfaces, you can implement your own metric system more flexible.
  • Tracing: The OpenTelemetry is conformed to achieve the tracing of microservices chains.
  • Encoding: The selection of the content encoding is automatically supported by Accept and Content-Type.
  • Transport: The uniform plugins for Middleware are supported by HTTP/gRPC.
  • Registry: The interfaces of the centralized registry is able to be connected with various other centralized registries through plug-ins.

Getting Started

Required

Installing

go get github.com/go-kratos/kratos/cmd/kratos/v2@latest

Create a service

# create project template
kratos new helloworld

cd helloworld
# download modules
go mod download

# generate Proto template
kratos proto add api/helloworld/helloworld.proto
# generate Proto source code
kratos proto client api/helloworld/helloworld.proto
# generate server template
kratos proto server api/helloworld/helloworld.proto -t internal/service

# generate all proto source code, wire, etc.
go generate ./...
# compile
go build -o ./bin/ ./...
# run
./bin/helloworld -conf ./configs

Kratos Boot

import "github.com/go-kratos/kratos/v2"
import "github.com/go-kratos/kratos/v2/transport/grpc"
import "github.com/go-kratos/kratos/v2/transport/http"

httpSrv := http.NewServer(http.Address(":8000"))
grpcSrv := grpc.NewServer(grpc.Address(":9000"))

app := kratos.New(
    kratos.Name("kratos"),
    kratos.Version("latest"),
    kratos.Server(httpSrv, grpcSrv),
)
app.Run()

Related

Community

License

Kratos is MIT licensed. See the LICENSE file for details.

Contributors

Thanks for their outstanding contributions.

Owner
Kratos
A Go framework for microservices.
Kratos
Comments
  • 无法找到相应想依赖包

    无法找到相应想依赖包

    go build main.go main.go:11:2: cannot find package "kratos-demo/internal/server/grpc" in any of: /usr/local/Cellar/go/1.12.4/libexec/src/kratos-demo/internal/server/grpc (from $GOROOT) /Users/sunzhenya/Work/go/src/kratos-demo/internal/server/grpc (from $GOPATH) main.go:12:2: cannot find package "kratos-demo/internal/server/http" in any of: /usr/local/Cellar/go/1.12.4/libexec/src/kratos-demo/internal/server/http (from $GOROOT) /Users/sunzhenya/Work/go/src/kratos-demo/internal/server/http (from $GOPATH) main.go:13:2: cannot find package "kratos-demo/internal/service" in any of: /usr/local/Cellar/go/1.12.4/libexec/src/kratos-demo/internal/service (from $GOROOT) /Users/sunzhenya/Work/go/src/kratos-demo/internal/service (from $GOPATH)

  • RPC 服务发现问题,帮忙看看,谢谢

    RPC 服务发现问题,帮忙看看,谢谢

    rpc 服务已注册到 discovery, 通过http://127.0.0.1:7171/discovery/polls?appid=demo.service&env=dev&hostname=LAPTOP-7S54OGTM&latest_timestamp=0 也能查到, { "code": 0, "message": "0", "ttl": 1, "data": { "demo.service": { "instances": { "zone01": [ { "region": "region01", "zone": "zone01", "env": "dev", "appid": "demo.service", "hostname": "LAPTOP-7S54OGTM", "addrs": [ "grpc://127.0.0.1:9000" ], "version": "", "metadata": null, "status": 1, "reg_timestamp": 1581261208864765800, "up_timestamp": 1581261208864765800, "renew_timestamp": 1581263278866447800, "dirty_timestamp": 1581261208864765800, "latest_timestamp": 0 } ] }, "latest_timestamp": 1 } } }

    但是在客户端在调用rpc 服务时报错 discovery://default/demo.service?subset=50&zone=zone01 error context deadline exceeded!panic: context deadline exceeded

    请问要如何解决

    客户端点用方式 const AppID = "demo.service" // NOTE: example // NewClient new member grpc client func NewClient(cfg *warden.ClientConfig, opts ...grpc.DialOption) (demoapi.DemoClient, error) { client := warden.NewClient(cfg, opts...) conn, err := client.Dial(context.Background(), "discovery://default/"+AppID) if err != nil { return nil, err } return demoapi.NewDemoClient(conn), nil }

  • feat(config): support Resolver for config variable placeholders

    feat(config): support Resolver for config variable placeholders

    using go template lib to replace placeholder to environment variable

    like this:

    http:
      server:
        # replace with env var "PORT" with default value 8080 
        port: ${PORT:8080}
        # replace with env var "TIMEOUT" without default value
        timeout: "${TIMEOUT}"
    

    please help for code reviewing @ymh199478

    thanks in advance :-)

  • [Question] context deadline exceeded

    [Question] context deadline exceeded

    version: github.com/go-kratos/kratos/v2 v2.0.0-rc6

    按教程配置kratos,并使用etcd注册发现服务。 registry.Discovery的GetService是可以获取得到服务的Endpoints信息。

    conn, err := grpc.DialInsecure(
    		context.Background(),
    		grpc.WithEndpoint("discovery:///servicename"),
    		grpc.WithDiscovery(r),
    	)
    	if err != nil {
    		panic(err)
    	}
    
    	cli := ccv1.NewConnectorClient(conn)
    	reply, err := cli.CreateConnect(context.Background(), &ccv1.CreateConnectRequest{
    		Username: "root",
    		Password: "root",
    	})
    	if err != nil {
    		log.Fatal(err)
    	}
    

    但是这样会报错: rpc error: code = DeadlineExceeded desc = context deadline exceeded

    这可能是什么原因造成?

  • ..\api\client.go:15:68: undefined: DemoClient

    ..\api\client.go:15:68: undefined: DemoClient

    环境: PS C:\src> go env set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\37773\AppData\Local\go-build set GOENV=C:\Users\37773\AppData\Roaming\go\env set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=C:\Users\37773\go set GOPRIVATE= set GOPROXY=https://goproxy.cn,direct set GOROOT=c:\go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=c:\go\pkg\tool\windows_amd64 set GCCGO=gccgo set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=NUL set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\37773\AppData\Local\Temp\go-build367084410=/tmp/go-build -gno-record-gcc-switches PS C:\src>

    安装: PS C:\src> kratos new kratos-demo go get -u github.com/bilibili/kratos/tool/kratos-gen-project go: finding golang.org/x/crypto latest go: finding golang.org/x/sys latest genproject: 安装成功! go: finding github.com/bilibili/kratos master go get -u github.com/bilibili/kratos/tool/kratos-protoc protoc: 安装成功! 2019/12/05 18:37:11 protoc --proto_path=C:\Users\37773\go/src --proto_path=C:\Users\37773\go/src/github.com/bilibili/kratos/third_party --proto_path=C:\src\kratos-demo\api --bm_out=:. api.proto google/protobuf/descriptor.proto: File not found. github.com/gogo/protobuf/gogoproto/gogo.proto:32:1: Import "google/protobuf/descriptor.proto" was not found or had errors. github.com/gogo/protobuf/gogoproto/gogo.proto:38:8: "google.protobuf.EnumOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.EnumOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.EnumOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.EnumOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.EnumOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto:46:8: "google.protobuf.EnumValueOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto:50:8: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FileOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto:92:8: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.MessageOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto:129:8: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. github.com/gogo/protobuf/gogoproto/gogo.proto: "google.protobuf.FieldOptions" is not defined. google/protobuf/empty.proto: File not found. google/api/annotations.proto:20:1: Import "google/protobuf/descriptor.proto" was not found or had errors. google/api/annotations.proto:28:8: "google.protobuf.MethodOptions" is not defined. api.proto:6:1: Import "github.com/gogo/protobuf/gogoproto/gogo.proto" was not found or had errors. api.proto:7:1: Import "google/protobuf/empty.proto" was not found or had errors. api.proto:8:1: Import "google/api/annotations.proto" was not found or had errors. api.proto:19:15: ".google.protobuf.Empty" is not defined. api.proto:19:48: ".google.protobuf.Empty" is not defined. api.proto:20:42: ".google.protobuf.Empty" is not defined. exit status 1 exit status 1 go get -u github.com/bilibili/kratos/tool/kratos-gen-bts genbts: 安装成功! Close: 无声明 忽略此方法 Ping: 无声明 忽略此方法 dao.bts.go: 生成成功 go get -u github.com/bilibili/kratos/tool/kratos-gen-mc genmc: 安装成功! mc.cache.go: 生成成功 go get -u github.com/google/wire/cmd/wire go: finding golang.org/x/tools latest wire: 安装成功! wire: C:\src\kratos-demo\internal\di\wire.go:17:65: DemoServer not declared by package api wire: generate failed exit status 1 Project: kratos-demo OnlyGRPC: false OnlyHTTP: false Directory: C:\src\kratos-demo

    项目创建成功.

    运行: $ go build kratos-demo/api ..\api\client.go:15:68: undefined: DemoClient ..\api\client.go:21:9: undefined: NewDemoClient

  • kratos安装失败

    kratos安装失败

    执行GO111MODULE=on && go get -u github.com/go-kratos/kratos/tool/kratos

    出现下面错误

    go: downloading github.com/go-kratos/kratos v0.3.3 go: extracting github.com/go-kratos/kratos v0.3.3 go get: github.com/go-kratos/[email protected]: parsing go.mod: module declares its path as: github.com/bilibili/kratos but was required as: github.com/go-kratos/kratos

    below is my go env:

    GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/Users/shiquan/Library/Caches/go-build" GOENV="/Users/shiquan/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/shiquan/.gvm/pkgsets/go1.13/global" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/Users/shiquan/.gvm/gos/go1.13" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/Users/shiquan/.gvm/gos/go1.13/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/dev/null" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_3/krxyp3x92d358gd8zxxqd8rh0000gn/T/go-build887245376=/tmp/go-build -gno-record-gcc-switches -fno-common"

  • [Feature] Maybe something like `EmitUnpopulated` should be added for custom http response encoder

    [Feature] Maybe something like `EmitUnpopulated` should be added for custom http response encoder

    What problem is the feature used to solve?

    In order to use custom response, I created a http.ResponseEncoder. However, becasue of the omitempty tag generated by protobuf, using the default golang json Marshaler will cause default values missing in the response.

    And in kratos, the json encoding implemented like this:

    func (codec) Marshal(v interface{}) ([]byte, error) {
    	if m, ok := v.(proto.Message); ok {
    		return MarshalOptions.Marshal(m)
    	}
    	return json.Marshal(v)
    }
    

    In this situation, without proto defining, it cannot use the EmitUnpopulated option. To take over this, I use the following codes to give back all the values:

    func responseEncoder(w stdHttp.ResponseWriter, r *stdHttp.Request, v interface{}) (err error) {
    	codec, _ := http.CodecForRequest(r, "Accept")
    	rawV, err := codec.Marshal(v)
    	if err != nil {
    		return err
    	}
    	rawMap := make(map[string]interface{})
    	err = codec.Unmarshal(rawV, &rawMap)
    	if err != nil {
    		return err
    	}
    
    	reply := newResponse()
    	reply.Code = 200
    	reply.Data = rawMap
    	reply.Message = "success"
    	data, err := codec.Marshal(reply)
    	if err != nil {
    		return err
    	}
    	w.Header().Set("Content-Type", contentType(codec.Name()))
    	w.WriteHeader(stdHttp.StatusOK)
    	_, err = w.Write(data)
    	if err != nil {
    		return err
    	}
    	return
    }
    

    It uses several addtional Marshal/Unmarshals, which is not elegant and low efficiency. Besides, in order to use the above code, a json_name field option has to be specified in the proto file.

    Requirements description of the feature

    Maybe something like EmitUnpopulated should be added for custom response encoder.

  • [Feature]Support ServiceRegister  ServerOption

    [Feature]Support ServiceRegister ServerOption

    What problem is the feature used to solve?

    当前的初始化顺序是:先创建Service,然后创建Server。创建Server的模块中引入所有Service模块,并将Server作为参数调用Service的Register函数向Server注册。比如:

    func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, greeter1 *service.Greeter1Service, greeter2 *service.Greeter2Service) *grpc.Server {
    	var opts = []grpc.ServerOption{
    		grpc.Middleware(
    			recovery.Recovery(),
    			tracing.Server(),
    			logging.Server(log.DefaultLogger),
    			metrics.Server(),
    			validate.Validator(),
    		),
    	}
    	srv := grpc.NewServer(opts...)
    	v1.RegisterGreeterServer(srv, greeter)
    	v1.RegisterGreeter1Server(srv, greeter1)
    	v1.RegisterGreeter2Server(srv, greeter2)
    	return srv
    }
    

    可以考虑由Server提供ServiceRegister选项,各Service提供Register注入到Server中,将Server依赖Service变为Service依赖Server。比如: server/grpc.go

    import "mydemo/internal/service"
    
    func NewGRPCServer(c *conf.Server) *grpc.Server {
    	var opts = []grpc.ServerOption{
                    grpc.ServiceRegisters(service.Registers...),
    		grpc.Middleware(
    			recovery.Recovery(),
    			tracing.Server(),
    			logging.Server(log.DefaultLogger),
    			metrics.Server(),
    			validate.Validator(),
    		),
    	}
    	srv := grpc.NewServer(opts...)
    	return srv
    }
    

    这样代码会简洁很多 而register的实现可以放到各个Service模块内。比如: service/service.go

    package "service"
    var Registers []grpc.ServiceRegister
    

    service/greeter_service.go

    package "service"
    
    func init(){
    	greeter := NewGreeterService()
    	register := grpc.RegisterFunc(func(srv *grpc.Server){
    		v1.RegisterGreeterService(srv, greeter)
    	})
    	Registers = append(Registers, register)
    }
    

    Requirements description of the feature

    References

  • [Feature] 配置文件增加对环境变量占位符的替换功能

    [Feature] 配置文件增加对环境变量占位符的替换功能

    背景和动机

    我们不希望将一些敏感的内容提交到 git 仓库中,所以这些敏感内容我们会和提交到 git 仓库的配置文件分离。

    我们将这些机密内容放在 dotenv运行时加载到环境变量 或是 直接存于环境变量 中。

    除此之外,配置可能因为程序运行环境发生改变,而需要动态的进行配置。

    所以我们希望 kratos 能够在配置文件中动态的识别环境变量占位符,并使用环境变量值替换配置的内容。

    提议的方案

    修订1:提议方案需根据 @tonybase 描述建议修正 修订2:语义占位符采用 os.Expand 解析规则完成,废弃修订1的讨论

    提案在配置文件中,通过占位符来替换环境变量中的功能:

    提案建议的占位符风格有如下几种:

    • %(VARIABLE_NAME) :传统的环境变量占位符
    • %env(VARIABLE_NAME)% : 带有环境变量处理器的占位符

    转义字符

    双倍的 %% 将会被视作转义,从而豁免 % 占位符被解析

    传统的环境变量占位符

    传统的占位符替换机制只能够替换环境变量(包含 dotenv 运行时加载)的内容,占位符采用 %() 包装变量名:

    data:
      database:
        driver: mysql
        source: %(DB_USERNAME):%(DB_PASSWORD)@tcp(127.0.0.1:3306)/test
    

    带有环境变量处理器的占位符

    环境变量处理器可以使配置程序具备更高的可扩展性,但相对会引入更高的复杂度。环境变量处理器实现可以被动态扩展。

    占位符结构中 process 可形成管道:

    %env([process:]VARIABLE_NAME)%
    %env([process-a:][process-b:]VARIABLE_NAME)%
    

    默认的处理器

    提案中提议了一些默认的处理器方案,除了默认方案外,处理器方案可自由扩展(kratos 提出一种接口规范,使开发者可以扩展处理器):

    读取环境变量

    等价于传统的环境变量占位符:

    data:
      database:
        user: %env(DB_USERNAME)%  # 等价于 %(DB_USERNAME)
    

    从文件读取内容的处理程序

    从环境变量 AUTH_FILE 的值中获取文件存储路径,并读取该文件内容作为值

    parameters:
        secrets: %env(file:AUTH_FILE)%
    

    支持 fallback 的环境变量读取

    当指定环境变量读取失败时,可以使用默认环境变量的值替代

    parameters:
        # 环境变量 PRIVATE_KEY 不存在,则使用 raw_key 值(非环境变量解析)替代
        private_key_a: %env(default:raw_key:PRIVATE_KEY)%
    
        # 环境变量 PRIVATE_KEY 不是一个有效的文件路径,使用 raw_key 值(非环境变量解析)替代
        private_key_b: %env(default:raw_key:file:PRIVATE_KEY)%
    

    补充

    • yaml 标准本身不支持变量占位符
    • 占位符格式应在不同配置格式中都具备良好的可读性
  • fix:调整consul注册写法,将gprc和http注册为不同的srvId,并通过tag区分grpc和http服务(#1690)

    fix:调整consul注册写法,将gprc和http注册为不同的srvId,并通过tag区分grpc和http服务(#1690)

    Description (what this PR does / why we need it):

    在保证接口不变的情况下,利用tag区分grpc和http服务。通过tag过滤支持spring cloud和kratos的互相服务发现

    Which issue(s) this PR fixes (resolves / be part of):

    fixes #1690

    Other special notes for reviewer:

    spring cloud 服务注册tag配置 spring.application.name=spring-cloud-consul-producer server.port=8501 spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.serviceName=helloworld spring.cloud.consul.discovery.tags=version=,scheme=http spring cloud 服务发现tag配置 spring.application.name=spring-cloud-consul-consumer server.port=8503 spring.cloud.consul.host=127.0.0.1 spring.cloud.consul.port=8500

    spring.cloud.consul.discovery.heartbeat.enabled=true spring.cloud.consul.discovery.health-check-path=/actuator/health spring.cloud.consul.discovery.default-query-tag=version=,scheme=http

  • [Question] 请教下pb error的问题

    [Question] 请教下pb error的问题

    extend google.protobuf.EnumOptions {
      int32 default_code = 1108;
    }
    
    extend google.protobuf.EnumValueOptions {
      int32 code = 1109;
    }
    

    这里的1108和1109是什么意思?为什么我改了_errors.pb.go 无法生成?

  • feat: grpc use the admin api

    feat: grpc use the admin api

    Description (what this PR does / why we need it):

    support grpcdebug https://github.com/grpc-ecosystem/grpcdebug

    Which issue(s) this PR fixes (resolves / be part of):

    Other special notes for the reviewers:

  • [Feature] http.NewClient允许支持带前缀path的endpoint

    [Feature] http.NewClient允许支持带前缀path的endpoint

    当前endpoint如果为http://127.0.0.1:8080/some_path时,NewClient实际会去掉后面的路径部分。 而在某些场景时,endpoint带路径是有必要的。如这个路径(/some_path)是由网关加出来的,此时他不应该包含于proto中路径的定义,最好的做法是通过这里的endpoint传入 conn, err := http.NewClient( context.Background(), http.WithEndpoint("http://127.0.0.1:8080/some_path"), )

  • [Proposal] export RegisterXxxHTTPRouter method

    [Proposal] export RegisterXxxHTTPRouter method

    Proposal description

    Related to #2593

    Implementation code

    # file: cmd/protoc-gen-go-http/template.go
    # line: 23
    
    func Register{{.ServiceType}}HTTPServer(s *http.Server, srv {{.ServiceType}}HTTPServer) {
    	r := s.Route("/")
    + 	Register{{.ServiceType}}HTTPRouter(r, srv)
    + }
    + 
    + func Register{{.ServiceType}}HTTPRouter(r *http.Router, srv {{.ServiceType}}HTTPServer) {
    	{{- range .Methods}}
    	r.{{.Method}}("{{.Path}}", _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv))
    	{{- end}}
    }
    

    Usage demonstration

    srv := http.NewServer(opts...)
    r := srv.Route("/prefix", middlewares...)
    v1.RegisterXxxServiceHTTPRouter(r, service)
    
  • feat: support polaris service governance

    feat: support polaris service governance

    Description (what this PR does / why we need it):

    Which issue(s) this PR fixes (resolves / be part of):

    Other special notes for the reviewers:

  • build(deps): bump github.com/prometheus/common from 0.37.0 to 0.39.0 in /contrib/metrics/prometheus

    build(deps): bump github.com/prometheus/common from 0.37.0 to 0.39.0 in /contrib/metrics/prometheus

    Bumps github.com/prometheus/common from 0.37.0 to 0.39.0.

    Release notes

    Sourced from github.com/prometheus/common's releases.

    v0.39.0

    • [ENHANCEMENT] Add support for proxy connect headers. #409
    • [ENHANCEMENT] Add platform info to labels. #403

    v0.37.1

    • [BUGFIX] Update go.mod for CVE-2022-41717 #420

    v0.38.0

    • [FEATURE] Implement Stringer on TLSVersion (#405)
    • [FEATURE] Check if TLS certificate and key file have been modified (#345)
    • [ENHANCEMENT] Add the ability to specify the maximum acceptable TLS version (#414)
    • [ENHANCEMENT] Make LoadHTTPConfigFile set directory and move from tests file (#415)
    • [ENHANCEMENT] Get Revision from debug.BuildInfo if not explicitly set (#374)
    Commits
    • 296ec92 Merge pull request #409 from prometheus/mem/proxy_header
    • 18281a2 Merge pull request #424 from prometheus/repo_sync
    • 4a0d730 Add support for proxy connect headers
    • 017dec0 Update common Prometheus files
    • befeabf Merge pull request #422 from prometheus/superq/add_mod_check
    • 1bc7f65 Add platform info to labels (#403)
    • 82accf3 Add go mod version test
    • 00e3fd7 Merge pull request #418 from roidelapluie/go119
    • 045094f Update deps and test with go 1.19
    • ddb642f Merge pull request #421 from prometheus/superq/update_sigv4
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Rpcx-framework - An RPC microservices framework based on rpcx, simple and easy to use, ultra fast and efficient, powerful, service discovery, service governance, service layering, version control, routing label registration.

RPCX Framework An RPC microservices framework based on rpcx. Features: simple and easy to use, ultra fast and efficient, powerful, service discovery,

Jan 5, 2022
Authentication-microservice - Microservice for user authentication built with golang and gRPC

Authentication-microservice - Microservice for user authentication built with golang and gRPC

May 30, 2022
Microservice - Microservice golang & nodejs
Microservice - Microservice golang & nodejs

Microservice Gabungan service dari bahasa pemograman go, nodejs Demo API ms-auth

May 21, 2022
Customer-microservice - Microservice of customer built with golang and gRPC

?? Building microservices to manage customer data using Go and gRPC Command to g

Sep 8, 2022
Microservice - A sample architecture of a microservice in go

#microservice Folder structure required. service certs config config.yaml loggin

Feb 3, 2022
Modern microservice web framework of golang
Modern microservice web framework of golang

gogo gogo is an open source, high performance RESTful api framework for the Golang programming language. It also support RPC api, which is similar to

May 23, 2022
Kitex byte-dance internal Golang microservice RPC framework with high performance and strong scalability, customized extensions for byte internal.
Kitex byte-dance internal Golang microservice RPC framework with high performance and strong scalability, customized extensions for byte internal.

Kitex 字节跳动内部的 Golang 微服务 RPC 框架,具有高性能、强可扩展的特点,针对字节内部做了定制扩展。

Jan 9, 2023
a microservice framework for rapid development of micro services in Go with rich eco-system
a microservice framework for rapid development of micro services in Go with rich eco-system

中文版README Go-Chassis is a microservice framework for rapid development of microservices in Go. it focus on helping developer to deliver cloud native a

Dec 27, 2022
Microservice Boilerplate for Golang with gRPC and RESTful API. Multiple database and client supported
Microservice Boilerplate for Golang with gRPC and RESTful API. Multiple database and client supported

Go Microservice Starter A boilerplate for flexible Go microservice. Table of contents Features Installation Todo List Folder Structures Features: Mult

Jul 28, 2022
A microservice gateway developed based on golang.With a variety of plug-ins which can be expanded by itself, plug and play. what's more,it can quickly help enterprises manage API services and improve the stability and security of API services.
A microservice gateway developed based on golang.With a variety of plug-ins which can be expanded by itself, plug and play. what's more,it can quickly help enterprises manage API services and improve the stability and security of API services.

Goku API gateway is a microservice gateway developed based on golang. It can achieve the purposes of high-performance HTTP API forwarding, multi tenant management, API access control, etc. it has a powerful custom plug-in system, which can be expanded by itself, and can quickly help enterprises manage API services and improve the stability and security of API services.

Dec 29, 2022
Trying to build an Ecommerce Microservice in Golang and Will try to make it Cloud Native - Learning Example extending the project of Nic Jackson

Golang Server Project Best Practices Dependency Injection :- In simple words, we want our functions and packages to receive the objects they depend on

Nov 28, 2022
Golang Caching Microservice (bequant.io test project to get hired)

bequant test project How to use: Simply type docker-compose up -d --build db warden distributor in terminal while in project's directory. MySQL, Warde

May 14, 2022
Microservice to manager users with golang

User Service Microservice to manager users. Assumptions & Limitations This API a

Dec 27, 2021
Golang Microservice making use of protobuf and gRPC as the underlying transport protocol.

Go-Microservices Golang Microservice making use of protobuf and gRPC as the underlying transport protocol. I will be building a generic microservice,

Jan 5, 2022
Go-rifa-microservice - Clean Architecture template for Golang services
Go-rifa-microservice - Clean Architecture template for Golang services

Test CI Go Clean template Clean Architecture template for Golang services Overvi

Sep 22, 2022
This project implements p11-kit RPC server protocol, allowing Go programs to act as a PKCS #11 module without the need for cgo

PKCS #11 modules in Go without cgo This project implements p11-kit RPC server protocol, allowing Go programs to act as a PKCS #11 module without the n

Nov 30, 2022
Go gRPC RabbitMQ email microservice

Go, RabbitMQ and gRPC Clean Architecture microservice ?? ??‍?? Full list what has been used: GRPC - gRPC RabbitMQ - RabbitMQ sqlx - Extensions to data

Dec 29, 2022
A Microservice Toolkit from The New York Times
A Microservice Toolkit from The New York Times

Gizmo Microservice Toolkit This toolkit provides packages to put together server and pubsub daemons with the following features: Standardized configur

Jan 7, 2023
Go products microservice

Golang Kafka gRPC MongoDB microservice example ?? ??‍?? Full list what has been used: Kafka - Kafka library in Go gRPC - gRPC echo - Web framework vip

Dec 28, 2022