Skip to content
/ libmsf Public

libmsf acting as mircro service process framework, provide infrastructure and provide service monitoring etc.

Notifications You must be signed in to change notification settings

wypx/libmsf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux平台微服务框架-libmsf

Build Status Codecov Maven Central GitHub release License

MSF (Mircro Service Framework) acting as mircro service process framework, provide base infrastructure such as network, epoll event, timer, general logger, mempool and process service monitoring etc.

Directory Features
app Application demo
cmake CMake tools
doc Document
lib Library with base and network
script Scripts tools
test Test code

Dependent environment

Software version requirements
linux kernel: 2.6.18 and above
gcc: 4.1.2 and above glibc-devel
cmake: 3.2 and above
numa: numa-dev

Compile and install

git clone https://github.com/wypx/libmsf.git
./build.py

Detailed reference

主要特性包括:

  • 为各个模块提供基础设施模块 比如内存 日志 网络 事件 信号等基础库
  • 提供微服务进程动态拉起 动态监控框架,实现微服务进程快速秒级拉起
  • 提供微服务so动态加载 动态卸载做到插件化的即插即用
  • 提供各个微服务进程的内存监控等亚健康状态监控,达到实时告警通知
  • 提供微服务配置动态解析,热切换微服务进程,实现平滑升级过度

libmsf

快速开始

安装环境

安装Ubuntu, Debian等Linux, ARM环境

要求gcc版本(测试gcc版本): gcc version 9.2.1 20200123 (Debian 9.2.1-25) 

安装numactrl: https://github.com/numactl/numactl

更新gcc方法:

https://www.cnblogs.com/whuanle/p/10546634.html

下载开源库

git clone https://github.com/wypx/libmsf/
git clone https://github.com/wypx/librpc/

Please see this Wiki Download Page for more repository infos.

编译开源库

root@KaliCI:/media/psf/tomato/mod/libmsf make
Make Subdirs msf
make[1]: Entering directory '/media/psf/tomato/mod/libmsf/msf
arm-linux-gnueabihf-gcc lib/src/msf_log.o
.................
make[1]: Leaving directory '/media/psf/tomato/mod/libmsf/msf_daemon

root@KaliCI:/media/psf/tomato/mod/librpc/server make
arm-linux-gnueabihf-gcc bin/src/conn.o
arm-linux-gnueabihf-gcc bin/src/config.o
.......
root@KaliCI:/media/psf/tomato/mod/librpc/client# ls ../../../packet/binary/
msf_agent  msf_daemon  msf_dlna  msf_shell  msf_upnp

root@KaliCI:/media/psf/tomato/mod/librpc/client# ls ../../../packet/library/
libipc.so  libmsf.so
msf_agent  各个服务进程之间的通信代理服务端程序
msf_dlna   测试程序 - 独立微服务进程客户端DLNA
msf_upnp   测试程序 - 独立微服务进程客户端UPNP
msf_daemon 用于守护监控代理进程msf_agent
msf_shell  壳子程序,用于记载配置文件中的插件

libipc.so 提供给各个微服务进程连接的通信代理客户端库
libipc.so 提供给各个微服务进程的基础设施库
          包括:网络 管道 Epoll等事件驱动 日志 共享内存 内存池 
          串口通信 线程 进程 CPU工具 文件 加密 微服务框架 定时器

运行开源库

1. 执行样例程序
   $ ./msf_agent
   $ ./msf_upnp
   $ ./msf_dlna
2. 查看运行日志
   运行结果

API使用解析

微服务API使用:(动态加载SO的方式, 一个服务就是一个独立的SO)
https://github.com/wypx/libmsf/blob/master/msf/src/msf_svc.c
https://github.com/wypx/libmsf/blob/master/msf_shell/src/msf_shell.c

s32 service_init(void) {
    u32 svc_idx;
    struct msf_svc *svc_cb;
    for (svc_idx = 0; svc_idx < g_proc->proc_svc_num; svc_idx++) {
        if (msf_svcinst_init(&(g_proc->proc_svcs[svc_idx])) < 0) {
            return -1;
        };
    }

    for (svc_idx = 0; svc_idx < g_proc->proc_svc_num; svc_idx++) {
        svc_cb = g_proc->proc_svcs[svc_idx].svc_cb;
        if (svc_cb->init(NULL, 0) < 0) {
            return -1;
        }
    }
    return 0;
}

