nothollyhigh

第 10332 号会员 /

回复了 nothollyhigh 创建的主题: 更新游戏服务器框架/组件包-KISS-新增集群示例工程

rpc server

package main

import (
	"github.com/nothollyhigh/kiss/log"
	"github.com/nothollyhigh/kiss/net"
	"time"
)

var (
	addr = "0.0.0.0:8888"
)

type HelloRequest struct {
	Message string
}

type HelloResponse struct {
	Message string
}

// Hello方法
func Hello(ctx *net.RpcContext) {
	req := &HelloRequest{}

	err := ctx.Bind(req)
	if err != nil {
		log.Error("Hello failed: %v", err)
		return
	}

	// 直接回包
	// err = ctx.Write(&HelloResponse{Message: req.Message})
	// if err != nil {
	// 	log.Error("Hello failed: %v", err)
	// 	return
	// }
	// log.Info("HelloRequest: %v", req.Message)

	// 支持异步回包
	go func() {
		err = ctx.Write(&HelloResponse{Message: req.Message})
		if err != nil {
			log.Error("Hello failed: %v", err)
			return
		}

		log.Info("HelloRequest: %v", req.Message)
	}()
}

func main() {
	server := net.NewRpcServer("Rpc")

	// 初始化方法,类似http初始化路由
	server.HandleRpcMethod("Hello", Hello)

	// 启动服务
	server.Serve(addr, time.Second*5)
}

rpc client

package main

import (
	"github.com/nothollyhigh/kiss/log"
	"github.com/nothollyhigh/kiss/net"
	"time"
)

var (
	addr = "0.0.0.0:8888"
)

type HelloRequest struct {
	Message string
}

type HelloResponse struct {
	Message string
}

func onConnected(c *net.TcpClient) {
	log.Info("RpcClient OnConnected")
}

func main() {
	engine := net.NewTcpEngine()
	client, err := net.NewRpcClient(addr, engine, nil, onConnected)
	if err != nil {
		log.Panic("NewReqClient Error: %v", err)
	}

	for {
		req := &HelloRequest{Message: "kiss"}
		rsp := &HelloResponse{}

		// 调用Hello方法
		err = client.Call("Hello", req, rsp, time.Second*3)
		if err != nil {
			log.Error("Hello failed: %v", err)
		} else {
			log.Info("HelloResponse: %v", rsp.Message)
		}

		time.Sleep(time.Second)
	}
}

回复了 nothollyhigh 创建的主题: 更新游戏服务器框架/组件包-KISS-新增集群示例工程

websocket echo server

package main

import (
	"github.com/nothollyhigh/kiss/log"
	"github.com/nothollyhigh/kiss/net"
)

var (
	addr = ":8888"

	CMD_ECHO = uint32(1)
)

func onEcho(client *net.WSClient, msg net.IMessage) {
	log.Info("ws server onEcho from %v: %v", client.Conn.RemoteAddr().String(), string(msg.Body()))
	client.SendMsg(msg)
}

func main() {
	server, err := net.NewWebsocketServer("echo", addr)
	if err != nil {
		log.Panic("NewWebsocketServer failed: %v", err)
	}

	// 初始化http ws路由
	server.HandleWs("/ws/echo")

	// 初始化协议号
	server.Handle(CMD_ECHO, onEcho)

	server.Serve()
}

websocket echo client

package main

import (
	"fmt"
	"github.com/nothollyhigh/kiss/log"
	"github.com/nothollyhigh/kiss/net"
	"time"
)

var (
	addr = "ws://localhost:8888/ws/echo"

	CMD_ECHO = uint32(1)
)

func onEcho(client *net.WSClient, msg net.IMessage) {
	log.Debug("ws client onEcho from %v: %v", client.Conn.RemoteAddr().String(), string(msg.Body()))
}

func main() {
	client, err := net.NewWebsocketClient(addr)
	if err != nil {
		log.Panic("NewWebsocketClient failed: %v, %v", err, time.Now())
	}

	// 初始化协议号
	client.Handle(CMD_ECHO, onEcho)

	for i := 0; true; i++ {
		err = client.SendMsg(net.NewMessage(CMD_ECHO, []byte(fmt.Sprintf("hello %v", i))))
		if err != nil {
			log.Error("ws client echo failed: %v", err)
			break
		}
		time.Sleep(time.Second)
	}
}

回复了 nothollyhigh 创建的主题: 更新游戏服务器框架/组件包-KISS-新增集群示例工程

tcp echo server

package main

import (
	"github.com/nothollyhigh/kiss/log"
	"github.com/nothollyhigh/kiss/net"
	"time"
)

var ()

const (
	addr = ":8888"

	CMD_ECHO = uint32(1)
)

func onEcho(client *net.TcpClient, msg net.IMessage) {
	log.Info("tcp server onEcho from %v: %v", client.Conn.RemoteAddr().String(), string(msg.Body()))
	client.SendMsg(msg)
}

func main() {
	server := net.NewTcpServer("Echo")

	// 初始化协议号
	server.Handle(CMD_ECHO, onEcho)

	server.Serve(addr, time.Second*5)
}

tcp echo client

package main

import (
	"fmt"
	"github.com/nothollyhigh/kiss/log"
	"github.com/nothollyhigh/kiss/net"
	"time"
)

var (
	addr = "127.0.0.1:8888"

	CMD_ECHO = uint32(1)
)

func onConnected(c *net.TcpClient) {
	log.Info("TcpClient OnConnected")
}

func onEcho(client *net.TcpClient, msg net.IMessage) {
	log.Debug("tcp client onEcho from %v: %v", client.Conn.RemoteAddr().String(), string(msg.Body()))
}

func main() {
	autoReconn := true
	netengine := net.NewTcpEngine()

	// 初始化协议号
	netengine.Handle(CMD_ECHO, onEcho)

	client, err := net.NewTcpClient(addr, netengine, nil, autoReconn, onConnected)
	if err != nil {
		log.Panic("NewTcpClient failed: %v, %v", client, err)
	}

	for i := 0; true; i++ {
		err = client.SendMsg(net.NewMessage(CMD_ECHO, []byte(fmt.Sprintf("hello %v", i))))
		if err != nil {
			log.Error("tcp client echo failed: %v", err)
		}
		time.Sleep(time.Second)
	}
}

回复了 fising 创建的主题: 团队准备使用 Go 构建微服务,请问大家有好的框架推荐么?

  • 有几个点需要注意:
  1. 微服务的本质就是服务拆分,服务拆分不必跟框架绑定,http+rpc基本足够了,用那些专门为微服务打造的“框架”感觉得不偿失,尤其是一些大框架,很多功能是源于大公司自己内部需求定制的,外人用起来复杂度高成本高,遇到问题还不容易解决

  2. 建议业务上可以横向拆分,尽量不要把事务性的业务进行纵向拆分

  3. 传统RPC比如GRPC,用着太麻烦了,推荐以下我的包:kiss rpc,像写http路由一样简单,欢迎使用