golang 基于grpc的插件框架——go-plugin 使用入门

20pzqm
• 阅读 2564

golang 基于grpc的插件框架——go-plugin 使用入门

说说我对插件的理解

大家都用过vscode,当我们想要在vscode中格式化json的时候,很简单,去插件市场安装一个json tools就好了;想要使用eclipse的键盘快捷方式,安装一个eclipse keymap 就可以. 由此可见,插件帮助我们扩展原有程序的功能,同时它与原有工程是解耦的,可以独立开发。 总结下:

  • 插件架构的功能
    • 为系统提供扩展能力
    • 不侵入系统现有功能
  • 插件的好处
    • Host与插件的代码解耦,独立开发
    • Host 只关注插件的接口,不关注实现细节
    • Host动态引入插件,因而可以自由定制所需的能力,避免部署包的体积过大
    • 插件可以独立升级

      一些常见的插件架构设计思路

  1. 共享库方式。
    • 优点:动态编译,发布件小
    • 缺点: a. 共享库,决定了发布件必然是动态编译构建的,跨平台能力会比较弱。比如你要开发个https下载功能,要依赖libssl吧,而libssl又依赖glibc,于是这个依赖链就产生了各种版本上的强依赖。 b. 不安全。共享库方式调用插件代码,相当于把共享库的代码附加到当前进程,其函数可以直接访问当前主系统进程的内存空间,不仅不安全,而且如果插件代码质量低,可能导致主系统直接崩溃 c. 共享库的日志输出到主系统很麻烦 d. 如果用c语言开发还好,要是换个编程语言,还要涉及到数据类型的转换,恶心。。。
  2. 基于轻量通信协议的方式
    • 优点:能解决以上所有问题

go-plugin

接下来我要介绍下github.com/hashicorp/go-plugin,go-plugin使用grpc协议来完成插件与主平台的接口调用. (不熟悉GRPC的话,后文阅读起来可能比较难懂)

UML图

golang 基于grpc的插件框架——go-plugin 使用入门

请对照UML图浏览后续内容,更有助于理解

讲解

proto

我们基于一个非常简单的protobuf来实现插件与HOST的通信

//protoc -I proto/ proto/print.proto --go_out=plugins=grpc:proto/ --go_out=. --go_opt=paths=source_relative
syntax = "proto3";
option go_package = "github.com/sxy/try-go-plugin/proto";

package proto;

message Empty {}

service HelloPlugin {
  // Sends a greeting
  rpc Hello (Request) returns (Response) {}
}

// 对应uml中的request
message Request{
  string name = 1;
}
// 对应uml的response
message Response{
  string result = 1;
}

定义IHelloService 接口

Host将插件实例化后的对象识别为接口——IHelloService接口,我们简单定义一个IHelloService

type IHelloService interface {
    Hello(name string) (string, error)
}

插件中会完成对IHelloService的实现——HelloService

type PluginService struct{}

func (p PluginService) Hello(name string) (string, error) {
    return "hello " + name, nil
}

(重要) 定义GRPCPlugin

插件要里通过go-plugin框架来注册一个 GRPCPlugin 实例, Host 会利用go-plugin的框架,来导入GRPCPlugin实例

GRPCPlugin接口时go-plugin提供的grpc 插件标准接口,只有两个成员函数

// GRPCServer 负责注册一个grpc server,本例中就是实现proto.HelloPluginServer的struct实例.
GRPCServer(*GRPCBroker, *grpc.Server) error

// GRPCClient 要返回一个实现了IHelloService接口的struct实例, 同时利用本方法传入的grpcConnection实例来调用proto.NewHelloPluginClient生成grpc通信所需的客户端实例, 这样它就能作为IHelloService接口方法与HelloPlugin接口(GRPC生成代码)的适配层.
GRPCClient(context.Context, *GRPCBroker, *grpc.ClientConn) (interface{}, error)

看代码就一目了然了.

