This commit is contained in:
wangjian 2022-10-23 14:03:36 +08:00
commit cba91dd876
6 changed files with 429 additions and 0 deletions

128
cmd/server.go Normal file
View File

@ -0,0 +1,128 @@
package cmd
import (
"fmt"
"git.hpds.cc/Component/network/log"
"github.com/spf13/cobra"
"hpds_mq/config"
"os"
"os/signal"
"syscall"
"git.hpds.cc/pavement/hpds_node"
discover "hpds_mq/internal/discover/consul"
)
var (
consulConfigs chan *discover.ConsulConfig
ConfigFileFlag string = "./config/config.yml"
ConsulAddress string = "http://localhost:8500"
NodeName string = "main-node"
Mode string = "dev"
)
func must(err error) {
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func NewStartCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start hpds_mq broker",
Run: func(cmd *cobra.Command, args []string) {
var (
cfg *config.MessageQueueConfig
err error
)
must(err)
configFileFlag, err := cmd.Flags().GetString("c")
if err != nil {
fmt.Println("get local config err: ", err)
return
}
ConsulAddress, err = cmd.Flags().GetString("r")
if err != nil {
fmt.Println("get remote config err: ", err)
return
}
NodeName, err = cmd.Flags().GetString("n")
if err != nil {
fmt.Println("get remote path config err: ", err)
return
}
Mode, err = cmd.Flags().GetString("m")
if err != nil {
fmt.Println("get remote path config err: ", err)
return
}
if len(configFileFlag) > 1 {
cfg, err = config.ParseConfigByFile(configFileFlag)
must(err)
err = config.UpdateRemoteConfig(cfg)
must(err)
ConfigFileFlag = configFileFlag
} else {
//获取consul注册中心的配置文件
cfg, err = config.GetRemoteConfig(ConsulAddress, fmt.Sprintf("hpds-pavement/hpds_mq/%s/%s", Mode, NodeName))
must(err)
err = config.UpdateLocalConfig(cfg, ConfigFileFlag)
}
// 退出channel
exitChannel := make(chan os.Signal)
defer close(exitChannel)
// 退出信号监听
go func(c chan os.Signal) {
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
}(exitChannel)
mq, err := hpds_node.NewMq(configFileFlag)
mq.InitOptions(hpds_node.WithAuth("token", cfg.Token))
defer mq.Close()
if len(cfg.CascadeNode) > 0 {
for i := 0; i < len(cfg.CascadeNode); i++ {
mq.AddDownstreamMq(hpds_node.NewDownstreamMq(
cfg.CascadeNode[i].Name,
hpds_node.WithMqAddr(fmt.Sprintf("%s:%d", cfg.CascadeNode[i].Host, cfg.CascadeNode[i].Port)),
hpds_node.WithCredential(fmt.Sprintf("token:%s", cfg.CascadeNode[i].Token)),
))
}
}
// start mq service
log.Printf("Server has started!, pid: %d", os.Getpid())
go func() {
err = mq.ListenAndServe()
if err != nil {
panic(err)
}
}()
select {}
},
}
cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path")
cmd.Flags().StringVar(&ConsulAddress, "r", "http://consul.hpds.cc", "The configuration remote consul address")
cmd.Flags().StringVar(&NodeName, "n", "main-node", "The configuration name")
cmd.Flags().StringVar(&Mode, "m", "dev", "run mode : dev | test | releases")
return cmd
}
/*
func Run() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 退出channel
exitChannel := make(chan os.Signal)
defer close(exitChannel)
// 退出信号监听
go func(c chan os.Signal) {
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
}(exitChannel)
}
*/

87
config/config.go Normal file
View File