使用配置文件配置文件名字msf_agent_svc.json
配置文件内容:
{
    "proc_name": "msf_agent",
    "proc_author": "http://luotang.me",
    "proc_version": "beta v1.0",
    "proc_desc": "Micro agent service config file",
    "proc_svcs":
    [
        {
          "svc_name": "msf_agent1",
          "svc_libs": "libmsf_agent1.so"
        },
        {
          "svc_name": "msf_agent2",
          "svc_libs": "libmsf_agent2.so"
        }
    ]
}
配置特点支持一个壳子进程拉起多个微服务
支持配置微服务进程的名称作者版本描述微服务实例信息

使用壳子进程拉起微服务SO:
./msf_shell -c msf_agent_svc.json
微服务API使用:(进程内静态微服务模块, 类似Nginx的模块加载)
https://github.com/wypx/mobile/blob/master/src/at_mod.c
https://github.com/wypx/mobile/blob/master/src/mobile.c

extern struct msf_svc  mobile_usb;
extern struct msf_svc  mobile_dial;
extern struct msf_svc  mobile_sms;
extern struct msf_svc* mobile_module[];

struct msf_svc* mobile_module[] = {
    &mobile_usb,
    &mobile_dial,
    &mobile_sms,
    NULL,
};

struct usb_info usb;
struct dial_info dial;
 
rc = mobile_module[MOBILE_MOD_USB]->init(NULL, 0);
mobile_module[MOBILE_MOD_USB]->get_param(&usb, sizeof(usb));
if (rc < 0) {
    return -1;
}
rc = mobile_module[MOBILE_MOD_USB]->start(NULL, 0);
mobile_module[MOBILE_MOD_USB]->get_param(&usb, sizeof(usb));

mobile_module[MOBILE_MOD_DIAL]->init(&usb, sizeof(usb));
mobile_module[MOBILE_MOD_DIAL]->start(NULL, 0);
mobile_module[MOBILE_MOD_DIAL]->get_param(&dial, sizeof(dial));
事件Event API使用https://github.com/wypx/librpc/blob/master/client/src/thread.c

static void tx_thread_callback(void *arg) {
    //do send something
}

void * tx_thread_worker(void *lparam) {
    struct client *rpc = (struct client*)lparam;
    while (!rpc->stop_flag) {
        msf_event_base_dispatch(rpc->tx_evt_base);
    }
    return NULL;
}

s32 thread_init(void) {
    s32 rc = -1;
    rpc->tx_evt_base = msf_event_base_new();
    if (!rpc->tx_evt_base) {
        return -1;
    }
    
    rpc->tx_evt = msf_event_new(rpc->evt_conn.fd, tx_thread_callback,
                    NULL, NULL, &rpc->evt_conn);
    if (!rpc->tx_evt) {
        return -1;
    }

    msf_event_add(rpc->tx_evt_base, rpc->tx_evt);

    rc = pthread_spawn(&rpc->tx_tid, (void*)tx_thread_worker, rpc);
    if (rc < 0) {
        MSF_RPC_LOG(DBG_ERROR, "TX thread create failed, ret(%d), errno(%d).", rc, errno);
        return -1;
    }
}

硬件平台适配

根据开发者目标平台以的不同,需要进行相应的适配

参考文章

License

Libmsf is released under the Gnu 2.0 license.

/**************************************************************************
*
* Copyright (c) 2017-2019, luotang.me <wypx520@gmail.com>, China.
* All rights reserved.
*
* Distributed under the terms of the GNU General Public License v2.
*
* This software is provided 'as is' with no explicit or implied warranties
* in respect of its properties, including, but not limited to, correctness
* and/or fitness for purpose.
*
**************************************************************************/

About

libmsf acting as mircro service process framework, provide infrastructure and provide service monitoring etc.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published