[**micro-cluster 中文示例**](https://www.bilibili.com/read/cv23255594)
<br>
By taking a simple e-commerce microservice cluster as an example, the product details page contains product information, inventory information, and product evaluation information. These data are scattered in different microservices. The RPC gateway service assembles the required data and returns it to the product details page, as shown in the following figure:
![micro-cluster](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_micro-cluster.png)
Four proto files have been prepared in advance. Each proto file generates corresponding service code:
- The comment.proto file defines RPC methods to obtain comment data based on the product ID and is used to generate the comment RPC service code.
- The inventory.proto file defines RPC methods to obtain inventory data based on the product ID and is used to generate the inventory RPC service code.
- The product.proto file defines RPC methods to obtain product details based on the product ID and is used to generate the product RPC service code.
- The shop_gw.proto file defines RPC methods to assemble the data required by the product details page based on the product ID and is used to generate the shop RPC gateway service code.
<br>
### Quickly generate and start comment, inventory, and product microservices
#### Generate comment, inventory, and product microservice code
Enter the Sponge UI interface, click the left menu bar 【Protobuf】--> 【RPC type】-->【Create RPC project】, fill in the respective parameters of the comment, inventory, and product, and generate the comment, inventory, and product service code respectively.
The microservice framework uses [grpc](https://github.com/grpc/grpc-go) and also contains common service governance function codes, build deployment scripts, etc. You can choose whatever database you want.
Quickly create a comment service as shown below:
![micro-rpc-pb-comment](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_micro-rpc-pb-comment.png)
Quickly create an inventory service as shown below:
![micro-rpc-pb-inventory](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_micro-rpc-pb-inventory.png)
Quickly create a product service as shown below:
![micro-rpc-pb-product](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_micro-rpc-pb-product.png)
Open three terminals, one for each of the comment, inventory, and product services.
<br>
#### Start the comment service
Switch to the comment directory and perform the following steps:
(1) Generate pb.go code, generate template code, and generate test code
```shell
make proto
```
(2) Open internal/service/comment.go, which is the generated template code. There is a line of panic code prompting you to fill in the business logic code. Fill in the business logic here, for example, fill in the return value:
```go
return &commentV1.ListByProductIDReply{
Total: 11,
ProductID: 1,
CommentDetails: []*commentV1.CommentDetail{
{
Id: 1,
Username: "Mr Zhang",
Content: "good",
},
{
Id: 2,
Username: "Mr Li",
Content: "good",
},
{
Id: 3,
Username: "Mr Wang",
Content: "not good",
},
},
}, nil
```
(3) Open the configs/comment.yml configuration file, find grpc, and modify the port and httpPort values under it:
```yaml
grpc:
port: 18203 # listen port
httpPort: 18213 # profile and metrics ports
```
(4) Compile and start the comment service
```shell
make run
```
<br>
#### Start the inventory service
Switch to the inventory directory and perform the same steps as for the comment:
(1) Generate pb.go code, generate template code, and generate test code
```shell
make proto
```
(2) Open internal/service/inventory.go, which is the generated template code. There is a line of panic code prompting you to fill in the business logic code. Fill in the business logic here, for example, fill in the return value:
```go
return &inventoryV1.GetByIDReply{
InventoryDetail: &inventoryV1.InventoryDetail{
Id: 1,
Num: 999,
SoldNum: 111,
},
}, nil
```
(3) Open the configs/inventory.yml configuration file, find grpc, and modify the port and httpPort values under it:
```yaml
grpc:
port: 28203 # listen port
httpPort: 28213 # profile and metrics ports
```
(4) Compile and start the inventory service
```shell
make run
```
<br>
#### Start the product service
Switch to the product directory and perform the same steps as for the comment:
(1) Generate pb.go code, generate template code, and generate test code
```shell
make proto
```
(2) Open internal/service/product.go, which is the generated template code. There is a line of panic code prompting you to fill in the business logic code. Fill in the business logic here, for example, fill in the return value:
```go
return &productV1.GetByIDReply{
ProductDetail: &productV1.ProductDetail{
Id: 1,
Name: "Data cable",
Price: 10,
Description: "Android type c data cable",
},
InventoryID: 1,
}, nil
```
(3) Open the configs/product.yml configuration file, find grpc, and modify the port and httpPort values under it:
```yaml
grpc:
port: 38203 # listen port
httpPort: 38213 # profile and metrics ports
```
(4) Compile and start the product service
```shell
make run
```
After the comment, inventory, and product microservices have started successfully, you can now generate and start the gateway service.
<br>
### Quickly generate and start the RPC gateway service
Enter the Sponge UI interface, click the left menu bar 【Protobuf】--> 【Web type】-->【Create RPC gateway project】, fill in some parameters to generate the RPC gateway project code.
The web framework uses [gin](https://github.com/gin-gonic/gin), which also contains swagger documentation, common service governance function codes, build deployment scripts, etc.
![micro-rpc-gw-pb-shopgw](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_micro-rpc-gw-pb-shopgw.png)
In order to connect to the comment, inventory, and product RPC services, you need to generate additional connection RPC service codes. Click the left menu bar 【Public】--> 【Generate connection RPC service code】, fill in the parameters to generate the code, and then move the generated connection RPC service code to the RPC gateway project code directory.
![micro-rpc-cli](https://raw.githubusercontent.com/zhufuyi/sponge_examples/main/assets/en_micro-cluster-rpc-cli.png)
In order to call the methods of the RPC services in the RPC gateway service, you need to copy the proto files of the comment, inventory, and product RPC services to the api/shop_gw/v1 directory of the RPC gateway service.
Switch to the shop_gw directory and perform the following steps:
(1) Generate pb.go code, generate registered routing code, generate template code, and generate swagger documentation
```shell
make proto
```
(2) Open internal/service/shop_gw_logic.go, which is the generated API interface code. Fill in the business logic code here, fill in the following simple business logic code:
```go
package service
import (
"context"
commentV1 "shop_gw/api/comment/v1"
inventoryV1 "shop_gw/api/inventory/v1"
productV1 "shop_gw/api/product/v1"
shop_gwV1 "shop_gw/api/shop_gw/v1"
"shop_gw/internal/ecode"
"shop_gw/internal/rpcclient"
"github.com/zhufuyi/sponge/pkg/grpc/interceptor"
"github.com/zhufuyi/sponge/pkg/logger"
)
var _ shop_gwV1.ShopGwLogicer = (*shopGwClient)(nil)
type shopGwClient struct {
productCli productV1.ProductClient
inventoryCli inventoryV1.InventoryClient
commentCli commentV1.CommentClient
}
// NewShopGwClient create a client
func NewShopGwClient() shop_gwV1.ShopGwLogicer {
return &shopGwClient{
productCli: productV1.NewProdu
没有合适的资源?快使用搜索试试~ 我知道了~
后端、go语言、使用Go语言和Gin框架快速上手项目
共2000个文件
go:636个
proto:634个
sh:238个
需积分: 0 0 下载量 27 浏览量
2024-02-08
09:48:47
上传
评论
收藏 8.56MB ZIP 举报
温馨提示
后端、go语言、使用Go语言和Gin框架快速上手项目
资源推荐
资源详情
资源评论
收起资源包目录
后端、go语言、使用Go语言和Gin框架快速上手项目 (2000个子文件)
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 14KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 11KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 8KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 2KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 1KB
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 32B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 31B
BUILD.bazel 0B
BUILD.bazel 0B
BUILD.bazel 0B
BUILD.bazel 0B
BUILD.bazel 0B
BUILD.bazel 0B
BUILD.bazel 0B
BUILD.bazel 0B
BUILD.bazel 0B
Dockerfile 729B
Dockerfile 705B
Dockerfile 705B
Dockerfile 697B
Dockerfile 689B
Dockerfile 689B
Dockerfile 689B
Dockerfile 672B
Dockerfile 665B
Dockerfile 665B
Dockerfile_build 2KB
Dockerfile_build 2KB
Dockerfile_build 2KB
Dockerfile_build 2KB
Dockerfile_build 1KB
Dockerfile_build 1KB
Dockerfile_build 1KB
Dockerfile_build 1KB
Dockerfile_build 1KB
Dockerfile_build 1KB
Dockerfile_test 517B
Dockerfile_test 502B
Dockerfile_test 502B
Dockerfile_test 497B
Dockerfile_test 492B
Dockerfile_test 492B
Dockerfile_test 492B
Dockerfile_test 477B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
飞影铠甲
- 粉丝: 3375
- 资源: 207
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 课设毕设基于SSM的个人交友网站 LW+PPT+源码可运行.zip
- 课设毕设基于SSM的高校信息资源共享平台 LW+PPT+源码可运行.zip
- 课设毕设基于SSM的高校二手交易平台 LW+PPT+源码可运行.zip
- Z`@9ZFCVIJ41EJ2%J$RWI3S.png
- 课设毕设基于SSM的艺诚美业管理系统 LW+PPT+源码可运行.zip
- 基于STM8j003单片机设计MICRO-USB接口 圆环形PCB小板AD设计硬件(原理图+PCB+封装库)文件.zip
- Excel模板个人简历稳重大气单页22.docx
- Excel模板个人简历稳重大气单页24.docx
- Excel模板个人简历稳重大气单页25.docx
- Excel模板个人简历稳重大气单页07.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功