C++GO语言微服务之图片、短信验证码生成及存储

目录

[01 session的处理](#01 session的处理)

[02 获取网页图片验证码ID](#02 获取网页图片验证码ID)

[03 测试图片验证码](#03 测试图片验证码)

[04 图片验证码模块集成](#04 图片验证码模块集成)

[05 图片验证码功能移植微服务](#05 图片验证码功能移植微服务)

[06 图片验证码功能对接微服务的web实现](#06 图片验证码功能对接微服务的web实现)

[07 对接微服务的web实现步骤小结](#07 对接微服务的web实现步骤小结)

[08 Redis数据库基本操作回顾](#08 Redis数据库基本操作回顾)

[09 go语言操作Redis数据库API介绍](#09 go语言操作Redis数据库API介绍)

[10 go语言操作Redis数据库-测试案例](#10 go语言操作Redis数据库-测试案例)

[11 在微服务端添加redis数据库存储](#11 在微服务端添加redis数据库存储)

[12 对应web端实现及流程总结](#12 对应web端实现及流程总结)


01 session的处理

图片验证码

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

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

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

  3. 查看 gin 框架 中文文档。------ "获取路径中的参数"

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

```go

router.GET("/api/v1.0/imagecode/:uuid", controller.GetImageCd)

```

  1. web/controller/user.go 中 ,实现回调:

```go

// 获取图片信息

func GetImageCd(ctx *gin.Context) {

uuid := ctx.Param("uuid")

fmt.Println("uuid = ", uuid)

}

```

02 获取网页图片验证码ID

图片验证码获取

```go

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)

}

```

03 测试图片验证码

图片验证码模块集成到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 的微服务项目。

  1. 修改密码本 ------ getCaptcha/proto/getCaptcha.proto

```protobuf

syntax = "proto3";

package go.micro.srv.getCaptcha;

service GetCaptcha {

rpc Call(Request) returns (Response) {}

}

message Request {

}

message Response {

// 使用切片存储图片信息, 用 json 序列化

bytes img = 1;

}

```

  1. 编译 proto 文件。 ------ make 命令!得到 getCaptcha.micro.go 和 getCaptcha.pb.go

  2. 修改 service/getCaptcha/main.go

```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)

}

}

```

  1. 修改服务发现 : mdns ------> consul

  2. 初始化consul

  3. 添加 consul 到 micro.NewSerive( )

```go

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"),

)

```

  1. 启动 consul , consul agent -dev

04 图片验证码模块集成

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

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

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

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

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

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

```go

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

}

```

05 图片验证码功能移植微服务

web端对接微服务实现

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

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

`getCaptcha "bj38web/web/proto/getCaptcha" `

  1. 指定consul 服务发现:

```go

// 指定 consul 服务发现

consulReg := consul.NewRegistry()

consulService := micro.NewService(

micro.Registry(consulReg),

)

```

  1. 初始化客户端

```go

microClient := getCaptcha.NewGetCaptchaService("getCaptcha", consulService.Client())

```

  1. 调用远程函数

```go

resp, err := microClient.Call(context.TODO(), &getCaptcha.Request{})

if err != nil {

fmt.Println("未找到远程服务...")

return

}

```

  1. 将得到的数据,反序列化,得到图片数据

```go

var img captcha.Image

json.Unmarshal(resp.Img, &img)

```

  1. 将图片写出到 浏览器.

```go

png.Encode(ctx.Writer, img)

```

  1. 测试:

  2. 启动 consul , consul agent -dev

  3. 启动 service/ 下的 main.go

  4. 启动 web/ 下的 main.go

  5. 浏览器中 192.168.IP: port/home 点击注册 查看图片验证码!

06 图片验证码功能对接微服务的web实现

连接形式

  • 长连接:

  • TCP。------ 有状态!

  • 短连接:

  • http。------ 无状态!断开后,再发送请求,与上次发送无关!

  • 选用带有 "时效性" 的介质,存储。 ------ redis数据库。setex --- expire。

07 对接微服务的web实现步骤小结

Redis 数据库

Redis 基本使用

  1. 修改 配置文件。 /etc/redis/redis.conf .
  • bind 地址。修改成当前主机地址。 ------ 192.168.6.108
  1. port:
  • 6379
  1. 开启 redis:
  • sudo redis-server /etc/redis/redis.conf

  • 验证 : ps xua | grep redis ------ iP 和 port

  1. 连接 redis :
  • redis-cli -h 192.168.6.108 -p 6379
  1. 查看所有:
  • keys *
  1. 删除所有:
  • flushall
  1. 添加一条:
  • set key value ------ set hello world
  1. 获取一条:
  • get key

08 Redis数据库基本操作回顾

go语言操作 Redis

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

  • 添加测试案例:

```go

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)

}

```

09 go语言操作Redis数据库API介绍

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

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

```go

message Request {

string uuid = 1;

}

```

  1. 使用 make 命令,重新生成 getCaptcha.proto 对应的文件。

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

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

10 go语言操作Redis数据库-测试案例

```go

// 存储图片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 // 不需要回复助手!

}

```

  1. 在 getCaptcha.go 文件的 Call() 方法中, cap.Create() 后, 调用 SaveImgCode() 传参。

11 在微服务端添加redis数据库存储

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

}

```

12 对应web端实现及流程总结

  1. 测试验证:

  2. 确认 consul 已经启动

  3. 启动 service/ main.go

  4. 启动 web/ main.go

  5. 浏览器:192.168.6.108:8080/home --- 点击"注册"。看到 图片验证码。

  6. 查看 redis 数据库, keys * 能看到 图片验证吗,对应 uuid。 校验!!

开发者平台

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

  • 常用平台:

  1. 聚合数据:

  2. 京东万象:

  3. 腾讯云:

  4. 阿里云(推荐): 推荐,服务器。------ 生态好! API接口丰富。 对应开发友好度高!

  • 资料搜索。

  • 免费:------ github

  • 收费:------ 各种开发者平台。刁钻、生僻领域。

  • 最后Google。

注册阿里云账号

  1. 通过实名认证

  2. 开通短信验证码功能 ------ 充值 2 元左右。

  3. 申请 AccessKey

  4. 申请签名。国内消息 ------ 签名管理

  5. 申请模板。国内消息 ------ 模板管理

  6. 测试使用 OpenAPI Explorer, 成功发送一条短信验证码!

相关推荐
钢铁男儿38 分钟前
C# 方法(栈帧)
开发语言·c#
忆源3 小时前
【Qt】之音视频编程1:QtAV的背景和安装篇
开发语言·qt·音视频
敲键盘的小夜猫3 小时前
Python核心数据类型全解析:字符串、列表、元组、字典与集合
开发语言·python
巨龙之路6 小时前
C语言中的assert
c语言·开发语言
2301_776681657 小时前
【用「概率思维」重新理解生活】
开发语言·人工智能·自然语言处理
fanly117 小时前
凯亚物联网增加MQTT设备功能测试
微服务·surging microservice
熊大如如7 小时前
Java 反射
java·开发语言
ll7788118 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
我不想当小卡拉米8 小时前
【Linux】操作系统入门:冯诺依曼体系结构
linux·开发语言·网络·c++