@ -0,0 +1,87 @@
package config
import (
"bytes"
"fmt"
"github.com/spf13/viper"
"os"
consulapi "github.com/hashicorp/consul/api"
_ "github.com/spf13/viper/remote"
"gopkg.in/yaml.v3"
)
type MessageQueueConfig struct {
Name string `yaml:"name,omitempty"`
Host string `yaml:"host,omitempty"`
Port int `yaml:"port,omitempty"`
Mode string `yaml:"mode,omitempty"`
Token string `yaml:"token,omitempty"`
CascadeNode []MessageQueueConfig `yaml:"cascadeNode,omitempty"`
Consul ConsulConfig `yaml:"consul,omitempty"`
}
type ConsulConfig struct {
Host string `yaml:"host,omitempty"`
Port int `yaml:"port,omitempty"`
Interval int `yaml:"interval,omitempty"`
Timeout int `yaml:"timeout,omitempty"`
Deregister int `yaml:"deregister,omitempty"`
Tags []string `yaml:"tags,omitempty"`
}
func ParseConfigByFile(path string) (cfg *MessageQueueConfig, err error) {
buffer, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return load(buffer)
}
func load(buf []byte) (cfg *MessageQueueConfig, err error) {
cViper := viper.New()
cViper.SetConfigType("yaml")
cfg = new(MessageQueueConfig)
cViper.ReadConfig(bytes.NewBuffer(buf))
err = cViper.Unmarshal(cfg)
if err != nil {
return nil, err
}
return
}
func UpdateLocalConfig(cfg *MessageQueueConfig, fn string) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
err = os.WriteFile(fn, data, 0600)
return err
}
func UpdateRemoteConfig(cfg *MessageQueueConfig) error {
consulClient, err := consulapi.NewClient(&consulapi.Config{Address: fmt.Sprintf("%s:%d", cfg.Consul.Host, cfg.Consul.Port)})
if err != nil {
return err
}
val, err := yaml.Marshal(cfg)
if err != nil {
return err
}
p := &consulapi.KVPair{Key: fmt.Sprintf("hpds-pavement/hpds_mq/%s/%s", cfg.Mode, cfg.Name), Value: val}
if _, err = consulClient.KV().Put(p, nil); err != nil {
return err
}
return nil
}
func GetRemoteConfig(remoteAddr, path string) (cfg *MessageQueueConfig, err error) {
consulClient, err := consulapi.NewClient(&consulapi.Config{Address: remoteAddr})
if err != nil {
return nil, err
}
kv, _, err := consulClient.KV().Get(path, nil)
if err != nil {
return nil, err
}
return load(kv.Value)
}

15
config/config.yaml Normal file
View File

@ -0,0 +1,15 @@
name: main-node
host: 0.0.0.0
port: 27188
mode: dev
token: 06d36c6f5705507dae778fdce90d0767
consul:
host: http://consul.hpds.cc
port: 80
interval: 300
timeout: 5
deregister: 1
tags:
- mq
functions:
- name: echo-sf

88
go.mod Normal file
View File

