master

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: LGPL-2.1 Imports: 18 Imported by: 1

README

go-service

go 语言开发的服务器模板,可与 acl_master 服务器框架深度集成。

一、安装

	go get -u github.com/acl-dev/go-service

二、使用

2.1、简单示例

编写源码 main.go 如下:

package main

import (
    "flag"
    "fmt"
    "log"
    "net"

    "github.com/acl-dev/go-service"
)

func onAccept(conn net.Conn) {
    buf := make([]byte, 8192)
    for {
        n, err := conn.Read(buf)
        if err != nil {
            fmt.Println("read over", err)
            break
        }

        _, err = conn.Write(buf[0:n])
        if err != nil {
            fmt.Println("write wrror", err)
            break
        }
    }
}

func onClose(conn net.Conn) {
    log.Println("---client onClose---", conn.RemoteAddr())
}

var (
    listenAddrs string
)

func main() {
    flag.StringVar(&listenAddrs, "listen", "127.0.0.1:8080; 127.0.0.1:8081", "listen addr in alone running")
    flag.Parse()

    master.Prepare()

    // Bind the given addresses from commandline or from master framework.
    service, err := master.TcpServiceInit(listenAddrs)
    if err != nil {
        log.Println("Init tcp service error:", err)
        return
    }

    // Set callback when accepting one connection.
    service.AcceptHandler = onAccept

    // Set callback when closing one connection.
    service.CloseHandler = onClose

    fmt.Printf("listen: %s\r\n", listenAddrs)

    // Start the service in alone or daemon mode.
    service.Run()
}

编译:

$ go build -o echod

手工运行:

$ ./echod -alone

该程序为一个简单的回显服务,运行后可以手工 telneet 127.0.0.1 8080 进行测试。

2.2、将 Go 服务程序部署在 acl_master 框架下
2.2.1 部署 acl_master 服务管理框架

首先需要从 https://github.com/acl-dev/aclhttps://gitee.com/acl-dev/acl 下载 acl 工程,然后编译安装,过程如下:

#cd acl; make
#cd disk/master; ./setup.sh /opt/soft/acl-master
#cd /opt/soft/acl-master/sh; ./start.sh

上面过程便完成了编译、安装及启动 acl_master 服务管理框架的过程。
如果您使用 CentOS 操作系统,还可以通过下面过程来完成(即:生成 acl_master RPM 包,然后安装该 RPM 包即可):

#cd packaging; make
#cd x86_64; rpm -ivh acl-master*.rpm

当 RPM 安装后 acl_master 服务管理程序会自动启动。

2.2.2 部署 Go 服务程序至 acl_master 框架下

首先下载 go-service 软件包并编译其中的服务示例,然后安装这些服务程序:

#go get -u github.com/acl-dev/go-service
#cd $GOPATH/src/github.com/acl-dev/go-service/examples/
#(cd go-echod; go build; ./setup.sh /opt/soft/go-echod)
#(cd go-httpd; go build; ./setup.sh /opt/soft/go-httpd)
#(cd gin-server; go get; go build; ./setup.sh /opt/soft/gin-server)
#/opt/soft/go-echod/bin/start.sh
#/opt/soft/go-httpd/bin/start.sh
#/opt/soft/gin-server/bin/start.sh

通过启动脚本分别启动这几个服务例子,启动脚本实际上是通知 acl_master 服务程序来启动这几个服务程序。

其中的示例 gin-server 是使用 gin 编写的一个简单的 Go web 服务。

最后运行 acl_master 服务框架中的管理工具来查看由 acl_master 管理的服务:

#/opt/soft/acl-master/bin/master_ctl -a list

结果显示如下:

status  service                                         type    proc    owner   conf    
200     127.0.0.1|5001, 127.0.0.1|5002, echod.sock      4       1       root    /opt/soft/go-echod/conf/go-echod.cf
200     |8881, 127.0.0.1|8882, go-httpd.sock            4       2       nobody  /opt/soft/go-httpd/conf/go-httpd.cf
200     |87, |88, |89, gin-server.sock                  4       2       nobody  /opt/soft/gin-server/conf/gin-server.cf

可以使用 curl 工具测试一下 gin-server 服务,如下:

# curl http://127.0.0.1:88/test
hello world!

