motan

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2021 License: Apache-2.0 Imports: 47 Imported by: 6

README

Motan-go

License Build Status codecov GoDoc Go Report Card

Overview

Motan is a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

This project is the golang Motan implementation. Provides golang motan server, motan client and motan agent. motan agent is designed to support bio-proxy for any other language such as PHP, Python by motan2 protocol.

Features

  • Interactive with mulit language through motan2 protocol,such as Java, PHP.
  • Provides cluster support and integrate with popular service discovery services like Consul or Zookeeper.
  • Supports advanced scheduling features like weighted load-balance, scheduling cross IDCs, etc.
  • Optimization for high load scenarios, provides high availability in production environment.
  • Supports both synchronous and asynchronous calls.

Quick Start

Installation

go get -u -v github.com/weibocom/motan-go

The quick start gives very basic example of running client and server on the same machine. For the detailed information about using and developing Motan, please jump to Documents. the demo case is in the main/ directory

Motan server

  1. Create serverdemo.yaml to config service
#config of registries
motan-registry:
  direct-registry: # registry id 
    protocol: direct   # registry type

#conf of services
motan-service:
  mytest-motan2:
    path: com.weibo.motan.demo.service.MotanDemoService # e.g. service name for register
    group: motan-demo-rpc
    protocol: motan2
    registry: direct-registry
    serialization: simple
    ref : "main.MotanDemoService"
    export: "motan2:8100"
  1. Write an implementation, create and start RPC Server.
package main

import (
	"fmt"
	"time"

	motan "github.com/weibocom/motan-go"
)

func main() {
	runServerDemo()
}

func runServerDemo() {
	mscontext := motan.GetMotanServerContext("serverdemo.yaml") //get config by filename
	mscontext.RegisterService(&MotanDemoService{}, "") // registry implement
	mscontext.Start(nil) // start server
	time.Sleep(time.Second * 50000000)
}

// service implement
type MotanDemoService struct{}

func (m *MotanDemoService) Hello(name string) string {
	fmt.Printf("MotanDemoService hello:%s\n", name)
	return "hello " + name
}

Motan client

  1. Create clientdemo.yaml to config service for subscribe
#config of registries
motan-registry:
  direct-registry: # registry id 
    protocol: direct   # registry type. 
    host: 127.0.0.1 
    port: 9981 

#conf of refers
motan-refer:
  mytest-motan2:
    path: com.weibo.motan.demo.service.MotanDemoService # e.g. service name for subscribe
    group: motan-demo-rpc # group name
    protocol: motan2 # rpc protocol
    registry: direct-registry
    requestTimeout: 1000
    serialization: simple
    haStrategy: failover
    loadbalance: roundrobin
  1. Start call
package main

import (
	"fmt"

	motan "github.com/weibocom/motan-go"
	motancore "github.com/weibocom/motan-go/core"
)

func main() {
	runClientDemo()
}

func runClientDemo() {
	mccontext := motan.GetClientContext("clientdemo.yaml")
	mccontext.Start(nil)
	mclient := mccontext.GetClient("mytest-motan2")

	var reply string
	err := mclient.Call("hello", []interface{}{"Ray"}, &reply)  // sync call
	if err != nil {
		fmt.Printf("motan call fail! err:%v\n", err)
	} else {
		fmt.Printf("motan call success! reply:%s\n", reply)
	}

	// async call
	result := mclient.Go("hello", []interface{}{"Ray"}, &reply, make(chan *motancore.AsyncResult, 1))
	res := <-result.Done
	if res.Error != nil {
		fmt.Printf("motan async call fail! err:%v\n", res.Error)
	} else {
		fmt.Printf("motan async call success! reply:%+v\n", reply)
	}
}

Use agent.

agent is not necessary for golang. it designed for interpreted languages such as PHP to support service governance

  1. Create clientdemo.yaml to config service for subscribe or register
#config fo agent
motan-agent:
  port: 9981 # agent serve port. 
  mport: 8002 # agent manage port 

#config of registries
motan-registry:
  direct-registry: # registry id 
    protocol: direct   # registry type. will get instance from extFactory.
    host: 127.0.0.1 # direct server ip.
    port: 8100 #direct server port

#conf of refers
motan-refer:
  mytest-motan2:
    path: com.weibo.motan.demo.service.MotanDemoService # e.g. service name for subscribe
    group: motan-demo-rpc
    protocol: motan2
    registry: direct-registry
    serialization: simple
  1. Start Agent
package main

import motan "github.com/weibocom/motan-go"

func main() {
	runAgentDemo()
}

func runAgentDemo() {
	agent := motan.NewAgent(nil)
	agent.ConfigFile = "./agentdemo.yaml"
	agent.StartMotanAgent()
}

Documents

Contributors

License

Motan is released under the Apache License 2.0.

Documentation

