gokit

package
v0.0.1-202202171822 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2022 License: MPL-2.0 Imports: 26 Imported by: 0

README

MicroService

Create and run microservice with help of hdsdk and hdkit

It provides out-of-the-box GRPC and HTTP functionality with only small piece of config.

Table of Contents

Usage

Use hdkit create project boilerplate

Please refer to hdkit to create project boilerplate

Directory autogen are files generated automatically each time, all files under this directory will be overwritten if run hdkit again

Config

[sdk.service]
   [sdk.service.default]
       name = "testservice"
       [[sdk.service.default.clients]]
           transport = "grpc"
           address = "0.0.0.0:12345"
           middlewares=["circuitbreak", "ratelimit"]
       [[sdk.service.default.servers]]
           transport = "grpc"
           address = "0.0.0.0:12345"
           middlewares=["circuitbreak", "ratelimit"]
       [[sdk.service.default.servers]]
           transport = "http"
           address = "0.0.0.0:23456"
           middlewares=["trace", "circuitbreak", "ratelimit"]
           ...
   [[sdk.service.items]]
       name = "httpservice"
       [[sdk.service.items.servers]]
           address = "0.0.0.0:12345"
           middlewares=["circuitbreak", "ratelimit"]
           ...        

There are two kinds of service config here:

  • default: default service, it can be get by sdk.MicroService.My()
  • items: service identified by name, it can be get by sdk.MicroService.By(name)

One service could have multiple server, like: grpc, http, please make sure s exists in servers Except default config, other items config must be [[ and ]] wrapped

middleware trace config

trace config can be ignored, which will be used default trace config instead

[sdk.service]
	[[sdk.service.items]]
		name = "testservice"
	    [sdk.service.items.trace]
	      url = "http://localhost:9411/api/v2/spans"
	      address = "localhost:80"	
  • url: zipkin http report url
  • address: open tracer address
middleware circuit break config

circuitbreak config can be ignored, which will be used default circuit break config instead

[sdk.service]
	[[sdk.service.items]]
		name = "testservice"
	    [sdk.service.items.circuitbreak]
	        requests = 10,
	        interval = 0
		    timeout = 60
		    max_requests =  100
		    failure_ratio = 1.0
  • requests: successive requests
  • interval: what's the period it reset timeout counter when in shutdown status
  • timeout: time between half open status to open status
  • max_requests: when in half open status, if max requests is 0, only one request can be allowed
  • failure_ratio: request failure rate
middleware rate limit config

ratelimit config can be ignored, which will be used default rate limit config instead

[sdk.service]
	[[sdk.service.items]]
		name = "testservice"
	    [sdk.service.items.ratelimit]
	        limit = 30
		    burst = 50
  • limit: how many requests allowed in one second
  • burst: max burst requests
server config
[sdk.service]
	[[sdk.service.items]]
		name = "testservice"
    [[sdk.service.items.servers]]
	    type = "http"
	    address = "0.0.0.0:12345"
		middlewares=["trace", "circuitbreak", "ratelimit"]
  • type: what's the server type to serve the service, now supports: grpc and http. If not specified this item, then grpc taken as default

  • address: what's the server listen address

  • middlewares: service middlewares, now it supports:

    • trace
    • circuitbreak
    • ratelimit

    Note: if specified middleware type here, then it inherits the config from above

http client

It can access http transport by using following url:

/<service name>/<method name> all use snake case format

The parameters sent by json body, it supports GET and POST

Example

svc := &grpc.SearchServiceImpl{}
manager := hdsdk.MicroService.By("testservice").NewGrpcServerManager()

endpoints := &grpc.GrpcEndpoints{
	SearchEndpoint: manager.CreateHandler(svc, &grpc.SearchHandler{}),
	HelloEndpoint:  manager.CreateHandler(svc, &grpc.HelloHandler{}),
}

pb.RegisterSearchServiceServer(manager.GetServer(), endpoints)

err = hdsdk.Initialize(&conf)
if err != nil {
    utils.Fatal("sdk initialize", "err", err)
}

var group parallel.Group
group.Add(
    func() error {
        return manager.RunServer()
    },
    func(err error) {
        manager.Close()
    },
)

