2024 go-zero社交项目实战

背景

一位商业大亨,他非常看好国内的社交产品赛道,想要造一款属于的社交产品,于是他找到了负责软件研发的小明。 小明跟张三一拍即合,小明决定跟张三大干一番。

社交产品MVP版本需求

MVP指:Minimum Viable Product,即最小可行产品

张三希望以最快的时间看到一款属于自己的社交产品,于是有了接下来的需求。

1.用户服务--已完成

注册、登录、查看个人信息、更新个人信息、注销功能

2.推文服务

发布推文、查看推文、更新推文、删除推文、我的推文列表

用户服务详细需求

张三跟小明很快就赶出了MVP版的用户服务的详细需求

1.注册功能。
支持账号密码注册功能:
  • 账号6-16位,支持数字大小写不敏感的字母和特殊字符_且必须是英文字符开头
  • 密码必须8-32位,支持数字大小写敏感的字母
2.登录功能。
  • 支持账号密码登录功能
3.更新个人信息功能。
  • 支持更新的信息:头像、昵称、个性签名、性别、地区
4.查看个人信息功能。
  • 查看个人的信息:头像、昵称、个性签名、性别、地区
5.注销功能。
  • 注销功能:申请注销后,7天内没有登录,则把账号注销
  • 查看已注销的用户信息时,昵称显示已注销,头像设置展示为默认的官方头像

推文服务详细需求

张三跟小明很快就赶出了MVP版的推文系统的详细需求:

功能1:推文
  • 发表推文。推文最大长度限制10000字符,标题可选,推文必填
  • 查看推文。展示的信息字段:标题、内容、时间(优先展示编辑时间,其次发表时间)
  • 更新推文。支持更新的字段:标题、推文。标题可选,推文必填
  • 删除推文。真实删除,从数据库中移除数据
  • 我的推文列表。查看已经发表过的推文,按发表时间排序排列
功能2:互动
  • 浏览推文。浏览推文按计算方式:每篇推文,点击进入详情累计加1,同一用户24小时内浏览多次,只累计加1
  • 点赞推文。点赞数计算方式:用户点赞+1,取消点赞-1
  • 评论推文
    • 无限层级评论
    • 评论数计算方式:所有能展示的评论数量。比如有1条评论,这条评论有3条子评论,评论的总数量是4.如果删除这条评论,那么评论的总数量是0
  • 分享推文。分享数计算方式:每篇推文,分享累计加1,同一用户24小时内分享多次,只累计加1
  • 收藏推文。点赞数计算方式:用户点赞+1,取消点赞-1

开发环境的搭建

0.安装go语言,推荐使用1.22以上的版本

1.安装goctl,用于提升效率,生成各种代码

go install github.com/zeromicro/go-zero/tools/goctl@latest

2.安装protoc,微服务grpc需要用到的组件

goctl env check --install --verbose --force

3.mysql

docker run -p 3306:3306 --name test-mysql -v mysql:/var/lib/mysql/ -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest

4.redis

docker run -d --name redis -p 6379:6379 redis

5.etcd


# etcd服务
docker network create demo-network --driver bridge
docker run -d --name etcd-server --network demo-network --publish  2379:2379 --publish  2380:2380 --env ALLOW_NONE_AUTHENTICATION=yes --env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 bitnami/etcd:latest
# 检查etcd容器ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' etcd-server
# (可选)etcd keeper http://127.0.0.1:8080/
docker run -d -p 8080:8080 -e ETCD_SERVERS=http://172.18.0.2:2379 --network=demo-network --name etcd-keeper evildecay/etcdkeeper

实现用户服务

1.注册功能
支持账号密码注册:
  • 账号6-16位,支持数字大小写不敏感的字母和特殊字符_且必须是英文字符开头
  • 密码必须8-32位,支持数字大小写敏感的字母
  • 支持账号密码登录功能
2.登录功能
  • 如果该用户申请注销账号了,登录后自动取消注销
3.个人信息。包含查看、更新
  • 查看/更新的个人信息:头像、昵称、个性签名(255字符以内)、性别、地区

4.注销账号功能

  • 注销功能:申请注销后,7天内没有登录,则把账号注销
  • 查看已注销的用户信息时,昵称显示已注销,头像设置展示为默认的官方头像


初始化user服务

# 初始化user rpc服务
goctl rpc new user
# 安装依赖
go mod tidy
create table user
(
    id         bigint auto_increment comment '主键id'
        primary key,
    avatar     varchar(255) default '' not null comment '头像链接地址',
    nickname   char(32)     default '' not null comment '昵称',
    account    char(32)     default '' not null comment '账号',
    password   char(32)     default '' not null comment '密码',
    bio        varchar(255) default '' not null comment '个人简介 Biography',
    gender     tinyint      default 2  not null comment '性别 0 女 1 男 2 未知',
    region     varchar(20)  default '' not null comment '地区',
    status     tinyint      default 0  not null comment '用户状态0 正常 1 注销中 2 已注销',
    created_at int          default 0  not null comment '创建时间',
    updated_at int          default 0  not null comment '更新时间',
    deleted_at int          default 0  not null comment '删除时间',
    constraint user_pk
        unique (account)
)
    comment '用户表' collate = utf8mb4_bin;
# 项目根目录下执行
# 加上 -c 参数可生成集成缓存的model代码
# --ignore-columns -i 忽略字段控制
goctl model mysql datasource -url="root:123456@tcp(127.0.0.1:3306)/go_zero_demo" -table="user"  -dir="./user/model" --ignore-columns -i

user服务功能实现

定义proto
syntax = "proto3";

package user;
option go_package="./user";


message UserInfo {
  int64 UserId  = 1;        // 主键id
  string Avatar = 2;     // 头像链接地址
  string Nickname  = 3;  // 昵称
  string Account    = 4; // 账号
  string Password  = 5; // 密码
  string Bio       = 6; // 个人简介 Biography
  int64 Gender     = 7; // 性别 0 女 1 男 2 未知
  string Region     = 8; // 地区
  int64 Status     = 9; // 地区
  int64 CreatedAt = 10; // 创建时间
  int64 UpdatedAt = 11; //更新时间
}

message RegisterReq {
  string Account = 1; // 自定义账号
  string Password = 2; // 密码
}
message RegisterResp {
  int64 UserId = 1; // 用户ID
}

message LoginReq {
  string Account = 1; // 自定义账号
  string Password = 2; // 密码
}
message LoginResp {
  string SessionId = 1; // 用户登录标识
}

message CancellationReq {
  int64 UserId = 1; // 用户ID
}
message CancellationResp {}

message GetUsersReq {
  int64 UserId = 1; // 用户ID
}
message GetUsersResp {
  UserInfo UserInfo = 1;
}

message UpdateUserReq {
  UserInfo UserInfo = 1;
}
message UpdateUserResp {}

service User {
  // 注册
  rpc Register(RegisterReq) returns(RegisterResp);
  // 登录
  rpc Login(LoginReq) returns(LoginResp);
  // 注销
  rpc Cancellation(CancellationReq) returns(CancellationResp);
  // 查用户信息
  rpc GetUsers(GetUsersReq) returns(GetUsersResp);
  // 更新用户信息
  rpc UpdateUser(UpdateUserReq) returns(UpdateUserResp);
}
# 项目根目录下执行
goctl rpc protoc user/user.proto --go_out=./user --go-grpc_out=./user --zrpc_out=./user
1.注册功能
功能逻辑代码::./user/internal/logic/registerlogic.go
测试代码::./user/internal/logic/registerlogic_test.go
2.登录功能
功能逻辑代码::./user/internal/logic/loginlogic.go
测试代码::./user/internal/logic/loginlogic.go
3.注销功能
功能逻辑代码::./user/internal/logic/cancellationlogic.go
测试代码::./user/internal/logic/cancellationlogic_test.go
4.个人信息
查看个人信息
功能逻辑代码::./user/internal/logic/getuserslogic.go
测试代码::./user/internal/logic/getuserslogic_test.go
更新个人信息
功能逻辑代码::./user/internal/logic/updateuserlogic.go
测试代码::./user/internal/logic/updateuserlogic_test.go

实现推文服务

初始化推文post服务

goctl rpc new user
go mod tidy
create table post
(
  id         bigint auto_increment comment '主键id',
  user_id    bigint   				 			 not null comment '用户id',
  title      varchar(255) default '' not null comment '标题',
  content    text         					 not null comment '推文内容',
  status     tinyint      default 0  not null comment '状态 0 正常 1 已删除',
  views      int          default 0  not null comment '浏览数',
  likes      int          default 0  not null comment '点赞数',
  comments   int          default 0  not null comment '评论数',
  shares     int          default 0  not null comment '分享数',
  collects   int          default 0  not null comment '收藏数',
  created_at int          default 0  not null comment '发表时间',
  updated_at int          default 0  not null comment '更新时间',
  deleted_at int          default 0  not null comment '删除时间',
  constraint post_pk
  primary key (id)
)
    comment '推文表' collate = utf8mb4_bin;
# 项目根目录下执行
# 加上 -c 参数可生成集成缓存的model代码
# --ignore-columns -i 忽略字段控制
goctl model mysql datasource -url="root:123456@tcp(127.0.0.1:3306)/go_zero_demo" -table="post"  -dir="./post/model" --ignore-columns -i

post服务功能实现

定义proto
syntax = "proto3";

package post;
option go_package="./post";

// 定义实体结构
message PostData {
  int64 Id = 1; // id
  string Title = 2; // 标题
  string Content = 3; // 内容
  int64 Views = 4; // 查看数
  int64 Likes = 5; // 喜欢数
  int64 Comments = 6; // 评论数
  int64 Shares = 7; // 分享数
  int64 Collects = 8; // 收藏数

}

message CreatePostReq {
  PostData PostData = 1;
}

message CreatePostResp {}

message UpdatePostReq {
  PostData PostData = 1;
}

message UpdatePostResp {}

message DeletePostReq {
  int64 PostId = 1;
}

message DeletePostResp {}

message GetPostReq {
  int64 PostId = 1;
}

message GetPostResp {}

message BatchPostReq {
  repeated int64 PostId = 1;
}

message BatchPostResp {
  repeated PostData Infos = 1;
}

message GetUserPostListReq {
  int64 UserId = 1; // 用户ID
}

message GetUserPostListResp {
  repeated PostData Infos = 1;
}

service Post {
  // 发表推文
  rpc CreatePost(CreatePostReq) returns(CreatePostResp);
  // 更新推文
  rpc UpdatePost(UpdatePostReq) returns(UpdatePostResp);
  // 删除推文
  rpc DeletePost(DeletePostReq) returns(DeletePostReq);
  // 获取单条推文
  rpc GetPost(GetPostReq) returns(GetPostResp);
  // 批量获取推文
  rpc BatchPost(BatchPostReq) returns(BatchPostResp);
  // 用户用户推文列表
  rpc GetUserPostList(GetUserPostListReq) returns(GetUserPostListResp);
}
# 项目根目录下执行
goctl rpc protoc post/post.proto --go_out=./post --go-grpc_out=./post --zrpc_out=./post
1.发表推文功能
功能逻辑代码::./post/internal/logic/createpostlogic.go
测试代码::./post/internal/logic/createpostlogic_test.go
2.查看推文功能
功能逻辑代码::./post/internal/logic/getpostlogic.go
测试代码::./post/internal/logic/getpostlogic_test.go
3.更新推文功能
功能逻辑代码::./post/internal/logic/updatepostlogic.go
测试代码::./post/internal/logic/updatepostlogic_test.go
4.删除推文功能
功能逻辑代码::./post/internal/logic/deletepostlogic.go
测试代码::./post/internal/logic/deletepostlogic_test.go
5.查看用户推文列表功能
功能逻辑代码::./post/internal/logic/getuserpostlistlogic.go
测试代码::./post/internal/logic/getuserpostlistlogic_test.go

实现API服务

初始化bff服务

goctl api new bff

# 生成api代码
goctl api go -api ./bff/bff.api -dir ./bff/

其它

1.跨域配置