Overview

Motan-go is the golang implementation of Motan. Motan is a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

Motan-go includes server, client and agent. agent is designed for interpreted languages such as PHP to support service governance.

Index

Constants

View Source
const (
	DefaultMeshRequestTimeout = 5 * time.Second
	DefaultMeshAddress        = "127.0.0.1:9981"
	DefaultMeshSerialization  = "simple"
)
View Source
const (
	Version = "1.0.0"
)

Variables

View Source
var (

	// PermissionCheck is default permission check for manage request
	PermissionCheck = NoPermissionCheck
)

Functions

func AddDefaultExt

func AddDefaultExt(d motan.ExtensionFactory)

func Cmdline

func Cmdline(w http.ResponseWriter, r *http.Request)

Cmdline responds with the running program's command line, with arguments separated by NUL bytes. The package initialization registers it as /debug/pprof/cmdline.

func GetDefaultExtFactory

func GetDefaultExtFactory() motan.ExtensionFactory

func GetDefaultManageHandlers

func GetDefaultManageHandlers() map[string]http.Handler

func Handler

func Handler(name string) http.Handler

Handler returns an HTTP handler that serves the named profile.

func Index

func Index(w http.ResponseWriter, r *http.Request)

Index responds with the pprof-formatted profile named by the request. For example, "/debug/pprof/heap" serves the "heap" profile. Index responds to a request for "/debug/pprof/" with an HTML page listing the available profiles.

func MeshTrace

func MeshTrace(w http.ResponseWriter, r *http.Request)

func NoPermissionCheck

func NoPermissionCheck(r *http.Request) bool

func Profile

func Profile(w http.ResponseWriter, r *http.Request)

Profile responds with the pprof-formatted cpu profile. The package initialization registers it as /debug/pprof/profile.

func StatConnections

func StatConnections(w http.ResponseWriter)

func StatOpenFiles

func StatOpenFiles(w http.ResponseWriter)

func StatProcess

func StatProcess(w http.ResponseWriter)

func StatSystem

func StatSystem(w http.ResponseWriter)

func Symbol

func Symbol(w http.ResponseWriter, r *http.Request)

Symbol looks up the program counters listed in the request, responding with a table mapping program counters to function names. The package initialization registers it as /debug/pprof/symbol.

func Trace

func Trace(w http.ResponseWriter, r *http.Request)

Trace responds with the execution trace in binary form. Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified. The package initialization registers it as /debug/pprof/trace.

Types

type Agent

type Agent struct {
	ConfigFile string

	Context *motan.Context
	// contains filtered or unexported fields
}

func NewAgent

func NewAgent(extfactory motan.ExtensionFactory) *Agent

func (*Agent) ExportService

func (a *Agent) ExportService(url *motan.URL) error

func (*Agent) GetAgentServer

func (a *Agent) GetAgentServer() motan.Server

get Agent server

func (*Agent) OnAfterStart

func (a *Agent) OnAfterStart(f func(a *Agent))

func (*Agent) RegisterManageHandler

func (a *Agent) RegisterManageHandler(path string, handler http.Handler)

func (*Agent) RuntimeDir

func (a *Agent) RuntimeDir() string

RuntimeDir acquires the agent runtime working directory

func (*Agent) SetAllServicesAvailable

func (a *Agent) SetAllServicesAvailable()

func (*Agent) SetAllServicesUnavailable

func (a *Agent) SetAllServicesUnavailable()

func (*Agent) SetSanpshotConf

func (a *Agent) SetSanpshotConf()

func (*Agent) StartMotanAgent

func (a *Agent) StartMotanAgent()

func (*Agent) StartMotanAgentFromConfig

func (a *Agent) StartMotanAgentFromConfig(config *cfg.Config)

func (*Agent) SubscribeService

func (a *Agent) SubscribeService(url *motan.URL) error

func (*Agent) UnexportService

func (a *Agent) UnexportService(url *motan.URL) error

type AgentListener

type AgentListener struct {
	CurrentCommandInfo string
	// contains filtered or unexported fields
}

func (*AgentListener) GetIdentity

func (a *AgentListener) GetIdentity() string

func (*AgentListener) NotifyCommand

func (a *AgentListener) NotifyCommand(registryURL *motan.URL, commandType int, commandInfo string)

type Client

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

func NewClient

func NewClient(url *motan.URL, cluster *cluster.MotanCluster, extFactory motan.ExtensionFactory) *Client

func (*Client) BaseCall

func (c *Client) BaseCall(req motan.Request, reply interface{}) error

func (*Client) BaseGo

func (c *Client) BaseGo(req motan.Request, reply interface{}, done chan *motan.AsyncResult) *motan.AsyncResult

func (*Client) BuildRequest

func (c *Client) BuildRequest(method string, args []interface{}) motan.Request

func (*Client) Call

