mangoconfig

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

README

MangoConfig配置管理包

mangoconfig配置服务用于服务器配置修改,在服务器启动或部署中可能存在配置信息更新情况, 某些情况需要重启服务从而重新初始化某些服务,例如mysql初始化、redis初始化等。mangoconfig包 提供了修改内存配置及重启初始化方法等功能。能够满足小部分需求,相较于现有的配置中心,功能更 加间接。

使用技术

  1. golang
  2. rpc
  3. etcd

使用方法

server端

config := mangoconfig.RpcConfig{
    Addr:     "192.168.199.135:9456",
    ETCDAddr: []string{"192.168.199.243:2379"},
}
global.ConfigServer = mangoconfig.NewRPCxServer(config)
global.ConfigServer.Start()

服务端发送更新配置消息

mm := mangoconfig.ConfMsg{
    Name: reqJ.ConfName,
    Data: reqJ.Config,
}
mmStr, err := mm.Encode()
if err != nil{
    return
}

m := mangoconfig.MessageMng{
    Name:    reqJ.ClientName,
    IP:      reqJ.IP,
    Message: mangoconfig.Message{
        Type: mangoconfig.UPDATE_CONFIG_MOMERY,
        Data: mmStr,
    },
}

global.ConfigServer.SendMessage(m)

服务端发送重新初始化消息

m := mangoconfig.MessageMng{
		Name:    reqJ.ClientName,
		IP:      reqJ.IP,
		Message: mangoconfig.Message{
			Type: mangoconfig.RESETUP_CONFIG,
			Data: reqJ.Label,
		},
	}

	global.ConfigServer.SendMessage(m)

client端

client := mangoconfig.NewClient("MangoApi", []string{"192.168.199.243:2379"})
client.Start()
_ = client.RegisterConfStruct("config", config.Conf)
_ = client.RegisterSetupFunc("初始化rpc", "rpcSetup", "初始化rpc方法", clientrpcx.Setup)
_ = client.RegisterSetupFunc("初始化http", "httpSetup", "初始化http方法", server.Setup)

方法

server提供

  • SendMessage():用于服务端发送通知消息

client提供

  • RegisterConfStruct():用于注册配置对象
  • RegisterSetupFunc():用于注册初始化方法

心跳问题

心跳由服务端主动推送,若客户端没响应则会删掉客户端。

关于服务

Documentation

Overview

* * @Author vangogh * @Description 配置客户端 * @File: client * @Datetime 2022/5/17 14:48 *

* * @Author vangogh * @Description 连接管理 * @File: connect * @Datetime 2022/5/17 15:16 *

* * @Author vangogh * @Description mango * @File: mango * @Datetime 2022/5/17 15:01 *

* * @Author vangogh * @Description 消息 * @File: message * @Datetime 2022/5/18 10:32 *

* * @Author vangogh * @Description 数据结构体 * @File: model * @Datetime 2022/5/18 10:18 *

* * @Author vangogh * @Description 配置服务端 * @File: server * @Datetime 2022/5/17 14:48 *

Index

Constants

View Source
const (
	CONFIG_SERVICE = "ConfigService"
)

Variables

This section is empty.

Functions

func GetAllConnection

func GetAllConnection() map[string][]*Connection

获取现有的所有连接

func ParsePort

func ParsePort(addr string) (port string)

Types

type API

type API struct {
}

控制服务

func (*API) RegisterClient

func (a *API) RegisterClient(ctx context.Context, req RegisterReq, resp *RegisterResp) error

注册客户端

func (*API) RegisterConfigStruct

func (a *API) RegisterConfigStruct(ctx context.Context, req RegisterConfigStruct, resp *RegisterResp) error

注册配置

func (*API) RegisterSetupFunc

func (a *API) RegisterSetupFunc(ctx context.Context, req RegisterSetupFunc, resp *RegisterResp) error

注册初始化方法

type ClientConf

type ClientConf struct {
	ConfName string
	ConfData string
}

type ConfMsg

type ConfMsg struct {
	Name string
	Data string
}

