uriroute

package
v1.3.93-0...-ab5ed69 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 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)

FDataParsing 指定数据解析函数 返回值如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)

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

type Option

type Option func(*URIRoute)

Option Option

func CallTimeOut

func CallTimeOut(t time.Duration) Option

CallTimeOut CallTimeOut

func DataParsing

func DataParsing(t FDataParsing) Option

DataParsing DataParsing

func Selector

func Selector(t FSelector) Option

Selector Selector

type URIRoute

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

URIRoute URIRoute

func NewURIRoute

func NewURIRoute(module module.RPCModule, opts ...Option) *URIRoute

NewURIRoute NewURIRoute

func (*URIRoute) OnRoute

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

OnRoute OnRoute

Jump to

Keyboard shortcuts

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