func (c *Client) Call(method string, args []interface{}, reply interface{}) error

func (*Client) Go

func (c *Client) Go(method string, args []interface{}, reply interface{}, done chan *motan.AsyncResult) *motan.AsyncResult

type CustomTrace

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

func (*CustomTrace) Trace

func (c *CustomTrace) Trace(rid uint64, ext *motan.StringMap) *motan.TraceContext

type DebugHandler

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

DebugHandler control pprof dynamically ***the func of pprof is copied from net/http/pprof ***

func (*DebugHandler) ServeHTTP

func (d *DebugHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implement handler interface

type DynamicConfigurer

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

func NewDynamicConfigurer

func NewDynamicConfigurer(agent *Agent) *DynamicConfigurer

func (*DynamicConfigurer) Register

func (c *DynamicConfigurer) Register(url *core.URL) error

func (*DynamicConfigurer) Subscribe

func (c *DynamicConfigurer) Subscribe(url *core.URL) error

func (*DynamicConfigurer) Unregister

func (c *DynamicConfigurer) Unregister(url *core.URL) error

type DynamicConfigurerHandler

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

func (*DynamicConfigurerHandler) ServeHTTP

func (h *DynamicConfigurerHandler) ServeHTTP(res http.ResponseWriter, req *http.Request)

func (*DynamicConfigurerHandler) SetAgent

func (h *DynamicConfigurerHandler) SetAgent(agent *Agent)

type HotReload

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

func (*HotReload) ServeHTTP

func (h *HotReload) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*HotReload) SetAgent

func (h *HotReload) SetAgent(agent *Agent)

type InfoHandler

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

func (*InfoHandler) ServeHTTP

func (i *InfoHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*InfoHandler) SetAgent

func (i *InfoHandler) SetAgent(agent *Agent)

type LogHandler

type LogHandler struct{}

func (*LogHandler) ServeHTTP

