openedge-hub模块启动源码浅析——百度BIE边缘侧openedge项目源码阅读(1)

前言

因为最近项目需要用到边缘计算,结合百度的openedge进行开发,openedge目前主要功能为结合docker容器实现边缘计算,具体内容官网很多,其架构中,openedge-hub作为所有模块的通信中心节点(消息的接收和转发)是非常重要的,本篇主要介绍一下openedge-hub模块的启动以及在QOS=0的情况下消息的发送和转发,本文主要是为了记录下思路方便后续改造,因个人水平有限,对于MQTT了解有限,很多名词使用不当,中间若有错误劳烦告知,谢谢!

openedge-hub的启动

openedge-hub的开始的节点是在openedge/openedge-hub/main.go文件进行的,main函数如下所示:

	openedge.Run(func(ctx openedge.Context) error {
		m := mo{log: ctx.Log()}
		defer m.close()
		err := m.start()
		if err != nil {
			return err
		}
		ctx.Wait()
		return nil
	})

其作用主要是启动mo对象,mo结构如下:

type mo struct {
	cfg      config.Config
	Rules    *rule.Manager
	Sessions *session.Manager
	broker   *broker.Broker
	servers  *server.Manager
	factory  *persist.Factory
	log      logger.Logger
}
  1. cfg是读取配置文件后保存配置的实体。
  2. Rules是消息转发使得“路由器”。
  3. Sessions保存着所有客户端的连接。
  4. broker中间带有channel,用于接收来自session发送的消息,并通过这个channel发送至路由器(这里说的有点欠妥,后面会讲到的),路由器找到对应的session,并将消息发送到session对应的客户端。
  5. factory是用于进行持久化的,暂不做解析。
  6. log进行日志的记录,暂不做解析。

刚刚main文件中有一行代码是:err := m.start(),这个就是启动的入口,接下来看一下这个start方法:

func (m *mo) start() error {
	err := utils.LoadYAML(openedge.DefaultConfFile, &m.cfg)
	if err != nil {
		m.log.Errorln("failed to load config:", err.Error())
		return err
	}
	m.factory, err = persist.NewFactory(m.cfg.Storage.Dir)
	if err != nil {
		m.log.Errorln("failed to new factory:", err.Error())
		return err
	}
	m.broker, err = broker.NewBroker(&m.cfg, m.factory)
	if err != nil {
		m.log.Errorln("failed to new broker:", err.Error())
		return err
	}
	m.Rules, err = rule.NewManager(m.cfg.Subscriptions, m.broker)
	if err != nil {
		m.log.Errorln("failed to new rule manager:", err.Error())
		return err
	}
	m.Sessions, err = session.NewManager(&m.cfg, m.broker.Flow, m.Rules, m.factory)
	if err != nil {
		m.log.Errorln("failed to new session manager:", err.Error())
		return err
	}
	m.servers, err = server.NewManager(m.cfg.Listen, m.cfg.Certificate, m.Sessions.Handle)
	if err != nil {
		m.log.Errorln("failed to new server manager:", err.Error())
		return err
	}
	m.Rules.Start()
	m.servers.Start()
	return nil
}

总体上就是分别对mo的各个属性进行初始化,下面分别对每一个属性的初始化进行解析。

cfg加载

cfg加载在main.go中start方法的代码如下:

err := utils.LoadYAML(openedge.DefaultConfFile, &m.cfg)
if err != nil {
	m.log.Errorln("failed to load config:", err.Error())
	return err
}

其实就是把yaml对应的属性填充到mo的cfg中,下面介绍一下cfg这个结构体的一些配置项:

// Config all config of edge
type Config struct {
	Listen      []string          `yaml:"listen" json:"listen"`
	Certificate utils.Certificate `yaml:"certificate" json:"certificate"`

	Principals    []Principal    `yaml:"principals" json:"principals" validate:"principals"`
	Subscriptions []Subscription `yaml:"subscriptions" json:"subscriptions" validate:"subscriptions"`

	Message Message `yaml:"message" json:"message"`
	Status  struct {
		Logging struct {
			Enable   bool          `yaml:"enable" json:"enable"`
			Interval time.Duration `yaml:"interval" json:"interval" default:"1m"`
		} `yaml:"logging" json:"logging"`
	} `yaml:"status" json:"status"`
	Storage struct {
		Dir string `yaml:"dir" json:"dir" default:"var/db/openedge"`
	} `yaml:"storage" json:"storage"`
	Shutdown struct {
		Timeout time.Duration `yaml:"timeout" json:"timeout" default:"10m"`
	} `yaml:"shutdown" json:"shutdown"`
}

附上一个源码中示例的文件:

name: localhub
listen:
- tcp://0.0.0.0:1883
principals:
- username: 'test'
  password: '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
  permissions:
  - action: 'pub'
    permit: ['#']
  - action: 'sub'
    permit: ['#']
subscriptions:
- source:
    topic: 't'
  target:
    topic: 't/topic'
logger:
  path: var/log/openedge/localhub/localhub.log
  console: true
  level: "debug"
  • name是模块的名称,localhub的模块起名为localhub。
  • listen是用于开启服务器后监听的地址(客户端连接的地址)。
  • principals存放客户端连接的用户名、密码(密码是使用SHA-256加密的,客户端连接的时候使用解密后的明文密码)、权限等相关信息。
  • subscriptions用于存放消息转发(订阅)的路由规则,source是发布消息时的地址,target是转发的地址(比如A发送的“t”主题消息就会转发到订阅“t/topic”主题的客户端中。
  • logger是配置日志相关信息。

通过加载配置文件后,cfg中就存储了这些信息。

broker加载

使用淘宝的人既可以自己开店,也可以去买货,但是卖货的信息发布在什么地方呢?想要买东西从哪里浏览呢?当然是通过淘宝了,broker就是淘宝,所有连接到openedge-hub(后面简称为hub)的session既可以作为卖家发布消息,这个消息就发送到broker的channel(根据QOS不同对应了不同的channel)了,在生活中为了让买家能够买到称心如意的东西,淘宝一般会进行个性化推荐,在hub中每一个session会个性化的订阅主题,这时候路由器(Rule.Manager)就会把符合session需要的消息发送到session中了。<

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QAD ERP专用数据库及程序开发语言手册 ABL Reference Preface This Preface contains the following sections: * Purpose * Audience * Organization * Using this manual * Typographical conventions * Examples of syntax descriptions * Example procedures * OpenEdge messages * Third party acknowledgements Purpose This book describes ABL (Advanced Business Language), which is the OpenEdge® programming language for building business applications. It covers all ABL statements, functions, phrases, operators, preprocessor directives, special symbols, widgets, handles, classes, interfaces, attributes, methods, properties, and events. Audience This book is intended for programmers who develop applications using ABL and for anyone who needs to read and understand ABL code. Organization This book consists of the following sections: * A dictionary of ABL statements, functions, phrases, operators, preprocessors, and special symbols. * A dictionary of ABL widgets. * A dictionary of ABL handles. * A dictionary of ABL attributes and methods (for handles). * A dictionary of ABL handle-based object events. * A dictionary of ABL classes and interfaces. * A dictionary of ABL properties and methods (for classes). * A dictionary of ABL class events and event methods * An index of ABL keywords. Using this manual OpenEdge provides a special purpose programming language for building business applications. In the documentation, the formal name for this language is ABL (Advanced Business Language). With few exceptions, all keywords of the language appear in all UPPERCASE, using a font that is appropriate to the context. All other alphabetic language content appears in mixed case. For the latest documentation updates see the OpenEdge Product Documentation Overview page on PSDN: http://communities.progress.com/pcom/docs/DOC-16074. References to ABL compiler and run-time features ABL is both a compiled and an interpreted language that executes in a run-time engine.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值