xrpc

command module
v0.0.0-...-2105adc Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 6 Imported by: 0

README

xrpc

RPC 功能概述

测试用例分开执行

go test -v ./test/example_rpc_server_and_client_test.go
go test -v ./test/example_rpc_server_test.go
go test -v ./test/example_rpc_client_test.go
1、注册服务

服务配置目录在app/rpc/service/service.go

// 设置监听端口
conf := config.NewService()
addr := fmt.Sprintf(":%d", conf.Port)

// 设置中间件
middlewares := []server.Handler{middleware2.F1, middleware2.F2}

// 注册服务
srv := server.NewXRpc()
srv.Register("hello", services.Hello, middlewares...)

监听请求端口
# 该操作会阻塞,可以通过测试用例检测
go run cmd/rpc/main.go
3、服务调用
// 设置监听端口 默认为:7777
conf := config.NewService()
addr := fmt.Sprintf(":%d", conf.Port)

// 请求链接
conn, err := net.Dial("tcp", addr)
if err != nil {
    panic(err)
}

// 数据编码和解码
cli := server.NewClient(conn)

// 设置请求超时
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// 调用服务
var hello func(name string) (string, error)
cli.Call(ctx, "hello", &hello)
u, err := hello("张三")
if err != nil {
    panic(err)
}

// 输出响应
fmt.Println(u)

HTTP 功能概述

启动Http服务

http 默认端口为:7000

go run cmd/web/main.go
测试
curl http://127.0.0.1:7000/?name=zhangsan
  • RPC服务端
  • RPC客户端
  • 路由管理
  • 中间件实现
  • 多协议支持,同时支持http和rpc(多端口实现)
  • 序列化,反序列化
  • 通过server options实现超时配置
  • 安全退出,信号监听
  • 配置统一管理、待完善

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
app
api
cmd
rpc
web
core

Jump to

Keyboard shortcuts

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