err := group.Run()
if err != nil {
  utils.Fatal("grpc server exist", "err", err)
}

More details please refer to microservice example

Documentation

Overview

Package gokit

Package gokit microservice implemented by gokit @Author Ryan Fan 2021-07-27 @Update Ryan Fan 2021-07-27

Index

Constants

View Source
const (
	MDW_TRACER       = "trace"
	MDW_CIRCUITBREAK = "circuitbreak"
	MDW_RATELIMIT    = "ratelimit"
)
View Source
const (
	GRPC = "grpc"
	HTTP = "http"
)

transport type

Variables

View Source
var (
	ErrInvalidTraceConfig = errors.New("invalid trace config")
)

Functions

func NewMicroService

func NewMicroService(logger types.LogProvider, config *MicroServiceConfig) (types.MicroService, error)

Types

type BaseGokitClient

type BaseGokitClient struct {
	Logger      types.LogProvider
	Config      *ClientConfig
	Middlewares []*MsMiddleware
}

type BaseServerManager

type BaseServerManager struct {
	Name        string
	Config      *ServerConfig
	Logger      types.LogProvider
	Middlewares []*MsMiddleware
	// contains filtered or unexported fields
}

func (*BaseServerManager) Close

func (s *BaseServerManager) Close()

Close 关闭GrpcServer

type CircuitBreakConfig

type CircuitBreakConfig struct {
	MaxRequests  uint32  `mapstructure:"max"`           // 半开后最大允许通过的请求,如果最大请求为0,熔断器值允许一个请求通过
	Interval     int     `mapstructure:"interval"`      // 熔断器在关闭状态周期清除内部计数器的间隔,如果为0,则在关闭状态不清除计数器
	Timeout      int     `mapstructure:"timeout"`       // 在熔断器半开后进入开放状态的时间,如果为0,则默认设置为60秒
	Requests     uint32  `mapstructure:"requests"`      // 连续请求数量
	FailureRatio float64 `mapstructure:"failure_ratio"` // 请求失败率
}

CircuitBreakConfig 服务熔断配置

type ClientConfig

type ClientConfig struct {
	Transport   string   `mapstructure:"transport"`
	Address     string   `mapstructure:"address"`
	Middlewares []string `mapstructure:"middlewares"`
}

ClientConfig 客户端配置

type GokitGrpcServerManager

type GokitGrpcServerManager struct {
	BaseServerManager

	Options []kitgrpc.ServerOption
	// contains filtered or unexported fields
}

func (*GokitGrpcServerManager) CreateHandler

func (s *GokitGrpcServerManager) CreateHandler(concreteService interface{}, ep types.GrpcAspect) *kitgrpc.Server

CreateHandler 创建Grpc Transport的handler

func (*GokitGrpcServerManager) GetServer

func (s *GokitGrpcServerManager) GetServer() *grpc.Server

func (*GokitGrpcServerManager) RunServer

func (s *GokitGrpcServerManager) RunServer() error

RunServer 运行GrpcServer

type GokitHttpServer

type GokitHttpServer struct {
	BaseServerManager
	Options []kithttp.ServerOption
}

func (*GokitHttpServer) CreateHandler

func (s *GokitHttpServer) CreateHandler(concreteService interface{}, ap types.HttpAspect) http.Handler

CreateHandler 创建Http Transport的Handler

func (*GokitHttpServer) RunServer

func (s *GokitHttpServer) RunServer(handlers map[string]http.Handler) error

RunServer 运行GrpcServer

type GokitProvider

type GokitProvider struct {
	ms.BaseMsProvider
}

func (*GokitProvider) Init

func (gp *GokitProvider) Init(rootConfiger types.Configer, logger types.LogProvider, args ...interface{}) error

Init implements types.Provider interface, used to initialize the capability @author Ryan Fan (2021-06-09) @param types.Configer root config interface to extract config info @return error

type GokitProviderConfig

type GokitProviderConfig struct {
	Default *MicroServiceConfig   `mapstructure:"default"`
	Items   []*MicroServiceConfig `mapstructure:"items"`
}

type GrpcClientManager

type GrpcClientManager struct {
	*BaseGokitClient
	Options []kitgrpc.ClientOption
}