@ -0,0 +1,88 @@
module hpds_mq
go 1.19
require (
github.com/hashicorp/consul/api v1.15.3
github.com/spf13/cobra v1.6.0
github.com/spf13/viper v1.13.0
gopkg.in/yaml.v3 v3.0.1
)
require (
cloud.google.com/go v0.100.2 // indirect
cloud.google.com/go/compute v1.6.1 // indirect
cloud.google.com/go/firestore v1.6.1 // indirect
git.hpds.cc/Component/mq_coder v0.0.0-20221010064749-174ae7ae3340 // indirect
git.hpds.cc/Component/network v0.0.0-20221012021659-2433c68452d5 // indirect
git.hpds.cc/pavement/hpds_node v0.0.0-20221023053316-37f7ba99eab3 // indirect
github.com/armon/go-metrics v0.3.10 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/serf v0.9.7 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lucas-clemente/quic-go v0.29.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.2 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect
github.com/matoous/go-nanoid/v2 v2.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sagikazarmark/crypt v0.6.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
go.etcd.io/etcd/api/v3 v3.5.4 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.4 // indirect
go.etcd.io/etcd/client/v2 v2.305.4 // indirect
go.etcd.io/etcd/client/v3 v3.5.4 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/api v0.81.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

View File

@ -0,0 +1,79 @@
package discover
import (
"fmt"
"github.com/hashicorp/consul/api"
)
type ConsulConfig struct {
Client *api.Client `json:"client"` // consul client
ConsulAddress string `json:"consulAddress"` // consul 服务地址:IP+port
ServiceId string `json:"serviceId"` // 服务ID
ServiceName string `json:"serviceName"` // 服务名称
ServiceIP string `json:"serviceIP"` // 服务IP
ServicePort int `json:"servicePort"` // 服务端口
Tags []string `json:"tags"` // 服务标签列表
DeregisterCriticalServiceAfter int `json:"deregisterCriticalServiceAfter"` // 指定与服务关联的检查应在此时间之后注销
Interval int `json:"interval"` // 指定运行此检查的频率
Timeout int `json:"timeout"` // 在脚本、HTTP、TCP 或 gRPC 检查的情况下指定传出连接的超时时间
}
func NewConsulConfig(consulAddress string, serviceId string, serviceName string, serviceIP string, servicePort int, tags []string, deregisterCriticalServiceAfter int, interval int, timeout int) (*ConsulConfig, error) {
// 1.consul配置
config := api.DefaultConfig()
config.Address = consulAddress
// 2.client
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
return &ConsulConfig{
Client: client,
ConsulAddress: consulAddress,
ServiceId: serviceId,
ServiceName: serviceName,
ServiceIP: serviceIP,
ServicePort: servicePort,
Tags: tags,
DeregisterCriticalServiceAfter: deregisterCriticalServiceAfter,
Interval: interval,
Timeout: timeout,
}, nil
}
// ServiceRegister 服务注册
func (cf *ConsulConfig) ServiceRegister() (err error) {
// 注册器
reg := &api.AgentServiceRegistration{
ID: cf.ServiceId,
Name: cf.ServiceName,
Address: cf.ServiceIP,
Port: cf.ServicePort,
Tags: cf.Tags,
Check: &api.AgentServiceCheck{
Interval: fmt.Sprintf("%vs", cf.Interval), // 健康检查间隔
HTTP: fmt.Sprintf("http://%v:%v/health", cf.ServiceIP, cf.ServicePort), // HTTP 支持执行健康检查的地址service 会传到 Health.Check 函数中
Timeout: fmt.Sprintf("%vs", cf.Timeout), // 健康检查超时时间
DeregisterCriticalServiceAfter: fmt.Sprintf("%vs", cf.DeregisterCriticalServiceAfter), // 注销时间,相当于过期时间
Notes: "Consul check service health status.",
},
}
// 注册服务
err = cf.Client.Agent().ServiceRegister(reg)
if err != nil {
return err
}
return nil
}
// ServiceDeregister 服务注销
func (cf *ConsulConfig) ServiceDeregister() error {
return cf.Client.Agent().ServiceDeregister(cf.ServiceId)
}
// ServiceDiscover 服务发现
func (cf *ConsulConfig) ServiceDiscover(service string, tags []string, q *api.QueryOptions) ([]*api.CatalogService, *api.QueryMeta, error) {
return cf.Client.Catalog().ServiceMultipleTags(service, tags, q)
}

32
main.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
"fmt"
"github.com/spf13/cobra"
"hpds_mq/cmd"
"os"
)
var (
rootCmd = &cobra.Command{
Use: "hpds_mq",
Long: "hpds_mq is a IoT broker that fully implements MQTT V5.0 and V3.1.1 protocol",
Version: "0.1",
}
)
func init() {
//rootCmd.PersistentFlags().StringVarP(&cmd.ConfigFileFlag, "config", "c", cmd.ConfigFileFlag, "The configuration file path")
//rootCmd.PersistentFlags().StringVarP(&cmd.ConsulAddress, "remote", "r", cmd.ConsulAddress, "The configuration remote consul address")
rootCmd.AddCommand(cmd.NewStartCmd())
//cmd.NewStartCmd()
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
}