filetext_grpc.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "context"
  4. "download_file/fileproto"
  5. server "download_file/server"
  6. "fmt"
  7. "github.com/golang/protobuf/proto"
  8. "google.golang.org/grpc"
  9. cu "jygit.jydev.jianyu360.cn/data_capture/myself_util/commonutil"
  10. "time"
  11. )
  12. var FileTextClient server.CallerClient
  13. // 初始化客户端
  14. func InitFileTextGrpcClient() {
  15. defer cu.Catch()
  16. // 连接服务器
  17. t1 := time.Now()
  18. conn, err := grpc.Dial(OcrServerAddr, grpc.WithInsecure())
  19. if err != nil {
  20. fmt.Printf("连接服务端失败: %s", err)
  21. return
  22. } else {
  23. fmt.Println("连接服务端OK")
  24. }
  25. fmt.Println("连接GRPC耗时:", time.Since(t1))
  26. // 新建一个客户端
  27. FileTextClient = server.NewCallerClient(conn)
  28. }
  29. func GetFileText(fileName, fileUrl, fileType string, fileBytes []byte) *fileproto.FileResponse {
  30. defer cu.Catch()
  31. params := &fileproto.Request{
  32. FileName: fileName,
  33. FileUrl: fileUrl,
  34. FileType: fileType,
  35. FileBytes: fileBytes,
  36. ReturnType: 0, //正式环境改为0
  37. ExtractType: 1,
  38. }
  39. aa := &fileproto.FileRequest{
  40. Message: []*fileproto.Request{
  41. params,
  42. },
  43. }
  44. bs, _ := proto.Marshal(aa)
  45. resp, err := FileTextClient.Call(context.Background(), &server.Request{
  46. Topic: "file2txt",
  47. Timeout: 300,
  48. Data: bs,
  49. })
  50. if err != nil {
  51. fmt.Println("调用服务端代码失败: ", err.Error())
  52. return nil
  53. }
  54. respData := new(fileproto.FileResponse)
  55. err = proto.Unmarshal(resp.GetData(), respData)
  56. if err != nil {
  57. fmt.Println("结果解析失败...")
  58. }
  59. return respData
  60. }