Skip to content

pojol/block-micro

Repository files navigation

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