func (GrpcClientManager) CreateConnection

func (cm GrpcClientManager) CreateConnection(args ...grpc.DialOption) (*grpc.ClientConn, error)

func (GrpcClientManager) CreateEndpoint

func (cm GrpcClientManager) CreateEndpoint(conn *grpc.ClientConn, ap types.GrpcAspect) endpoint.Endpoint

type InjectFunction

type InjectFunction func(logger types.LogProvider, endpointName string) (clientOptions []interface{}, serverOptions []interface{})

type MicroServiceConfig

type MicroServiceConfig struct {
	Name string `mapstructure:"name"`
	// middlewares
	Trace        *TraceConfig        `mapstructure:"trace"`        // 链路追踪
	CircuitBreak *CircuitBreakConfig `mapstructure:"circuitbreak"` // 熔断
	RateLimit    *RateLimitConfig    `mapstructure:"ratelimit"`    // 限流
	// clients and servers
	Clients []*ClientConfig `mapstructure:"clients"`
	Servers []*ServerConfig `mapstructure:"servers"`
}

MicroServiceConfig 微服务配置

type MicroServiceImpl

type MicroServiceImpl struct {
	Name   string
	Logger types.LogProvider
	Config *MicroServiceConfig
}

func (MicroServiceImpl) GetClientConfig

func (msi MicroServiceImpl) GetClientConfig(transport string) *ClientConfig

func (MicroServiceImpl) GetServerConfig

func (msi MicroServiceImpl) GetServerConfig(transport string) *ServerConfig

func (MicroServiceImpl) NewGrpcClientManager

func (msi MicroServiceImpl) NewGrpcClientManager() types.GrpcClientManager

NewGrpcClientManager 创建微服务client manager

func (MicroServiceImpl) NewGrpcServerManager

func (msi MicroServiceImpl) NewGrpcServerManager() types.GrpcServerManager

NewGrpcServerManager 创建微服务server manager

func (MicroServiceImpl) NewHttpServerManager

func (msi MicroServiceImpl) NewHttpServerManager() types.HttpServerManager

NewHttpServerManager 创建微服务server

type MsMiddleware

type MsMiddleware struct {
	Middleware      endpoint.Middleware
	InjectFunctions map[string]InjectFunction
}

MsMiddleware 因为有的中间件是处理的时候需要做一些动作, 有的中间件是需要在ClientOption或者serverOption前做一些动作 我们将不同类型的中间件抽象出来成未MsMiddleware

func NewMdwCircuitBreak

func NewMdwCircuitBreak(config *MicroServiceConfig) *MsMiddleware

NewMdwCircuitBreak 服务熔断

func NewMdwRateLimit

func NewMdwRateLimit(config *MicroServiceConfig) *MsMiddleware

NewMdwRateLimit 服务限流, limited to 1 request per second with burst of 100 requests. Note, rate is defined as a number of requests per second.

func NewMdwTrace

func NewMdwTrace(config *MicroServiceConfig) *MsMiddleware

NewMdwTrace returns an endpoint.Middleware that acts as a tracer. Requests that would exceed the maximum request rate are simply rejected with an error.

type NewMiddlewareFunc

type NewMiddlewareFunc func(config *MicroServiceConfig) *MsMiddleware

type RateLimitConfig

type RateLimitConfig struct {
	Limit float64 `mapstructure:"limit"` // 每秒允许多少个事件
	Burst int     `mapstructure:"burst"` // 突发最多允许多少个
}

RateLimitConfig 服务限流选项

type ServerConfig

type ServerConfig struct {
	Transport   string   `mapstructure:"transport"`
	Address     string   `mapstructure:"address"`
	Middlewares []string `mapstructure:"middlewares"`
}

ServerConfig 服务端配置

type TraceConfig

type TraceConfig struct {
	Url     string `mapstructure:"url"` // Zipkin tracing HTTP reporter URL
	Address string `mapstructure:"address"`
}

TraceConfig 服务跟踪选项

type Tracer

type Tracer struct {
	Reporter     reporter.Reporter
	ZipkinTracer *zipkin.Tracer     // zipkin tracer
	OpenTracer   opentracing.Tracer // opentracing tracer
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL