jiangshuai
2023-11-23 44bba3c3479d8b9cd04ef4087762f9f960c5bb55
获取客户列表grpc客户端
2个文件已添加
4个文件已修改
72 ■■■■■ 已修改文件
conf/config.yaml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/operation.go 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/client.proto 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
proto/client/client.go 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/config.yaml
@@ -25,5 +25,5 @@
  storePath: uploads/file
grpcServer:
  apsAddr: 192.168.20.119:9091
  crmAddr: 192.168.20.118:9092
  crmAddr: :9092
  srmAddr: 192.168.20.118:9093
controllers/operation.go
@@ -24,6 +24,7 @@
    "wms/opa"
    "wms/pkg/logx"
    "wms/pkg/structx"
    "wms/proto/client"
    "wms/proto/product_inventory"
    "wms/proto/purchase_wms"
    "wms/proto/supplier"
@@ -1084,3 +1085,21 @@
    }
    util.ResponseFormat(c, code.Success, resp.List)
}
// GetClientList
// @Tags      入库/出库
// @Summary   获取物流公司列表
// @Produce   application/json
//
//    @Success    200    {object}    util.Response    "成功"
//
// @Router    /api-wms/v1/operation/getClientList [get]
func (slf OperationController) GetClientList(c *gin.Context) {
    cli := client.NewClientServiceClient(client.ClientConn)
    resp, err := cli.GetClientList(c, &client.ClientListRequest{})
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "grpc调用失败:"+err.Error())
        return
    }
    util.ResponseFormat(c, code.Success, resp.List)
}
main.go
@@ -14,6 +14,7 @@
    "wms/controllers"
    "wms/models"
    "wms/pkg/logx"
    "wms/proto/client"
    "wms/proto/product_inventory"
    "wms/proto/purchase_wms"
    "wms/proto/supplier"
@@ -51,6 +52,7 @@
    go controllers.InitProductInventoryServiceConn()
    go controllers.InitCodeServiceConn()
    go supplier.InitConn()
    go client.InitConn()
    //启动grpc服务
    go func() {
        ln, err := net.Listen("tcp", ":"+conf.WebConf.GrpcPort)
@@ -90,6 +92,7 @@
    controllers.CloseProductInventoryServiceConn()
    controllers.CloseCodeServiceConn()
    supplier.CloseConn()
    client.CloseConn()
    // 关闭HTTP服务器
    if err := server.Shutdown(ctx); err != nil {
        logx.Infof("服务优雅退出失败: %v", err)
proto/client.proto
New file
@@ -0,0 +1,20 @@
syntax = "proto3";
option go_package = "./client";
service ClientService {
  rpc GetClientList(ClientListRequest) returns (ClientListResponse);
}
message ClientListRequest {
}
message ClientInfo {
  int32 ClientId =1;
  string ClientName =2;
}
message ClientListResponse {
  repeated ClientInfo List =1;
}
proto/client/client.go
New file
@@ -0,0 +1,27 @@
package client
import (
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    "wms/conf"
    "wms/pkg/logx"
)
var (
    ClientConn *grpc.ClientConn
)
func InitConn() {
    var err error
    ClientConn, err = grpc.Dial(conf.GrpcServerConf.CrmAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        logx.Errorf("grpc dial product service error: %v", err.Error())
        return
    }
}
func CloseConn() {
    if ClientConn != nil {
        ClientConn.Close()
    }
}
router/router.go
@@ -90,6 +90,7 @@
        operationAPI.PUT("cancel/:id", operationController.Cancel)
        operationAPI.PUT("outputOperation/:id", operationController.OutputOperation)
        operationAPI.GET("getSupplierList", operationController.GetSupplierList)
        operationAPI.GET("getClientList", operationController.GetClientList)
    }