GoFrame 微服务开发指南

发布于:2025-02-10 ⋅ 阅读:(25) ⋅ 点赞:(0)

基本介绍

GoFrame 框架支持微服务模式开发,提供了常用的微服务组件、开发工具、开发教程帮助团队快速微服务转型。

微服务组件简介

GoFrame 微服务组件具有低耦合及通用化设计,组件化使用支持大部分的微服务通信协议。在官方文档中,主要以 HTTP 及 GRPC 协议为示例,介绍微服务的开发以及组件工具的使用。

HTTP 微服务示例

服务端代码 (server.go)

package main

import (
    "github.com/gogf/gf/contrib/registry/file/v2"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
    "github.com/gogf/gf/v2/net/gsvc"
    "github.com/gogf/gf/v2/os/gfile"
)

func main() {
    // 使用文件系统作为服务注册发现组件
    gsvc.SetRegistry(file.New(gfile.Temp("gsvc")))

    // 创建服务并绑定处理器
    s := g.Server(`hello.svc`)
    s.BindHandler("/", func(r *ghttp.Request) {
        g.Log().Info(r.Context(), `request received`)
        r.Response.Write(`Hello world`)
    })
    s.Run()
}

客户端代码 (client.go)

package main

import (
    "time"

    "github.com/gogf/gf/contrib/registry/file/v2"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/gsvc"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/os/gfile"
)

func main() {
    // 使用文件系统作为服务注册发现组件
    gsvc.SetRegistry(file.New(gfile.Temp("gsvc")))

    // 创建客户端并访问服务
    client := g.Client()
    for i := 0; i < 10; i++ {
        ctx := gctx.New()
        res, err := client.Get(ctx, `http://hello.svc/`)
        if err != nil {
            panic(err)
        }
        g.Log().Debug(ctx, res.ReadAllString())
        res.Close()
        time.Sleep(time.Second)
    }
}

关键点说明

  1. 服务注册发现:

    • 使用 gsvc.SetRegistry() 设置服务注册组件
    • 本示例使用文件系统注册发现,仅适用于本地测试
    • 生产环境推荐使用 etcd, polaris, zookeeper 等组件
  2. 服务命名:

    • 为服务器设置唯一标识 hello.svc
    • 客户端通过服务名 http://hello.svc/ 访问服务
    • 底层自动进行服务地址解析和通信

GRPC 微服务示例

Protobuf 定义 (helloworld.proto)

syntax = "proto3";

package protobuf;

option go_package = "github.com/gogf/gf/grpc/example/helloworld/protobuf";

// 定义服务
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// 请求消息
message HelloRequest {
  string name = 1;
}

// 响应消息
message HelloReply {
  string message = 1;
}

控制器实现 (controller.go)

type Controller struct {
    protobuf.UnimplementedGreeterServer
}

func Register(s *grpcx.GrpcServer) {
    protobuf.RegisterGreeterServer(s.Server, &Controller{})
}

// 实现 SayHello 方法
func (s *Controller) SayHello(ctx context.Context, in *protobuf.HelloRequest) (*protobuf.HelloReply, error) {
    return &protobuf.HelloReply{Message: "Hello " + in.GetName()}, nil
}

服务端代码 (server.go)

package main

import (
    "github.com/gogf/gf/contrib/rpc/grpcx/v2"
    "github.com/gogf/gf/example/rpc/grpcx/basic/controller"
)

func main() {
    s := grpcx.Server.New()
    controller.Register(s)
    s.Run()
}

客户端代码 (client.go)

package main

import (
    "github.com/gogf/gf/contrib/rpc/grpcx/v2"
    "github.com/gogf/gf/example/rpc/grpcx/basic/protobuf"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
)

func main() {
    var (
        ctx    = gctx.New()
        conn   = grpcx.Client.MustNewGrpcClientConn("demo")
        client = protobuf.NewGreeterClient(conn)
    )
    res, err := client.SayHello(ctx, &protobuf.HelloRequest{Name: "World"})
    if err != nil {
        g.Log().Error(ctx, err)
        return
    }
    g.Log().Debug(ctx, "Response:", res.Message)
}

GRPC 特点

  1. 使用 Protobuf 定义接口和数据结构
  2. 通过 gf gen pb 命令生成 Go 代码
  3. 服务端实现 Protobuf 定义的接口
  4. 客户端通过服务名创建连接并调用远程方法

总结

GoFrame 提供了灵活、低耦合的微服务开发组件,支持多种服务注册发现方式和通信协议,帮助开发者快速构建现代微服务架构。


网站公告

今日签到

点亮在社区的每一天
去签到