// GRPCHelloPlugin implement plugin.GRPCPlugin
type GRPCHelloPlugin struct {
    plugin.Plugin
    Impl IHelloService
}

// 注册HelloPluginServer
func (p GRPCHelloPlugin) GRPCServer(broker *plugin.GRPCBroker, server *grpc.Server) error {
    proto.RegisterHelloPluginServer(server, GPRCHelloPluginServerWrapper{impl: p.Impl})
    return nil
}

// Host去获取插件的实例时,就掉用这个方法,将HelloPluginClient 作为 GRPCHelloPluginClientWrapper 的成员并返回GRPCHelloPluginClientWrapper
// 同时GRPCHelloPluginClientWrapper 也实现了IHelloService
func (p GRPCHelloPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, conn *grpc.ClientConn) (interface{}, error) {
    return GRPCHelloPluginClientWrapper{client: proto.NewHelloPluginClient(conn)}, nil
}

type GPRCHelloPluginServerWrapper struct {
    impl IHelloService
    proto.UnimplementedHelloPluginServer
}

func (_this GPRCHelloPluginServerWrapper) Hello(ctx context.Context, request *proto.Request) (*proto.Response, error) {
    r, _ := _this.impl.Hello(request.Name)
    return &proto.Response{
        Result: r,
    }, nil
}

// GRPCHelloPluginClientWrapper 作为server 调用插件接口的包装器,
type GRPCHelloPluginClientWrapper struct {
    client proto.HelloPluginClient
}

func (_this GRPCHelloPluginClientWrapper) Hello(name string) (string, error) {
    in := proto.Request{Name: name}
    resp, err := _this.client.Hello(context.Background(), &in)
    if err != nil {
        return "", err
    } else {
        return resp.Result, nil
    }
}

插件 main 启动grpc并注册自己的服务

func main() {
    plugin.Serve(&plugin.ServeConfig{
        HandshakeConfig: shared.Handshake,
        Plugins: map[string]plugin.Plugin{
            "PrintPlugin": &shared.GRPCHelloPlugin{Impl: &PluginService{}},
        },
        // A non-nil value here enables gRPC serving for this plugin...
        GRPCServer: plugin.DefaultGRPCServer,
    })
}

Host 调用插件

func main() {
    log.SetOutput(os.Stdout)
    pluginClientConfig := &plugin.ClientConfig{
        HandshakeConfig:  shared.Handshake,
        // helloPlugin.exe 是我们编译插件得到的可执行文件
        Cmd:              exec.Command("./helloPlugin.exe"),
        // Host 只使用 GRPCHelloPlugin 的 GRPCClient 方法,无需使用任何GRPCHelloPlugin内部成员
        Plugins:          map[string]plugin.Plugin{"main": &shared.GRPCHelloPlugin{}},
        AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
    }

    client := plugin.NewClient(pluginClientConfig)
    pluginClientConfig.Reattach = client.ReattachConfig()
    protocol, err := client.Client()
    if err != nil {
        log.Fatalln(err)
    }
    // 实例化,此处实例化得到的其实就是 GRPCHelloPluginClientWrapper
    raw, err := protocol.Dispense("main")
    if err != nil {
        log.Fatalln(err)
    }
    // 类型断言为IHelloService接口, 也可以用反射调用函数
    service := raw.(shared.IHelloService)
    res, err := service.Hello("sxy")
    if err != nil {
        log.Fatalln(err)
    }
    log.Println(res)
}

最终效果

编译插件

go build -o helloPlugin.exe plugin/plugin.go

运行server

go build -o server.exe server/server.go

golang 基于grpc的插件框架——go-plugin 使用入门

完成代码实现

try-go-plugin

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Eclipse插件开发_学习_00_资源帖
一、官方资料 1.eclipseapi(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fhelp.eclipse.org%2Fmars%2Findex.jsp%3Ftopic%3D%252Forg.eclipse.platform.doc.isv%252Fguide%2
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这