go-kratos使用备忘

我搭建的一个kratos项目模板,欢迎使用,仓库地址 需要特别注意的一些建议 API路由覆盖的问题 比如有两个接口 A get /v1/user/{user_id}和 B get /v1/user/profile如果A定义在B之前,那么B可能会被A覆盖路由。需要将A放到B之前。 JWT使用的建议 摘自极客时间课程《高并发系统实战课》 通讯过程必须使用 HTTPS 协议,这样才可以降低被拦截的可能。 要注意限制 token 的更换次数,并定期刷新 token,比如用户的 access_token 每天只能更换 50 次,超过了就要求用户重新登陆,同时 token 每隔 15 分钟更换一次。这样可以降低 token 被盗取后给用户带来的影响。 Web 用户的 token 保存在 cookie 中时,建议加上 httponly、SameSite=Strict 限制,以防止 cookie 被一些特殊脚本偷走。 配置文件 配置文件校验 配合buf的validate可以方便地进行配置文件的校验,在程序启动之前就对配置文件进行一次校验。下面是一个简单的proto配置定义 syntax = "proto3"; package conf; import "buf/validate/validate.proto"; import "google/protobuf/duration.proto"; option go_package = "github.com/tpl-x/kratos/internal/conf;conf"; message Bootstrap { Server server = 1; Data data = 2; Log log = 3; } message Server { message HTTP { string network = 1; string addr = 2; google.protobuf.Duration timeout = 3 [ // the field is required (buf.validate.field).required = true, // duration must be longer than 1 second but no longer than 10 min. (buf.validate.field).duration = { // Validates that duration is greater than 1 second gt: {seconds: 1} // Validates that duration is less than or equal to 10 min. lte: {seconds: 600} } ]; } message GRPC { string network = 1; string addr = 2; google.protobuf.Duration timeout = 3 [ // the field is required (buf.validate.field).required = true, // duration must be longer than 1 second but no longer than 10 min. (buf.validate.field).duration = { // Validates that duration is greater than 1 second gt: {seconds: 1} // Validates that duration is less than or equal to 10 min. lte: {seconds: 600} } ]; } HTTP http = 1; GRPC grpc = 2; } message Data { message Database { string driver = 1; string source = 2; } message Redis { string network = 1; string addr = 2; google.protobuf.Duration read_timeout = 3; google.protobuf.Duration write_timeout = 4; } Database database = 1; Redis redis = 2; } enum LogLevel { Debug = 0; Info = 1; Warn = 2; Error = 3; Fatal = 4; } message Log { string log_path = 1; LogLevel log_level = 2 [(buf.validate.field).enum = { defined_only: true in: [ 0, 1, 2, 3, 4 ] }]; int32 max_size = 3; int32 max_keep_days = 4; int32 max_keep_files = 5; bool compress = 6; } 生成proto对应的go文件中以后,就可以通过下面的方式进行校验: ...

2022-08-12 · 36 分钟 · czyt

为Kratos prtobuf文件添加多种编译输出

Csharp 安装Grpc.tools https://www.nuget.org/packages/Grpc.Tools/ 下载解压 nupkg文件(改扩展名为zip),也可以使用附件的7z包 解压 找到tools中对应系统架构的软件,设置下环境变量,让系统可以找到就行。 Linux 需要创建一个符号链接 ln -s `which grpc_csharp_plugin` /usr/bin/protoc-gen-grpc-csharp 修改Kratos项目的Make文件 在api这个make任务中添加下面内容 --csharp_out=./api/pipe/v1 \ --grpc-csharp_out=./api/pipe/v1 \ 完整内容为 .PHONY: api # generate api proto api: protoc --proto_path=./api \ --proto_path=./third_party \ --go_out=paths=source_relative:./api \ --go-http_out=paths=source_relative:./api \ --go-grpc_out=paths=source_relative:./api \ --csharp_out=./api/pipe/v1 \ --grpc-csharp_out=./api/pipe/v1 \ --openapi_out==paths=source_relative:. \ 参考 https://github.com/grpc/grpc/blob/master/src/csharp/BUILD-INTEGRATION.md 📎tools.7z Python 安装必要包 pip install grpclib protobuf 查询路径 which protoc-gen-grpclib_python 或者 which protoc-gen-python_grpc我这里返回信息如下: ➜ czyt which protoc-gen-grpclib_python /usr/sbin/protoc-gen-grpclib_python 如法炮制,创建软链接 ln -s /usr/sbin/protoc-gen-grpclib_python /usr/sbin/protoc-gen-grpc_python 修改Makefile 添加下面的内容,再执行make api生成api即可。 --python_out=./api \ --grpc_python_out=./api \ Dart 安装插件 dart pub global activate protoc_plugin 将插件工具加入环境变量 export PATH="$PATH:$HOME/.pub-cache/bin" 脚本添加--dart_out=选项 --dart_out=./api \ TypeScript 纯typescript类 ts-proto typescript类+服务 安装proto工具 go get go.einride.tri p tech/protoc-gen-typescript-http ...

2022-04-08 · 1 分钟 · czyt