dmicro

package module
v1.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2022 License: Apache-2.0 Imports: 0 Imported by: 0

README

dmicro GitHub release report card github issues github closed issues GoDoc view examples

dmicro简介

dmicro logo

dmicro是一个高效、可扩展且简单易用的微服务框架。包含drpc,easyserver等组件。

该项目的诞生离不开erpcGoFrame两个优秀的项目。

其中drpc组件参考erpc项目的架构思想,依赖的基础库是GoFrame

详细文档

中文文档

安装

go get -u -v github.com/osgochina/dmicro

推荐使用 go.mod:

require github.com/osgochina/dmicro latest
  • import
import "github.com/osgochina/dmicro"

限制

golang版本 >= 1.15

rpc服务

如何快速的通过简单的代码创建一个真正的rpc服务。 以下就是示例代码:

package main

import (
	"fmt"
	"github.com/osgochina/dmicro/drpc"
	"github.com/osgochina/dmicro/dserver"
	"github.com/osgochina/dmicro/logger"
)
// DRpcSandBox  默认的服务
type DRpcSandBox struct {
	dserver.BaseSandbox
	endpoint drpc.Endpoint
}

func (that *DRpcSandBox) Name() string {
	return "DRpcSandBox"
}

func (that *DRpcSandBox) Setup() error {
	fmt.Println("DRpcSandBox Setup")
	cfg := that.Config.EndpointConfig(that.Name())
	cfg.ListenPort = 9091
	cfg.CountTime = true
	cfg.PrintDetail = true
	that.endpoint = drpc.NewEndpoint(cfg)
	that.endpoint.RouteCall(new(Math))
	return that.endpoint.ListenAndServe()
}

func (that *DRpcSandBox) Shutdown() error {
	fmt.Println("DRpcSandBox Shutdown")
	return that.endpoint.Close()
}


// Math rpc请求的最终处理器,必须集成drpc.CallCtx
type Math struct {
	drpc.CallCtx
}

func (m *Math) Add(arg *[]int) (int, *drpc.Status) {
	// test meta
	logger.Infof("author: %s", m.PeekMeta("author"))
	// add
	var r int
	for _, a := range *arg {
		r += a
	}
	// response
	return r, nil
}

func main() {
	dserver.Authors = "osgochina@gmail.com"
	dserver.SetName("DMicro_drpc")
	dserver.Setup(func(svr *dserver.DServer) {
		err := svr.AddSandBox(new(DRpcSandBox))
		if err != nil {
			logger.Fatal(err)
		}
	})
}

rpc客户端

服务已经建立完毕,如何通过client链接它呢?


package main

import (
	"github.com/osgochina/dmicro/drpc"
	"github.com/osgochina/dmicro/drpc/message"
	"github.com/osgochina/dmicro/logger"
	"time"
)

func main() {

	cli := drpc.NewEndpoint(drpc.EndpointConfig{PrintDetail: true, RedialTimes: -1, RedialInterval: time.Second})
	defer cli.Close()
	

	sess, stat := cli.Dial("127.0.0.1:9091")
	if !stat.OK() {
		logger.Fatalf("%v", stat)
	}
    var result int
    stat = sess.Call("/math/add",
        []int{1, 2, 3, 4, 5},
        &result,
        message.WithSetMeta("author", "liuzhiming"),
    ).Status()
    if !stat.OK() {
        logger.Fatalf("%v", stat)
    }
    logger.Printf("result: %d", result)
}

通过以上的代码事例,大家基本可以了解drpc框架是怎么使用。

Documentation

Index

Constants

View Source
const Authors = "ClownFish(osgochina@gmail.com)"
View Source
const Version = "v1.0.0"

Variables

This section is empty.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
message
Package message 消息对象
Package message 消息对象
mixer/websocket/jsonSubProto
Package jsonSubProto 实现JSON套接字通信协议的。
Package jsonSubProto 实现JSON套接字通信协议的。
mixer/websocket/pbSubProto
Package pbSubProto 实现PROTOBUF套接字通信协议的。
Package pbSubProto 实现PROTOBUF套接字通信协议的。
plugin/ignorecase
Package ignorecase dynamically ignoring the case of path
Package ignorecase dynamically ignoring the case of path
examples
checkptr command
dserver command
easyservice command
easyservice_log command
event command
eventbus command
grace command
kcp command
multiclient command
protobuf command
proxy_and_seq command
quic command
quick_start command
redis command
registryetcd command
registrymdns command
simple command
simple_dserver command
supervisor command
supervisor
backoff
Package backoff 提供阻塞功能
Package backoff 提供阻塞功能

Jump to

Keyboard shortcuts

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