说明 acl_master 服务管理程序已经管理了这几个 Go 写的服务进程。同时可以看到:

  • gin-server 服务由 acl_master 启动了两个进程;
  • gin-server 可以同时多个 TCP 端口地址(其实是由 acl_master 是监听的,gin-server 只是继承了这种监听行为);
  • gin-server 不仅可以监听 TCP 端口,还可同时监听 UNIX 域地址(这是由 acl_master 监听后传递给 gin-server 的);
  • gin-server 虽然监听的 TCP 端口 < 1024,但 gin-server 的运行身份已经被 acl_master 切换为普通身份(nobody)。

此外,还需要两点需要注明:

  • 在 UNIX 系统平台上,服务程序监听的端口如果小于 1024,则操作系统则要求此时的运行身份需为 root;
  • 在 Linux 平台下 Go 语言编写的服务程序是无法正常切换运行身份的(具体可参考 Go 源码)。

acl_master 的运行机制下,可以完美解决以上二者的矛盾。

三、参考

更多请参考 examples

Documentation

Index

Constants

View Source
const Version string = "1.1.2"

Variables

View Source
var (
	AppConf        *Config
	AppService     string
	AppLogPath     string
	AppOwner       string
	AppArgs        string
	AppRootDir     string
	AppUseLimit    = 0
	AppIdleLimit   = 0
	AppReusePort   = false
	AppQuickAbort  = false
	AppWaitLimit   = 10
	AppAccessAllow = "all"
	Appthreads     = 0

	TlsCertFile string
	TlsKeyFile  string
)

from configure file of the app

View Source
var (
	Configure    string
	ServiceName  string
	ServiceType  string
	Verbose      bool
	Unprivileged bool
	Chroot       = false
	SocketCount  = 1

	Alone bool
)

from command args

Functions

func ConnCountCur

func ConnCountCur() int

func ConnCountDec

func ConnCountDec()

func ConnCountInc

func ConnCountInc()

func GetListeners

func GetListeners() ([]net.Listener, error)

GetListeners In acl_master daemon running mode, this function will be called to init the listener handles.

func GetListenersByAddrs

func GetListenersByAddrs(addrs string) ([]net.Listener, error)

GetListenersByAddrs In run alone mode, the application should give the listening addrs and call this function to listen the given addrs

func OnAccept

func OnAccept(handler AcceptFunc)

func OnClose

func OnClose(handler CloseFunc)

func OnExit

func OnExit(handler ExitFunc)

func OnInit

func OnInit(handler InitFunc)

func OnPreJail

func OnPreJail(handler PreJailFunc)

func Prepare

func Prepare()

Prepare this function can be called automatically in net_service.go or web_service.go to load configure, and it can also be canned in application's main function

func ServiceInit

func ServiceInit(addrs string) ([]net.Listener, error)

func Stop added in v1.1.2

func Stop(ok bool)

func TcpAloneStart

func TcpAloneStart(addrs string) error

func TcpDaemonStart

func TcpDaemonStart() error

func TcpServiceStart

func TcpServiceStart(addrs string) error

TcpStart start TCP service with the specified listening addrs

func Wait added in v1.1.2

func Wait() bool

func WebServiceStart

func WebServiceStart(addrs string, handler http.Handler) error

WebServiceStart start WEB service with the specified listening addrs

Types

type AcceptFunc

type AcceptFunc func(net.Conn)

type CloseFunc

type CloseFunc func(net.Conn)

type Config

type Config struct {
	Entries map[string]string
}

func (Config) GetBool

func (c Config) GetBool(name string) bool

func (Config) GetInt

func (c Config) GetInt(name string) int

func (Config) GetString

func (c Config) GetString(name string) string

func (*Config) InitConfig

func (c *Config) InitConfig(path string)

type ExitFunc

type ExitFunc func()

type InitFunc

type InitFunc func()

type PreJailFunc

type PreJailFunc func()

type TcpService

type TcpService struct {
	AcceptHandler AcceptFunc
	CloseHandler  CloseFunc
	// contains filtered or unexported fields
}

func TcpServiceInit

func TcpServiceInit(addrs string) (*TcpService, error)

func (*TcpService) Run

func (service *TcpService) Run()

type WebService

type WebService struct {
	AcceptHandler AcceptFunc
	CloseHandler  CloseFunc
	// contains filtered or unexported fields
}

func WebServiceInit

func WebServiceInit(addrs string, handler http.Handler) (*WebService, error)

func (*WebService) Run

func (service *WebService) Run()

WebServiceStart start WEB service with the specified listening addrs

Jump to

Keyboard shortcuts

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