func StartHttpServer(configFile *string) {
    var c config.Config
    conf.MustLoad(*configFile, &c)

    server := rest.MustNewServer(c.RestConf, rest.WithCustomCors( //跨域处理
        func(header http.Header) {
            header.Set("Access-Control-Allow-Origin", "*")
            header.Set("Access-Control-Allow-Headers", "*")
            header.Set("Access-Control-Allow-Methods", "POST,OPTIONS")
            header.Set("Access-Control-Allow-Credentials", "true")
        }, nil, "*"))
    defer server.Stop()
    ……
    server.Start()
}

2.服务依赖配置

UserRpcConf:
  Etcd:
    Key: dev.user.rpc
# 默认是两秒
  Timeout: 4500 
# 当值为 true 时,不会阻塞 rpc 链接
  NonBlock: false

弱依赖可配置为 true,否则初始化rpc的时候会报以下错误

rpc dial: etcd://127.0.0.1:2379/dev.user.rpc, error: context deadline exceeded, make sure rpc service "dev.user.rpc" is already started


http://www.niftyadmin.cn/n/5666673.html

相关文章

网络编程:掌握TCP Socket和UDP Socket

IP地址: 两台计算机通信,双方都必须有IP地址。 IPV4地址有32位,由四个8位二进制组成,因为不好记所以我们把二进制转化为十进制,比如192.168.0.20,这称为点分十进制。 IPV6有128位,由8个16位的…

Java项目实战II基于Java+Spring Boot+MySQL的房屋租赁管理系统的设计与实现

目录 一、前言 二、技术介绍 三、系统实现 四、论文参考 五、核心代码 六、源码获取 全栈码农以及毕业设计实战开发,CSDN平台Java领域新星创作者,专注于大学生项目实战开发、讲解和毕业答疑辅导。获取源码联系方式请查看文末 一、前言 随着城市租…

1网络安全的基本概念

文章目录 网络安全的基本概念可以总结为以下几个方面: 网络安全的需求: 信息安全的重要性:信息安全是计算机、通信、物理、数学等领域的交叉学科,对于社会的发展至关重要。信息安全的目标:主要包括保密性、完整性、可用…

数据库系统原理(第一章 数据库概述)

文章目录 1.数据1.1数据的概念1.2数据与信息的关系1.3数据使用 2.数据管理3.数据库与数据库管理系统3.1数据库3.2数据库管理系统 4.数据库系统4.1数据库系统组成4.2 数据库系统的特点4.3数据库系统体系结构4.3.1内部体系结构4.3.2外部体系结构 本文首先从数据讲起,然…

深度解析ElasticSearch:构建高效搜索与分析的基石原创

引言 在数据爆炸的时代,如何快速、准确地从海量数据中检索出有价值的信息成为了企业面临的重要挑战。ElasticSearch,作为一款基于Lucene的开源分布式搜索和分析引擎,凭借其强大的实时搜索、分析和扩展能力,成为了众多企业的首选。…

tomcat,el表达式执行带参数命令,字符串数组,String[],el表达式注入

准备环境: docker pull tomcat:8;docker run --name tomcat8 -p 808:8080 -v /tmp/CC:/usr/local/tomcat/webapps/ -d tomcat:8;如下为 /tmp/CC/app/index.jsp <% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-8…

Java 数据类型转换详解:隐式转换(自动转换)与强制转换(手动转换)

目录 前言 取值范围从小到大的关系&#xff1a; 隐式转换&#xff08;自动转换&#xff09; &#x1f4dc;示例 1&#xff1a;基本类型隐式转换 &#x1f4dc;示例 2&#xff1a;算术运算中的类型提升 &#x1f4dc;示例 3&#xff1a;byte、short 和 char 的自动转换 隐…

Greiner 经典力学(多体系统和哈密顿力学)第二章 学习笔记

第二章 学习笔记 第二章的题目是 Free Fall on the Rotating Earth。这章的内容就是第一章结论的一个直接应用。这一章假设地心是做匀速直线运动的&#xff0c;也就是地心坐标系是惯性系 L。在地表处建立一个 M 坐标系。 首先先指出书上一个错误&#xff0c;书上公式 2.1 写的…