braid

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: MIT Imports: 7 Imported by: 0

README

Braid


Go Report Card CI Coverage Status

中文

Intro

Description of Service Node Module RPC Pub-sub

image.png


  • RPC Client/Server - Used for request / response from service to service
  • Pub-sub - Used to publish & subscribe messages from module to module
  • Discover - Automatic service discovery, and broadcast the node's entry, exit, update and other messages
  • Balancer - Client side load balancing which built on service discovery. Provide smooth weighted round-robin balancing by default
  • Elector - Select a unique master node for the same name service
  • Tracer - Distributed tracing system, used to monitor the internal state of the program running in microservices
  • Linkcache - Link cache used to maintain connection information in distributed systems

Quick start

b, _ := NewService(
	"service-name",
	"service-id",
	&components.DefaultDirector{
		Opts: &components.DirectorOpts{
			ClientOpts: []grpcclient.Option{
				grpcclient.AppendUnaryInterceptors(grpc_prometheus.UnaryClientInterceptor),
			},
			ServerOpts: []grpcserver.Option{
				grpcserver.WithListen(":14222"),
				grpcserver.AppendUnaryInterceptors(grpc_prometheus.UnaryServerInterceptor),
				grpcserver.RegisterHandler(func(srv *grpc.Server) {
					// register grpc handler
				}),
			},
			ElectorOpts: []electork8s.Option{
				electork8s.WithRefreshTick(time.Second * 5),
			},
			LinkcacheOpts: []linkcacheredis.Option{
				linkcacheredis.WithMode(linkcacheredis.LinkerRedisModeLocal),
			},
		},
	},
)

b.Init()
b.Run()
b.Close()
  • Rpc
err := braid.Send(
	ctx,
	"login", // target service name
	"/user.password", // methon
	"token", // (optional
	body,
	res,
)
  • Pub
braid.Topic(meta.TopicLinkcacheUnlink).Pub(ctx, &meta.Message(Body : []byte("usertoken")))
  • Sub
lc, _ := braid.Topic(meta.TopicElectionChangeState).Sub(ctx, "serviceid")
defer lc.Close()

lc.Arrived(func(msg *meta.Message) error { 
	
	scm := meta.DecodeStateChangeMsg(msg)
	if scm.State == elector.EMaster {
		// todo ...
	}

	return nil
})

Rpc Benchmark

Pubsub Benchmark
$ go test -benchmem -run=^$ -bench ^BenchmarkPubsub -cpu 2,4,8
cpu: 2.2 GHz 2.5
goos: darwin
goarch: amd64
pkg: github.com/pojol/braid-go/components/pubsubredis
BenchmarkPubsub-2   1959            724452 ns/op            7254 B/op        193 allocs/op
BenchmarkPubsub-4	2506            525298 ns/op            7313 B/op        194 allocs/op
BenchmarkPubsub-8	4233            282358 ns/op            3853 B/op        103 allocs/op
PASS

Documentation

Index

Constants

View Source
const (
	// Version of braid-go
	Version = "v1.4.0"
)

Variables

View Source
var (
	ErrTypeConvFailed = errors.New("type conversion failed")
)

Functions

func Send added in v1.4.0

func Send(ctx context.Context, target, methon, token string,
	args, reply interface{},
	opts ...interface{}) error

Send 发送rpc请求

target 目标服务名称
methon 目标服务方法
token  用户的唯一标识id
args   请求参数
reply  返回参数
opts   rpc调用选项

func Topic added in v1.4.0

func Topic(name string) module.ITopic

Topic 获取或创建一个pubsub消息主题

Types

type Braid

type Braid struct {
	// contains filtered or unexported fields
}

Braid framework instance

func NewService added in v1.2.25

func NewService(name string, id string, director components.IDirector) (*Braid, error)

NewService - 创建一个新的 braid 服务

name 服务名称
id   服务id (唯一标识
director 服务组件构建器

func (*Braid) Close

func (b *Braid) Close()

Close 关闭braid

func (*Braid) Init

func (b *Braid) Init() error

Init braid init

func (*Braid) Run

func (b *Braid) Run()

Run 运行braid

Directories

Path Synopsis
depends/btracer
实现文件 jaegertracing 基于 jaeger 实现的分布式追踪服务
实现文件 jaegertracing 基于 jaeger 实现的分布式追踪服务
discoverconsul
实现文件 基于 consul 实现的服务发现
实现文件 基于 consul 实现的服务发现
electorconsul
实现文件 electorconsul 基于 consul 实现的选举
实现文件 electorconsul 基于 consul 实现的选举
internal/balancer
接口文件 balancer 负载均衡
接口文件 balancer 负载均衡
linkcacheredis
实现文件 linkerredis 基于 redis 实现的链路缓存
实现文件 linkerredis 基于 redis 实现的链路缓存
rpcgrpc/grpcclient
实现文件 基于 grpc 实现的 rpc-client
实现文件 基于 grpc 实现的 rpc-client
rpcgrpc/grpcserver
实现文件 grpcserver 基于 grpc 实现的 rpc-server
实现文件 grpcserver 基于 grpc 实现的 rpc-server
rpc-client 模块接口文件
rpc-client 模块接口文件

Jump to

Keyboard shortcuts

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