func (*ConfMsg) Decode

func (m *ConfMsg) Decode(mStr string) error

解码

func (*ConfMsg) Encode

func (m *ConfMsg) Encode() (string, error)

加码

type ConfigClient

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

func NewClient

func NewClient(name string, etcd []string) *ConfigClient

初始化一个客户端

func (*ConfigClient) RegisterConfStruct

func (c *ConfigClient) RegisterConfStruct(name string, conf interface{}) error

*

  • @Description: 注册配置结构
  • @receiver c
  • @param name 配置名
  • @param conf 名字对象

func (*ConfigClient) RegisterSetupFunc

func (c *ConfigClient) RegisterSetupFunc(name, label, desc string, setup func()) error

*

  • @Description: 注册初始化方法
  • @receiver c
  • @param name 名称
  • @param label 标签,用于标记
  • @param desc 描述
  • @param setup 无参初始化方法
  • @return error

func (*ConfigClient) Start

func (c *ConfigClient) Start()

启动后开始监听

type Connection

type Connection struct {
	Conn   net.Conn     // 服务器连接
	IP     string       // 服务器ip
	Name   string       // 服务器名称
	Config []ClientConf // 服务配置列表
	Setups []SetupFunc  // 初始化方法列表
}

type ConnectionMng

type ConnectionMng struct {
	Clients map[string][]*Connection
	// contains filtered or unexported fields
}

func (*ConnectionMng) AddClient

func (c *ConnectionMng) AddClient(ip, name string, conn net.Conn)

添加

func (*ConnectionMng) AddConfigSetup

func (c *ConnectionMng) AddConfigSetup(ip, name, confName, confData string)

添加配置方法

func (*ConnectionMng) AddSetupFunc

func (c *ConnectionMng) AddSetupFunc(ip, name, funcName, funcLabel, funcDesc string)

添加初始化方法

func (*ConnectionMng) GetClient

func (c *ConnectionMng) GetClient(name, ip string) *Connection

取出一个客户端

func (*ConnectionMng) GetClients

func (c *ConnectionMng) GetClients(name string) []*Connection

取出一个分组客户端

func (*ConnectionMng) RemoveClient

func (c *ConnectionMng) RemoveClient(name, ip string)

移除

type Message

type Message struct {
	Type MessageType // 消息类型
	Data string      // 数据
}

消息分为消息加解码

func (*Message) Decode

func (m *Message) Decode(mStr string) error

解码

func (*Message) Encode

func (m *Message) Encode() (string, error)

加码

type MessageMng

type MessageMng struct {
	Name    string
	IP      string
	Message Message
}

type MessageType

type MessageType int
const (
	HEARTBEAT            MessageType = 0 // 心跳
	UPDATE_CONFIG_MOMERY MessageType = 1 // 只更新内存配置
	UPDATE_CONFIG_FILE   MessageType = 2 // 更新文件配置
	RESETUP_CONFIG       MessageType = 3 // 初始化普通函数
	RESTART_CONFIG       MessageType = 4 // 重启服务
)

type RPCxServer

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

func NewRPCxServer

func NewRPCxServer(config RpcConfig) *RPCxServer

func (*RPCxServer) SendMessage

func (r *RPCxServer) SendMessage(m MessageMng)

func (*RPCxServer) Start

func (r *RPCxServer) Start()

type RegisterConfigStruct

type RegisterConfigStruct struct {
	ClientName string
	ConfName   string
	ConfData   string
}

请求结构体

type RegisterReq

type RegisterReq struct {
	Name string
}

请求结构体

type RegisterResp

type RegisterResp struct {
	Code int
}

响应结构体

type RegisterSetupFunc

type RegisterSetupFunc struct {
	ClientName string
	FuncName   string
	FuncLabel  string
	FuncDesc   string
}

请求结构体

type RpcConfig

type RpcConfig struct {
	Addr     string
	ETCDAddr []string
}

type SetupFunc

type SetupFunc struct {
	FuncName  string
	FuncLabel string
	FuncDesc  string
}

Jump to

Keyboard shortcuts

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