go语言微服务项目,高级篇--04图片验证码-redis-开发者平台说明

图片验证码

获取 注册页面 图片验证码ID

  1. 启动 web页面,点击“注册” 按钮。在 NetWork —— Headers 中 看到 错误信息!

  2. 从 URL中,提取 图片验证码ID 。 保存 成 uuid

  3. 查看 gin 框架 中文文档。—— “获取路径中的参数”

  4. web/main.go 中 添加 路由匹配:

    router.GET("/api/v1.0/imagecode/:uuid", controller.GetImageCd)
    
  5. web/controller/user.go 中 ,实现回调:

    // 获取图片信息
    func GetImageCd(ctx *gin.Context) {
    	uuid := ctx.Param("uuid")
    	fmt.Println("uuid = ", uuid)
    }
    

图片验证码获取

  • 去 github 中搜索 “captcha” 关键词。 过滤 Go语言。 —— afocus/captcha

  • 使用 go get github.com/afocus/captcha 下载源码包。

  • 参照 github 中的示例代码,测试生成 图片验证码:

    package main
    
    import (
    	"github.com/afocus/captcha"   // 按住 Ctrl ,鼠标左键点击 captcha 看到 examples, 从中可以提取到 “comic.ttf”
    	"image/color"
    	"image/png"
    	"net/http"
    )
    
    func main()  {
    	// 初始化对象
    	cap := captcha.New()
    
    	// 设置字体
    	cap.SetFont("comic.ttf")
    
    	// 设置验证码大小
    	cap.SetSize(128, 64)
    
    	// 设置干扰强度
    	cap.SetDisturbance(captcha.MEDIUM)
    
    	// 设置前景色
    	cap.SetFrontColor(color.RGBA{0,0,0, 255})
    
    	// 设置背景色
    	cap.SetBkgColor(color.RGBA{100,0,255, 255}, color.RGBA{255,0,127, 255}, color.RGBA{255,255,10, 255})
    
    	// 生成字体 -- 将图片验证码, 展示到页面中.
    	http.HandleFunc("/r", func(w http.ResponseWriter, r *http.Request) {
    		img, str := cap.Create(4, captcha.NUM)
    		png.Encode(w, img)
    
    		println(str)
    	})
    
    	// 或者 自定固定的数据,来做图片内容.
    	http.HandleFunc("/c", func(w http.ResponseWriter, r *http.Request) {
    		str := "itcast"
    		img := cap.CreateCustom(str)
    		png.Encode(w, img)
    	})
    
    	// 启动服务
    	http.ListenAndServe(":8086", nil)
    }
    

图片验证码模块集成到web

  1. 将 ImgTest.go 中的测试代码, 粘贴到 web/controller/user.go 中的 GetImageCd() 函数
  2. 导入需要的包。 将 “comic.ttf” 文件 存放到 web/conf/ 中。 对应修改访问代码。
  3. 浏览器中,“注册” 页面测试!

微服务实现 图片验证码

  1. 创建 微服务项目: micro new --type srv bj38web/service/getCaptcha

    ​ 创建完成,会在项目 bj38web 的 service/ 中,多出 getCaptcha 的微服务项目。

  2. 修改密码本 —— getCaptcha/proto/getCaptcha.proto

    syntax = "proto3";
    
    package go.micro.srv.getCaptcha;
    
    service GetCaptcha {
    	rpc Call(Request) returns (Response) {}
    }
    message Request {
    }
    message Response {
    	// 使用切片存储图片信息, 用 json 序列化
    	bytes img = 1;
    }
    
  3. 编译 proto 文件。 —— make 命令!得到 getCaptcha.micro.go 和 getCaptcha.pb.go

  4. 修改 service/getCaptcha/main.go

    import (
    	"github.com/micro/go-micro/util/log"
    	"github.com/micro/go-micro"
    	"bj38web/service/getCaptcha/handler"
    
    	getCaptcha "bj38web/service/getCaptcha/proto/getCaptcha"
    )
    
    func main() {
    	// New Service
    	service := micro.NewService(
    		micro.Name("go.micro.srv.getCaptcha"),
    		micro.Version("latest"),
    	)
    	// Register Handler
    	getCaptcha.RegisterGetCaptchaHandler(service.Server(), new(handler.GetCaptcha))
    
    	// Run service
    	if err := service.Run(); err != nil {
    		log.Fatal(err)
    	}
    }
    
  5. 修改服务发现 : mdns ——> consul

    1. 初始化consul

    2. 添加 consul 到 micro.NewSerive( )

      consulReg := consul.NewRegistry()
      
      // New Service
      service := micro.NewService(
          micro.Address("192.168.6.108:12341"),  // 防止随机生成 port 
          micro.Name("go.micro.srv.getCaptcha"),
          micro.Registry(consulReg),				// 添加注册
          micro.Version("latest"),
      )
      
    3. 启动 consul , consul agent -dev

  6. 修改 handler/getCaptcha.go 文件。

    1. 删除 除 Call 以外的 其他 所有函数。—— 受 .proto 文件制约。

    2. 清空 Call 方法。准备添加具体实现代码。

    3. 将 之前实现的图片验证码测试模块代码, 剪切到 Call方法中。

    4. 在 getCaptcha/ 下 , 添加 conf/comic.ttf

    5. 将 生成的图片验证码,写成 json 序列化数据, 通过 rsp 参数传出!

      func (e *GetCaptcha) Call(ctx context.Context, req *getCaptcha.Request, rsp *getCaptcha.Response) error {
      	// 生成图片验证码
      
      	// 初始化对象
      	cap := captcha.New()
      
      	// 设置字体
      	cap.SetFont("./conf/comic.ttf")
      
      	// 设置验证码大小
      	cap.SetSize(128, 64)
      
      	// 设置干扰强度
      	cap.SetDisturbance(captcha.NORMAL)
      
      	// 设置前景色
      	cap.SetFrontColor(color.RGBA{0,0,0, 255})
      
      	// 设置背景色
      	cap.SetBkgColor(color.RGBA{100,0,255, 255}, color.RGBA{255,0,127, 255}, color.RGBA{255,255,10, 255})
      
      	// 生成字体
      	img, _ := cap.Create(4, captcha.NUM)
      
      	// 将 生成成的图片 序列化.
      	imgBuf, _ := json.Marshal(img)
      
      	// 将 imgBuf 使用 参数 rsp 传出
      	rsp.Img = imgBuf
      
      	return nil
      }
      

web端对接微服务实现

  1. 拷贝密码本。 将 service 下的 proto/ 拷贝 web/ 下

  2. 在 GetImageCd() 中 导入包,起别名:

    getCaptcha "bj38web/web/proto/getCaptcha"

  3. 指定consul 服务发现:

    // 指定 consul 服务发现
    consulReg := consul.NewRegistry()
    
    consulService := micro.NewService(
        micro.Registry(consulReg),
    )
    
  4. 初始化客户端

    microClient := getCaptcha.NewGetCaptchaService("getCaptcha", consulService.Client())
    
    
  5. 调用远程函数

    resp, err := microClient.Call(context.TODO(), &getCaptcha.Request{})
    if err != nil {
        fmt.Println("未找到远程服务...")
        return
    }
    
  6. 将得到的数据,反序列化,得到图片数据

    var img captcha.Image
    json.Unmarshal(resp.Img, &img)
    
  7. 将图片写出到 浏览器.

    png.Encode(ctx.Writer, img)
    
  8. 测试:

    1. 启动 consul , consul agent -dev
    2. 启动 service/ 下的 main.go
    3. 启动 web/ 下的 main.go
    4. 浏览器中 192.168.IP: port/home 点击注册 查看图片验证码!

连接形式

  • 长连接:
    • TCP。—— 有状态!
  • 短连接:
    • http。—— 无状态!断开后,再发送请求,与上次发送无关!
      • 选用带有 “时效性” 的介质,存储。 —— redis数据库。setex — expire。

Redis 数据库

Redis 基本使用

  1. 修改 配置文件。 /etc/redis/redis.conf .
    • bind 地址。修改成当前主机地址。 —— 192.168.6.108
  2. port:
    • 6379
  3. 开启 redis:
    • sudo redis-server /etc/redis/redis.conf
    • 验证 : ps xua | grep redis —— iP 和 port
  4. 连接 redis :
    • redis-cli -h 192.168.6.108 -p 6379
  5. 查看所有:
    • keys *
  6. 删除所有:
    • flushall
  7. 添加一条:
    • set key value —— set hello world
  8. 获取一条:
    • get key

go语言操作 Redis

  • 从 redis.cn —— 客户端 —— go语言 —— 选择 redigo —— https://godoc.org/github.com/gomodule/redigo/redis#pkg-examples 查看 API

  • 主要分为 3 类:

    1. 连接数据库。
      • API文档中,所有以 Dial 开头。
    2. 操作数据库。
      • Do() 函数【推荐】; Send()函数, 需要配合Flush()、Receive() 3 个函数使用。
    3. 回复助手。
      • 相当于 “类型断言”。根据使用的具体数据类型,选择调用。
  • 添加测试案例:

    package main
    
    import (
    	"github.com/gomodule/redigo/redis"
    	"fmt"
    )
    
    func main()  {
    	// 1. 链接数据库
    	conn, err := redis.Dial("tcp", "192.168.6.108:6379")
    	if err != nil {
    		fmt.Println("redis Dial err:", err)
    		return
    	}
    	defer conn.Close()
    
    	// 2. 操作数据库
    	reply, err := conn.Do("set", "itcast", "itheima")
    
    	// 3. 回复助手类函数. ---- 确定成具体的数据类型
    	r, e := redis.String(reply, err)
    
    	fmt.Println(r, e)
    }
    

redis 存储 图片验证码的uuid 和 码值

  • 微服务端

    1. 修改 service/proto 中 getCaptcha.proto 的 Request 消息体,填加 uuid 成员。

      message Request {
      	string uuid = 1;
      }
      
    2. 使用 make 命令,重新生成 getCaptcha.proto 对应的文件。

    3. 遵循 MVC 代码组织架构,在 service/getCaptcha/ 中 创建 model 目录

    4. 创建 modelFunc.go 文件 封装并实现 SaveImgCode() 函数:

      // 存储图片id 到redis 数据库
      func SaveImgCode(code, uuid string) error {
      	// 1. 链接数据库
      	conn, err := redis.Dial("tcp", "192.168.6.108:6379")
      	if err != nil {
      		fmt.Println("redis Dial err:", err)
      		return err
      	}
      	defer conn.Close()
      
      	// 2. 写数据库  --- 有效时间 5 分钟
      	_, err = conn.Do("setex", uuid, 60*5, code)
      
      	return err  // 不需要回复助手!
      }
      
    5. 在 getCaptcha.go 文件的 Call() 方法中, cap.Create() 后, 调用 SaveImgCode() 传参。

  • web端

    1. 修改密码本!因为 微服务端 修改了 proto/ , 添加消息体成员。 需要重新拷贝 proto/ 到web

    2. 修改 web/controller/user.go 中 Call() 方法传参。给 Request{} 初始化。

      ```go
      resp, err := microClient.Call(context.TODO(), &getCaptcha.Request{Uuid:uuid})
      if err != nil {
          fmt.Println("未找到远程服务...")
          return
      }
      ```
    
    1. 测试验证:
      1. 确认 consul 已经启动
      2. 启动 service/ main.go
      3. 启动 web/ main.go
      4. 浏览器:192.168.6.108:8080/home — 点击“注册”。看到 图片验证码。
      5. 查看 redis 数据库, keys * 能看到 图片验证吗,对应 uuid。 校验!!

开发者平台

  • 中国移动 —— 短信验证码 业务 —— 下放到各个大公司(资质)。

  • 常用平台:

    1. 聚合数据:
    2. 京东万象:
    3. 腾讯云:
    4. 阿里云(推荐): 推荐,服务器。—— 生态好! API接口丰富。 对应开发友好度高!
  • 资料搜索。

    • 免费:—— github
    • 收费:—— 各种开发者平台。刁钻、生僻领域。
    • 最后Google。

注册阿里云账号

  1. 通过实名认证
  2. 开通短信验证码功能 —— 充值 2 元左右。
  3. 申请 AccessKey
  4. 申请签名。国内消息 —— 签名管理
  5. 申请模板。国内消息 —— 模板管理
  6. 测试使用 OpenAPI Explorer, 成功发送一条短信验证码!
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有码无尘

知识无价,有收获就好!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值