简介
本专题将带你使用 axum 和 gRPC 构建一个分布式的博客系统数据结构与Protobuf
本章对我们项目的数据结构和proto进行定义实现分类服务
本章我们实现分类服务,即 `category-srv`实现文章服务
本章将带你实现文章的 gPRC 服务。实现前台web服务
本章将通过使用 axum 调用分类和文章的 gRPC 服务,来实现博客前台Web服务实现管理员服务
本章我们将实现管理员服务实现后台管理web服务
本章将使用 axum 调用 gRPC 服务来实现后台管理的 web 服务安全与鉴权
本章将讨论使用jwt进行鉴权服务扩容、注册、发现和编排
本章将讨论服务管理相关的话题配置中心服务
本章讨论配置中心的实现总结
本专题试图通过一个分布式博客的案例来探讨使用 rust 实现 gRPC 微服务架构的可行性
数据结构与Protobuf
数据结构
文章服务的定义
syntax = "proto3";
package pb;
import "google/protobuf/timestamp.proto";
message Topic {
int64 id = 1;
string title = 2;
int32 category_id = 3;
string summary = 4;
string content = 5;
int32 hit = 6;
bool is_del = 7;
google.protobuf.Timestamp dateline = 8;
}
message DatelineRange {
google.protobuf.Timestamp start = 1;
google.protobuf.Timestamp end = 2;
}
// -- 创建文章
message CreateTopicRequest {
string title = 1;
int32 category_id = 2;
string content = 3;
optional string summary = 4; // 如果没有提供摘要,则自动从内容中截取
}
message CreateTopicReply { int64 id = 1; }
// -- 修改文章
message EditTopicRequest {
int64 id = 1;
string title = 2;
int32 category_id = 3;
optional string summary = 4; // 如果没有提供摘要,则自动从内容中截取
string content = 5;
}
message EditTopicReply {
int64 id = 1;
bool ok = 2;
}
// -- 文章列表
message ListTopicRequest {
optional int32 page = 1; // 页码
optional int32 category_id = 2; // 分类
optional string keyword = 3; // 关键字
optional bool is_del = 4; // 是否删除
optional DatelineRange dateline_range = 5; // 时间区间
}
message ListTopicReply {
int32 page = 1; // 当前页码
int32 page_size = 2; // 每页条数
int64 page_totoal = 3; // 总页数
int64 record_total = 4; // 总记录数
repeated Topic topics = 5; // 文章列表
}
// -- 删除/恢复文章
message ToggleTopicRequest { int64 id = 1; }
message ToggleTopicReply {
int64 id = 1;
bool is_del = 2;
}
// -- 获取文章详情
message GetTopicRequest {
int64 id = 1;
optional bool is_del = 2;
optional bool inc_hit = 3; // 是否同时增加点击量
}
message GetTopicReply { optional Topic topic = 1; }
service TopicService {
// 创建文章
rpc CreateTopic(CreateTopicRequest) returns (CreateTopicReply);
// 修改文章
rpc EditTopic(EditTopicRequest) returns (EditTopicReply);
// 文章列表
rpc ListTopic(ListTopicRequest) returns (ListTopicReply);
// 删除/恢复文章
rpc ToggleTopic(ToggleTopicRequest) returns (ToggleTopicReply);
// 获取文章详情
rpc GetTopic(GetTopicRequest) returns (GetTopicReply);
}
管理员服务的定义
syntax = "proto3";
package pb;
message Admin {
int32 id = 1;
string email = 2;
optional string password = 3;
bool is_del = 4;
}
// -- 添加管理员
message CreateAdminRequest {
string email = 1;
string password = 2;
}
message CreateAdminReply { int32 id = 1; }
// -- 管理员列表
message ListAdminRequest {
optional string email = 1; // 根据EMAIL进行查找
optional bool is_del = 2; // 是否删除
}
message ListAdminReply { repeated Admin admins = 1; }
// -- 修改管理员
message EditAdminRequest {
int32 id = 1;
string email = 2;
string password = 3; // 现用密码
optional string new_password = 4; // 如果有密码,则修改密码
}
message EditAdminReply {
int32 id = 1;
bool ok = 2;
}
// -- 删除/恢复管理员
message ToggleAdminRequest { int32 id = 1; }
message ToggleAdminReply {
int32 id = 1;
bool is_del = 2;
}
// -- 管理员是否存在
message AdminExistsRequest {
oneof condition {
string email = 1;
int32 id = 2;
}
}
message AdminExistsReply { bool exists = 1; }
// -- 获取管理员
message GetAdminRequest {
message ByAuth {
string email = 1;
string password = 2;
}
message ById {
int32 id = 1;
optional bool is_del = 2;
}
oneof condition {
ById by_id = 1; // 通过ID直接获取
ByAuth by_auth = 2; // 通过登录信息获取
}
}
message GetAdminReply { optional Admin admin = 1; }
service AdminService {
// 添加管理员
rpc CreateAdmin(CreateAdminRequest) returns (CreateAdminReply);
// 管理员列表
rpc ListAdmin(ListAdminRequest) returns (ListAdminReply);
// 修改管理员
rpc EditAdmin(EditAdminRequest) returns (EditAdminReply);
// 删除/恢复管理员
rpc ToggleAdmin(ToggleAdminRequest) returns (ToggleAdminReply);
// 管理员是否存在
rpc AdminExists(AdminExistsRequest) returns (AdminExistsReply);
// 获取管理员
rpc GetAdmin(GetAdminRequest) returns (GetAdminReply);
}
本章代码位于01/数据结构与protobuf分支