func (l *LogHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type MCContext

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

func GetClientContext

func GetClientContext(confFile string) *MCContext

func GetClientContextWithFlagParse

func GetClientContextWithFlagParse(confFile string, parseFlag bool) *MCContext

func NewClientContextFromConfig

func NewClientContextFromConfig(conf *config.Config) (mc *MCContext)

func (*MCContext) GetClient

func (m *MCContext) GetClient(clientid string) *Client

func (*MCContext) GetRefer

func (m *MCContext) GetRefer(service string) interface{}

func (*MCContext) Initialize

func (m *MCContext) Initialize()

func (*MCContext) Start

func (m *MCContext) Start(extfactory motan.ExtensionFactory)

type MSContext

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

MSContext is Motan Server Context

func GetMotanServerContext

func GetMotanServerContext(confFile string) *MSContext

GetMotanServerContext start a motan server context by config a motan server context can listen multi ports and provide many services. so a single motan server context is suggested default context will be used if confFile is empty

func GetMotanServerContextWithFlagParse

func GetMotanServerContextWithFlagParse(confFile string, parseFlag bool) *MSContext

func NewMotanServerContextFromConfig

func NewMotanServerContextFromConfig(conf *config.Config) (ms *MSContext)

func (*MSContext) Initialize

func (m *MSContext) Initialize()

func (*MSContext) RegisterService

func (m *MSContext) RegisterService(s interface{}, sid string) error

RegisterService register service with serviceId for config ref. the type.string will used as serviceId if sid is not set. e.g. 'packageName.structName'

func (*MSContext) ServicesAvailable

func (m *MSContext) ServicesAvailable()

ServicesAvailable will enable all service registed in registries

func (*MSContext) ServicesUnavailable

func (m *MSContext) ServicesUnavailable()

ServicesUnavailable will enable all service registed in registries

func (*MSContext) Start

func (m *MSContext) Start(extfactory motan.ExtensionFactory)

type MeshClient

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

func NewMeshClient

func NewMeshClient() *MeshClient

func (*MeshClient) BaseCall

func (c *MeshClient) BaseCall(request core.Request, reply interface{}) core.Response

func (*MeshClient) BuildRequest

func (c *MeshClient) BuildRequest(service string, method string, args []interface{}) core.Request

func (*MeshClient) BuildRequestWithGroup

func (c *MeshClient) BuildRequestWithGroup(service string, method string, args []interface{}, group string) core.Request

func (*MeshClient) Call

func (c *MeshClient) Call(service string, method string, args []interface{}, reply interface{}) error

func (*MeshClient) Destroy

func (c *MeshClient) Destroy()

func (*MeshClient) Initialize

func (c *MeshClient) Initialize()

func (*MeshClient) SetAddress

func (c *MeshClient) SetAddress(address string)

func (*MeshClient) SetApplication

func (c *MeshClient) SetApplication(application string)

func (*MeshClient) SetRequestTimeout

func (c *MeshClient) SetRequestTimeout(requestTimeout time.Duration)

func (*MeshClient) SetSerialization

func (c *MeshClient) SetSerialization(serialization string)

type PermissionCheckFunc

type PermissionCheckFunc func(r *http.Request) bool

type SetAgent

type SetAgent interface {
	SetAgent(agent *Agent)
}

SetAgent : if need agent to do sth, the handler can implement this interface, the func SetAgent will called when agent init the handler

type StatConn

type StatConn struct {
	Listen      uint32 `json:"LISTEN"`
	Established uint32 `json:"ESTABLISHED"`
	TimeWait    uint32 `json:"TIME_WAIT"`
}

type StatConnInfo

type StatConnInfo struct {
	Fd         uint32 `json:"fd"`
	Status     string `json:"status"`
	LocalAddr  string `json:"localAddr"`
	RemoteAddr string `json:"remoteAddr"`
}

type StatIOInfo

type StatIOInfo struct {
	ReadTotal  uint64 `json:"readTotal(B)"`
	ReadRate   uint64 `json:"readRate(B/s)"`
	WriteTotal uint64 `json:"writsTotal(B)"`
	WriteRate  uint64 `json:"writsRate(B/s)"`
}

type StatMemInfo

type StatMemInfo struct {
	MemTotal    uint64  `json:"memTotal(MB)"`
	MemUsed     uint64  `json:"memUsed(MB)"`
	MemPercent  float64 `json:"memPercent"`
	SwapTotal   uint64  `json:"swapTotal(MB)"`
	SwapUsed    uint64  `json:"swapUsed(MB)"`
	SwapPercent float64 `json:"swapPercent"`
}

type StatNetInfo

type StatNetInfo struct {
	Name     string   `json:"name"`
	SentRate uint64   `json:"sentRate(B/s)"`
	RecvRate uint64   `json:"recvRate(B/s)"`
	IP       []string `json:"ip"`
}

type StatProcessEntity

type StatProcessEntity struct {
	NumThreads     int32       `json:"numThreads"`
	NumFDs         int32       `json:"numFds"`
	CpuPercent     float64     `json:"cpuPercent"`
	MemoryPercent  float32     `json:"memoryPercent"`
	UsedMemory     float64     `json:"usedMemory(MB)"`
	OpenFilesCount int32       `json:"openFilesCount"`
	CreateTime     string      `json:"createTime"`
	IO             *StatIOInfo `json:"io"`
	Connections    *StatConn   `json:"connections"`
}

type StatSystemEntity

type StatSystemEntity struct {
	CpuCores        int32         `json:"cpuCores"`
	Load1           float64       `json:"load1"`
	Load5           float64       `json:"load5"`
	Load15          float64       `json:"load15"`
	CpuPercent      float64       `json:"cpuPercent"`
	HostName        string        `json:"hostName"`
	Platform        string        `json:"platform"`
	PlatformVersion string        `json:"platformVersion"`
	KernelVersion   string        `json:"kernelVersion"`
	GoVersion       string        `json:"goVersion"`
	BootTime        string        `json:"bootTime"`
	MemInfo         *StatMemInfo  `json:"memInfo"`
	NetInfo         []StatNetInfo `json:"netInfo"`
}

type StatusHandler

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

StatusHandler can change http status, such as 200, 503 the registed services will not available when status is 503, and will available when status change to 200

func (*StatusHandler) ServeHTTP

func (s *StatusHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*StatusHandler) SetAgent

func (s *StatusHandler) SetAgent(agent *Agent)

type SwitcherHandler

type SwitcherHandler struct{}

func (*SwitcherHandler) ServeHTTP

func (s *SwitcherHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
Package cluster contains Cluster implement and command process.
Package cluster contains Cluster implement and command process.
Package core is motan-go base package.
Package core is motan-go base package.
Package endpoint is transport implement of different protocol.
Package endpoint is transport implement of different protocol.
Package filter is some default filter implements.
Package filter is some default filter implements.
Package ha is default HaStrategy implements.
Package ha is default HaStrategy implements.
Package lb is default LoadBalance implements.
Package lb is default LoadBalance implements.
********** this file is modified based on "gopkg.in/natefinch/lumberjack.v2" ********** Package lumberjack provides a rolling logger.
********** this file is modified based on "gopkg.in/natefinch/lumberjack.v2" ********** Package lumberjack provides a rolling logger.
Package protocol is motan2 protocol codec implements.
Package protocol is motan2 protocol codec implements.
Package provider is default Provider implements.
Package provider is default Provider implements.
Package registry is default registry implements.
Package registry is default registry implements.
Package serialize is default serialization implements.
Package serialize is default serialization implements.
Package server is default Server implements for different protocol.
Package server is default Server implements for different protocol.
tools

Jump to

Keyboard shortcuts

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