nrpc

package module
v0.0.0-...-3faa04f Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: MIT Imports: 18 Imported by: 0

README

nrpc

基于consul、TCP、msgpack的简单RPC

Demo
安装consul

docker-compose.yml

version: '3'

services:

consul-agent-1:
    image: consul:latest
    networks:
    - consul-demo
    command: "agent -retry-join consul-server-bootstrap -client 0.0.0.0"
    ports:
    - "8501:8500"

consul-agent-2: 
    image: consul:latest
    networks:
    - consul-demo
    command: "agent -retry-join consul-server-bootstrap -client 0.0.0.0"
    ports:
    - "8502:8500"

consul-agent-3: 
    image: consul:latest
    networks:
    - consul-demo
    command: "agent -retry-join consul-server-bootstrap -client 0.0.0.0"
    ports:
    - "8503:8500"

consul-server-1: 
    image: consul:latest
    networks:
    - consul-demo
    command: "agent -server -retry-join consul-server-bootstrap -client 0.0.0.0"

consul-server-2:
    image: consul:latest
    networks:
    - consul-demo
    command: "agent -server -retry-join consul-server-bootstrap -client 0.0.0.0"

consul-server-bootstrap:
    image: consul:latest
    networks:
    - consul-demo
    ports:
    - "8400:8400"
    - "8500:8500"
    - "8600:8600"
    - "8600:8600/udp"
    command: "agent -server -bootstrap-expect 3 -ui -client 0.0.0.0"

networks:
consul-demo:

docker-compose docker-compost up

go get
go get -u -v github.com/yinshuwei/nrpc
服务端代码

server.go

package main

import (
    "log"
    "github.com/yinshuwei/nrpc"
)

// Args Args
type Args struct {
    A, B int
}

// Reply Reply
type Reply struct {
    C int
}

// Arith Arith
type Arith struct{}

// Add Add
func (t *Arith) Add(args *Args, reply *Reply) error {
    reply.C = args.A + args.B
    log.Println(*args)
    return nil
}

func main() {
    sever := nrpc.NewServer()
    sever.Register(&Arith{})
    err := sever.Serve(
        "carts_v4", // server_name
        []string{"127.0.0.1:8109", "127.0.0.1:8501", "127.0.0.1:8502", "127.0.0.1:8503"}, // consol address
        "172.17.0.1",                        // local ip
        []int{8001, 8002, 8003, 8004, 8005}, // local ports
    )
    if err != nil {
        log.Println(err)
    }
}
客户端代码

client.go

package main

import (
    "log"
    "github.com/yinshuwei/nrpc"
)

func main() {
    log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

    cli, err := nrpc.Dial(
        "carts_v4", // server_name
        []string{"127.0.0.1:8109", "127.0.0.1:8501", "127.0.0.1:8502", "127.0.0.1:8503"}, // consol address
    )
    if err != nil {
        log.Println(err)
    }

    reply := map[string]int{}
    err = cli.Call("Arith.Add", map[string]int{"A": 1, "B": 5}, &reply)
    if err != nil {
        log.Println(err)
    }

    log.Println(reply["C"])
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(conn io.ReadWriteCloser) *rpc.Client

NewClient returns a new rpc.Client to handle requests to the set of services at the other end of the connection.

Types

type Client

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

Client Client

func Dial

func Dial(serviceName string, consuls []string) (*Client, error)

Dial Dial

func (*Client) Call

func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error

Call invokes the named function, waits for it to complete, and returns its error status.

type Server

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

Server Server

func NewServer

func NewServer() *Server

NewServer NewServer

func (*Server) Accept

func (server *Server) Accept(lis net.Listener) error

Accept accepts connections on the listener and serves requests for each incoming connection. Accept blocks until the listener returns a non-nil error. The caller typically invokes Accept in a go statement.

func (*Server) Register

func (server *Server) Register(rcvr interface{}) error

Register Register

func (*Server) Serve

func (server *Server) Serve(serviceName string, consuls []string, localIP string, portRange []int) error

Serve Serve

func (*Server) ServeConn

func (server *Server) ServeConn(conn io.ReadWriteCloser)

ServeConn runs the MSGPACK-RPC server on a single connection. ServeConn blocks, serving the connection until the client hangs up. The caller typically invokes ServeConn in a go statement.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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