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
go get github.com/asim/go-micro/v3
//需要用到Micro 3.0 环境的micro 工具,可以快速构建项目。但是在代码中不使用该库。
go get github.com/micro/micro/v3
安装go-micro的consul
go get github.com/asim/go-micro/plugins/registry/consul/v3
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)
}
}
在service目录中创建服务模板
进入service目录
micro new captcha
make proto
protoc --proto_path=. --micro_out=. --go_out=:. proto/captcha.proto
修改为
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"
)
go get github.com/afocus/captcha
r.GET("/api/imagecode/:uuid",controller.GetVerificationCode)
//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
}
}
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
}
参见官方文档