braid

package module
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 14 Imported by: 0

README

Braid

Braid 提供统一的交互模型,通过注册模块(支持自定义),构建属于自己的微服务。


Go Report Card drone codecov

交互模型

braid.Mailbox 统一的交互模型

共享(多个消息副本 竞争(只被消费一次 进程内 集群内 发布 订阅
Shared Competition Proc Cluster Pub Sub

范例 发布订阅消息

// 订阅一个信道`topic` 这个信道在进程(Proc 内广播(Shared
consumer := braid.Mailbox().Sub(mailbox.Proc, topic).Shared()

// 收取消息
consumer.OnArrived(func(msg mailbox.Message) error {
  return nil
})

// 发送(串行
err := braid.Mailbox().Pub("topic", message)
// 发送(并行
braid.Mailbox().PubAsync("topic", message)

范例 发起一次rpc请求

// ctx 用于分布式追踪,存储调用链路的上下文信息
// target 目标服务节点 例("mail")
// methon 目标节点支持的方法 例("api.mail/send")
// token 调用者的唯一凭据(用于链路缓存
// args 输入参数
// reply 回复参数
// opts 调用的额外参数选项
err := braid.GetClient().Invoke(ctx, target, methon, token, args, reply, opts...)
if err != nil {
  // todo ...
}

微服务

braid.Module 默认提供的微服务组件

Discover Balancer Elector RPC Tracer LinkCache
服务发现 负载均衡 选举 RPC 分布式追踪 链路缓存
discoverconsul balancerrandom electorconsul grpc-client jaegertracer linkerredis
balancerswrr electork8s grpc-server

构建

通过注册模块(braid.Module),构建braid的运行环境。

b, _ := braid.New(ServiceName)

// 将模块注册到braid
b.RegistModule(
  braid.Discover(         // Discover 模块
    discoverconsul.Name,  // 模块名(基于consul实现的discover模块,通过模块名可以获取到模块的构建器
    discoverconsul.WithConsulAddr(consulAddr)), // 模块的可选项
  braid.Client(grpcclient.Name),
  braid.Elector(
    electorconsul.Name,
    electorconsul.WithConsulAddr(consulAddr),
  ),
  braid.LinkCache(linkerredis.Name),
  braid.Tracing(
    jaegertracing.Name,
    jaegertracing.WithHTTP(jaegerAddr), 
    jaegertracing.WithProbabilistic(0.01)))

b.Init()  // 初始化注册在braid中的模块
b.Run()   // 运行
defer b.Close() // 释放
Wiki

Wiki 中提供了一个较为详细的Guide,用于帮助用户更加全面的理解braid的设计理念

https://github.com/pojol/braid/wiki

Sample

https://github.com/pojol/braidgo-sample

Web
  • 流向图

用于监控链路上的连接数以及分布情况

$ docker pull braidgo/sankey:latest
$ docker run -d -p 8888:8888/tcp braidgo/sankey:latest \
    -consul http://172.17.0.1:8500 \
    -redis redis://172.17.0.1:6379/0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetClient added in v1.2.10

func GetClient() client.IClient

GetClient get client interface

func GetServer added in v1.2.7

func GetServer() interface{}

GetServer iserver.server

func Mailbox added in v1.2.2

func Mailbox() mailbox.IMailbox

Mailbox pub-sub

func Tracer

func Tracer() tracer.ITracer

Tracer tracing

Types

type Braid added in v1.1.3

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

Braid framework instance

func New added in v1.1.7

func New(name string, mailboxOpts ...interface{}) (*Braid, error)

New 构建braid

func (*Braid) Close added in v1.1.7

func (b *Braid) Close()

Close 关闭braid

func (*Braid) Init added in v1.2.5

func (b *Braid) Init()

Init braid init

func (*Braid) RegistModule added in v1.2.6

func (b *Braid) RegistModule(modules ...Module) error

RegistModule 注册模块

func (*Braid) Run added in v1.1.7

func (b *Braid) Run()

Run 运行braid

type Module added in v1.2.6

type Module func(*Braid)

Module wraps

func Client added in v1.1.7

func Client(builderName string, opts ...interface{}) Module

Client rpc-client

func Discover

func Discover(builderName string, opts ...interface{}) Module

Discover 服务发现模块,提供以下异步消息

discover.AddService 发现新的服务
discover.RmvService 某个服务下线
discover.UpdateService 更新某个服务的权重信息

func Elector added in v1.2.2

func Elector(builderName string, opts ...interface{}) Module

Elector 选举模块

elector.StateChange 状态改变消息 Wait, Slave, Master

func LinkCache added in v1.2.2

func LinkCache(builderName string, opts ...interface{}) Module

LinkCache 链路缓存模块,提供以下异步消息

linkcache.ServiceLinkNum 获得服务当前的链路数量

func Server added in v1.1.7

func Server(builderName string, opts ...interface{}) Module

Server rpc-server

func Tracing added in v1.2.7

func Tracing(builderName string, opts ...interface{}) Module

Tracing 分布式追踪模块

Jump to

Keyboard shortcuts

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