uriRoute

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

README

topic uri路由器

mqant gate网关的默认路由规则为

[moduleType@moduleID]/[handler]/[msgid]

但这个规则不太灵活,因此设计了一套基于URI规则的topic路由规则。

基于uri协议的路由规则

<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<fragment>
  1. 可以充分利用uri公共库
  2. 资源划分更加清晰明确

示例

见 <IM通信协议.md>

如何启用模块

创建一个UriRoute结构体

route:=uriRoute.NewUriRoute(this,
    uriRoute.Selector(func(topic string, u *url.URL) (s module.ServerSession, err error) {
        moduleType:=u.Scheme
        nodeId:=u.Hostname()
        //使用自己的
        if nodeId=="modulus"{
            //取模
        }else if nodeId=="cache"{
            //缓存
        }else if nodeId=="random"{
            //随机
        }else{
            //
            //指定节点规则就是 module://[user:pass@]nodeId/path
            //方式1
            // moduleType=fmt.Sprintf("%v@%v",moduleType,u.Hostname())
            //方式2
            return this.GetRouteServer(moduleType,selector.WithFilter(selector.FilterEndpoint(nodeId)))
        }
        return this.GetRouteServer(moduleType)
    }),
    uriRoute.DataParsing(func(topic string, u *url.URL, msg []byte) (bean interface{}, err error) {
        //根据topic解析msg为指定的结构体
        //结构体必须满足mqant的参数传递标准
        //例如mqrpc.Marshaler
        //type Marshaler interface {
        //	Marshal() ([]byte, error)
        //	Unmarshal([]byte) error
        //	String() string
        //}
        return
    }),
    uriRoute.CallTimeOut(3*time.Second),
)

替换默认的gate路由规则

this.Gate.OnInit(this, app, settings,
    gate.SetRouteHandler(route),
    ...
)

完结

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FDataParsing

type FDataParsing func(topic string, u *url.URL, msg []byte) (bean interface{}, err error)

* 指定数据解析函数 返回值如bean!=nil err==nil则会向后端模块传入 func(session,bean)(result, error) 否则使用json或[]byte传参

type FSelector

type FSelector func(topic string, u *url.URL) (s module.ServerSession, err error)

* 服务节点选择函数,可以自定义服务筛选规则 如不指定,默认使用 Scheme作为moduleType,Hostname作为服务节点nodeId 如随机到服务节点Hostname可以用modulus,cache,random等通用规则 例如:

im://modulus/remove_feeds_member?msg_id=1002

type Option

type Option func(*UriRoute)

func CallTimeOut

func CallTimeOut(t time.Duration) Option

func DataParsing

func DataParsing(t FDataParsing) Option

func Selector

func Selector(t FSelector) Option

type UriRoute

type UriRoute struct {
	Selector    FSelector
	DataParsing FDataParsing
	CallTimeOut time.Duration
	// contains filtered or unexported fields
}

func NewUriRoute

func NewUriRoute(module module.RPCModule, opts ...Option) *UriRoute

func (*UriRoute) OnRoute

func (this *UriRoute) OnRoute(session gate.Session, topic string, msg []byte) (bool, interface{}, error)

Jump to

Keyboard shortcuts

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