golang分布式可插拔RPC服务框架插件库rpcx的使用

Golang分布式可插拔RPC服务框架插件库rpcx的使用

简介

rpcx是一个类似阿里巴巴Dubbo和微博Motan的RPC框架,具有以下特点:

  1. 简单:易于学习、开发、集成和部署
  2. 高性能:性能优于grpc-go
  3. 跨平台:支持原始字节切片、JSON、Protobuf和MessagePack
  4. 服务发现和治理:支持zookeeper、etcd和consul

安装

安装基础功能:

go get -v github.com/smallnest/rpcx/...

如果需要使用quic、kcp等功能,可以使用tags:

go get -v -tags "quic kcp" github.com/smallnest/rpcx/...

示例代码

服务端示例

// 定义服务
type Arith struct{}

func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
    reply.C = args.A * args.B
    return nil
}

func main() {
    // 创建服务器
    s := server.NewServer()
    // 注册服务
    s.RegisterName("Arith", new(Arith), "")
    // 启动服务
    s.Serve("tcp", "127.0.0.1:8972")
}

客户端示例

func main() {
    // 创建服务发现
    d, _ := client.NewPeer2PeerDiscovery("tcp@127.0.0.1:8972", "")
    
    // 创建XClient
    xclient := client.NewXClient(
        "Arith", 
        client.Failtry, 
        client.RandomSelect, 
        d, 
        client.DefaultOption,
    )
    defer xclient.Close()
    
    // 准备参数
    args := &Args{A: 10, B: 20}
    reply := &Reply{}
    
    // 调用服务
    err := xclient.Call(context.Background(), "Mul", args, reply, nil)
    if err != nil {
        log.Fatalf("failed to call: %v", err)
    }
    
    fmt.Printf("%d * %d = %d", args.A, args.B, reply.C)
}

主要特性

  • 支持原始Go函数,无需定义proto文件
  • 可插拔架构,功能可扩展
  • 支持TCP、HTTP、QUIC和KCP协议
  • 支持多种编解码器:JSON、Protobuf、MessagePack等
  • 服务发现支持peer2peer、zookeeper、etcd、consul等
  • 故障容错:Failover、Failfast、Failtry
  • 负载均衡:随机、轮询、一致性哈希等
  • 支持压缩、元数据传递、授权等功能

性能

rpcx在各种测试场景下都表现出优异的性能,特别是在高并发情况下。测试结果显示rpcx的性能优于大多数RPC框架。

更多示例

你可以在rpcx-examples找到更多完整示例。

贡献

欢迎贡献:

  • 提交问题或需求
  • 发送PR
  • 编写使用rpcx的项目
  • 编写教程或文章介绍rpcx

许可证

Apache License, Version 2.0


更多关于golang分布式可插拔RPC服务框架插件库rpcx的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang分布式可插拔RPC服务框架插件库rpcx的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang分布式可插拔RPC服务框架rpcx使用指南

rpcx是一个高性能、可插拔的分布式RPC框架,支持多种服务注册中心和服务发现机制。下面我将详细介绍rpcx的使用方法。

1. 安装rpcx

go get -u github.com/smallnest/rpcx/...

2. 基本使用示例

服务端代码

package main

import (
	"context"
	"flag"

	"github.com/smallnest/rpcx/server"
)

type Args struct {
	A int
	B int
}

type Reply struct {
	C int
}

type Arith struct{}

func (t *Arith) Mul(ctx context.Context, args *Args, reply *Reply) error {
	reply.C = args.A * args.B
	return nil
}

func main() {
	flag.Parse()

	s := server.NewServer()
	s.RegisterName("Arith", new(Arith), "")
	s.Serve("tcp", ":8972")
}

客户端代码

package main

import (
	"context"
	"flag"
	"fmt"
	"log"

	"github.com/smallnest/rpcx/client"
)

type Args struct {
	A int
	B int
}

type Reply struct {
	C int
}

func main() {
	flag.Parse()

	d := client.NewPeer2PeerDiscovery("tcp@localhost:8972", "")
	xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption)
	defer xclient.Close()

	args := &Args{
		A: 10,
		B: 20,
	}

	reply := &Reply{}
	err := xclient.Call(context.Background(), "Mul", args, reply)
	if err != nil {
		log.Fatalf("failed to call: %v", err)
	}

	fmt.Printf("%d * %d = %d\n", args.A, args.B, reply.C)
}

3. 高级特性

3.1 服务注册与发现

rpcx支持多种服务注册中心:

// 使用etcd作为注册中心
d := client.NewEtcdDiscovery(
    "/rpcx_test",
    "Arith",
    []string{"localhost:2379"},
    nil,
)

// 使用Zookeeper
d := client.NewZooKeeperDiscovery(
    "/rpcx_test",
    "Arith",
    []string{"localhost:2181"},
    nil,
)

3.2 插件机制

rpcx支持多种插件:

// 服务端添加插件
s := server.NewServer()

// 添加IP限流插件
s.Plugins.Add(&server.IpRestrictionPlugin{
    Whitelist: []string{"127.0.0.1"},
})

// 添加限流插件
s.Plugins.Add(&server.LimitPlugin{
    Limit: 1000, // 每秒1000次调用
})

// 添加鉴权插件
s.Plugins.Add(&server.AuthPlugin{
    Authenticate: func(token string) error {
        if token != "secret-token" {
            return errors.New("invalid token")
        }
        return nil
    },
})

3.3 负载均衡策略

rpcx提供多种负载均衡策略:

// 随机选择
xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption)

// 轮询
xclient := client.NewXClient("Arith", client.Failtry, client.RoundRobin, d, client.DefaultOption)

// 一致性哈希
xclient := client.NewXClient("Arith", client.Failtry, client.ConsistentHash, d, client.DefaultOption)

3.4 容错模式

// 快速失败
xclient := client.NewXClient("Arith", client.Failfast, client.RandomSelect, d, client.DefaultOption)

// 重试
xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, client.DefaultOption)

// 故障转移
xclient := client.NewXClient("Arith", client.Failover, client.RandomSelect, d, client.DefaultOption)

4. 性能优化

4.1 编解码器选择

// 使用MessagePack编解码
option := client.DefaultOption
option.SerializeType = protocol.MsgPack

xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)

4.2 连接池配置

option := client.DefaultOption
option.IdleTimeout = 10 * time.Minute
option.PoolSize = 100

xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)

5. 监控与追踪

// 添加Prometheus监控
s.Plugins.Add(&server.PrometheusPlugin{})

// 添加OpenTracing支持
s.Plugins.Add(&server.OpenTracingPlugin{})

总结

rpcx是一个功能强大且灵活的RPC框架,具有以下特点:

  1. 高性能,支持多种编解码方式
  2. 丰富的服务发现机制
  3. 可插拔的插件系统
  4. 多种负载均衡和容错策略
  5. 完善的监控和追踪支持

通过合理配置,rpcx可以满足从简单到复杂的各种分布式系统需求。

回到顶部