当前位置: 首页 > 工具软件 > go-captcha > 使用案例 >

go-micro v3版本安装并配置案例

慕鸿波
2023-12-01

1. go-micro 安装

1.1 安装protobuf

go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/asim/go-micro/cmd/protoc-gen-micro/v3

1.2 安装go-micro框架

go get github.com/asim/go-micro/v3

1.3 安装micro

//需要用到Micro 3.0 环境的micro 工具,可以快速构建项目。但是在代码中不使用该库。
go get github.com/micro/micro/v3

2.Consul服务发现配置

  1. 安装go-micro的consul

    go get github.com/asim/go-micro/plugins/registry/consul/v3
    
  2. service/captcha/main.go

    package main
    
    import (
    	"github.com/asim/go-micro/plugins/registry/consul/v3"
    	"github.com/asim/go-micro/v3"
    	"github.com/asim/go-micro/v3/logger"
    	"renthome/service/Captcha/handler"
    	proto "renthome/service/Captcha/proto"
    )
    
    func main() {
    	//Consul Registry
    	consulRegistry := consul.NewRegistry()
    	// Create service
    	srv := micro.NewService(
    		micro.Registry(consulRegistry),
    		micro.Address("127.0.0.1:10001"),
    		micro.Name("captcha"),
    		micro.Version("latest"),
    	)
    
    	// Register handler
    	proto.RegisterCaptchaHandler(srv.Server(), new(handler.C))
    
    	// Run service
    	if err := srv.Run(); err != nil {
    		logger.Fatal(err)
    	}
    }
    

3. 生成服务模板

在service目录中创建服务模板
进入service目录
micro new captcha

3.1 编译proto文件

make proto
protoc --proto_path=. --micro_out=. --go_out=:. proto/captcha.proto

3.2 修改main.go文件中的依赖

修改为

import (
	"github.com/asim/go-micro/plugins/registry/consul/v3"
	"github.com/asim/go-micro/v3"
	"github.com/asim/go-micro/v3/logger"
	"renthome/service/Captcha/handler"
	proto "renthome/service/Captcha/proto"
)

4. 图片验证码服务

4.1 下载图片验证码库

go get github.com/afocus/captcha

4.2 提供接口

r.GET("/api/imagecode/:uuid",controller.GetVerificationCode)

4.3 controller层处理

//GetImageCaptcha 获取图片验证码
func GetImageCaptcha(c *gin.Context) {
	//指定服务发现
	consulReg := consul.NewRegistry()
	consulService := micro.NewService(
		micro.Registry(consulReg),
	)
	//获取url参数
	uuid := c.Param("uuid")
	//获取指定服务客户端
	client := CaptchaPB.NewCaptchaService("captcha", consulService.Client())
	//通过客户端调用服务并接收返回值
	response, err := client.SendImageCaptcha(c, &CaptchaPB.SendImageCaptchaRequest{
		Uuid: uuid,
	})
	if err != nil {
		fmt.Println(err)
	}
	var image captcha.Image
	err = json.Unmarshal(response.Image, &image)
	if err != nil {
		fmt.Println("json.Unmarshal error:", err)
		return
	}
	err = png.Encode(c.Writer, image)
	if err != nil {
		fmt.Println("png.Encode error:", err)
		return
	}
}

4.4 service处理

service/captcha/handler/captcha.go

package handler

import (
	"context"
	"encoding/json"
	Captcha "github.com/afocus/captcha"
	"github.com/asim/go-micro/v3/util/log"
	"renthome/cache"
	proto "renthome/service/Captcha/proto"
)

type C struct{}

//SendImageCaptcha 发送图片验证码
func (e *C) SendImageCaptcha(ctx context.Context, req *proto.SendImageCaptchaRequest, rsp *proto.SendImageCaptchaResponse) error {
	captcha := Captcha.New()
	//设置字符集
	if err := captcha.SetFont("service/Captcha/conf/comic.ttf"); err != nil {
		return err
	}
	img, code := captcha.Create(4, Captcha.NUM)
	data, err := json.Marshal(img)
	if err != nil {
		return err
	}
	rsp.Image = data
	//调用redis存入验证码
	err = cache.SaveImageCaptcha(ctx, req.Uuid, code)
	if err != nil {
		return err
	}
	log.Info("Received Captcha.SendImageCaptcha request")
	return nil
}

//SendSMSCaptcha 发送短信验证码
func (e *C) SendSMSCaptcha(ctx context.Context, req *proto.SendSMSCaptchaRequest, rsp *proto.SendSMSCaptchaResponse) error {
	smsCaptcha, err := GenerateSMSCode(ctx, req.Phone, req.Label)
	if err != nil {
		return err
	}
	rsp.Code = smsCaptcha
	err = cache.SaveSMSCaptcha(ctx, req.Phone, req.Label, smsCaptcha)
	if err != nil {
		return err
	}
	return nil
}

5. 阿里云OSS

参见官方文档

 类似资料: