motan

package module
v0.0.0-...-b9d6073 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2018 License: Apache-2.0 Imports: 25 Imported by: 9

README

Motan-go

License Build Status 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", "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", "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

This section is empty.

Variables

This section is empty.

Functions

func AddDefaultExt

func AddDefaultExt(d motan.ExtentionFactory)

func GetDefaultExtFactory

func GetDefaultExtFactory() motan.ExtentionFactory

Types

type Agent

type Agent struct {
	ConfigFile string

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

func NewAgent

func NewAgent(extfactory motan.ExtentionFactory) *Agent

func (*Agent) RegisterManageHandler

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

func (*Agent) SetSanpshotConf

func (a *Agent) SetSanpshotConf()

func (*Agent) StartMotanAgent

func (a *Agent) StartMotanAgent()

func (*Agent) StatusChangeHandler

func (a *Agent) StatusChangeHandler(w http.ResponseWriter, r *http.Request)

StatusChangeHandler change agent server status, and set registed services available or unavailable.

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 (*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 MCContext

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

func GetClientContext

func GetClientContext(confFile string) *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.ExtentionFactory)

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 (*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.ExtentionFactory)

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.
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.

Jump to

Keyboard shortcuts

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