美文网首页
使用gRPC实现golang服务与flutter客户端交互

使用gRPC实现golang服务与flutter客户端交互

作者: 渣渣曦 | 来源:发表于2019-05-10 15:49 被阅读0次

    目录结构如下:

    grpc_golang_flutter (根目录)
           |-- golang_server(go语言服务目录)
           |---- server.go
           |---- client.go
    

    vscode配置文件settings.json内容如下:

    {
        "go.gopath": "H:/grpc_golang_flutter/modules",
        "terminal.integrated.env.windows": {
            "GOPATH": "H:/grpc_golang_flutter/modules",
            "PATH": "${env:PATH};H:/grpc_golang_flutter/modules/bin"
        },
    }
    

    以上环境变量也可使用命令行手工设置。

    go语言服务端

    在grpc_golang_flutter/golang_server目录下初始化go module:

    go mod init grpc_golang_flutter/golang_server
    

    安装 gRPC
    使用如下命令进行gRPC安装:

    go get google.golang.org/grpc
    

    安装 Protocol Buffers v3
    安装该协议编译器用来生成 gRPC 服务代码。最简便的方法是在https://github.com/google/protobuf/releases下载一个二进制压缩包。解压后加入环境变量,如下:

    export PATH=$PATH:执行路径 (linux下)
    set PATH=%PATH%;执行路径   (windows下)
    

    执行以下命令安装该协议插件,执行后会生成protoc-gen-go.exe用来生成go语言proto接口文件:

    go get github.com/golang/protobuf/protoc-gen-go
    

    在golang_server目录下创建目录“helloworld”并新建文件“helloworld.proto”,填写如下内容:

    syntax = "proto3";
    
    package helloworld;
    // 服务端定义
    service Greeter {
    // 服务端返馈信息方法
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    // 包含用户名的请求信息
    message HelloRequest {
    string name = 1;
    }
    // 服务端响应信息
    message HelloReply {
    string message = 1;
    }
    

    然后使用protoc命令,在grpc_golang_flutter根目录执行以下命令生成helloworld.pb.go

    protoc --proto_path=golang_server/helloworld --go_out=plugins=grpc:golang_server/helloworld golang_server/helloworld/helloworld.proto
    

    服务端server.go代码如下:

    package main
    
    import (
        "context"
        "log"
        "fmt"
        "net"
        "google.golang.org/grpc"
        pb "grpc_golang_flutter/golang_server/helloworld"
    )
    
    const (
        port = ":50051"
    )
    
    // server is used to implement helloworld.GreeterServer.
    type server struct{}
    // SayHello implements helloworld.GreeterServer
    func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
        log.Printf("Received: %v", in.Name)
        return &pb.HelloReply{Message: "Hello " + in.Name}, nil
    }
    
    func main() {
        lis, err := net.Listen("tcp", port)
        if err != nil {
            log.Fatalf("failed to listen: %v", err)
        }
        s := grpc.NewServer()
        pb.RegisterGreeterServer(s, &server{})
        fmt.Println("启动服务",port)
        if err := s.Serve(lis); err != nil {
            log.Fatalf("failed to serve: %v", err)
        }
    }
    

    客户端client.go代码如下:

    package main
    
    import (
        "log"
        "os"
        "golang.org/x/net/context"
        "google.golang.org/grpc"
        pb "grpc_golang_flutter/golang_server/helloworld"
    )
    
    const (
        address     = "localhost:50051"
        defaultName = "world"
    )
    
    func main() {
        // Set up a connection to the server.
        conn, err := grpc.Dial(address, grpc.WithInsecure())
        if err != nil {
            log.Fatalf("did not connect: %v", err)
        }
        defer conn.Close()
        c := pb.NewGreeterClient(conn)
        // Contact the server and print out its response.
        name := defaultName
        if len(os.Args) > 1 {
            name = os.Args[1]
        }
        r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
        if err != nil {
            log.Fatalf("could not greet: %v", err)
        }
        log.Printf("Greeting: %s", r.Message)
    }
    

    flutter客户端

    在grpc_golang_flutter目录下执行以下命令:

    flutter create flutter_client
    

    安装Dart的protoc插件:

    pub global activate protoc_plugin
    

    在pubspec.yaml文件中引入protobuf和grpc库(注意位置),如下图:


    image.png

    grpc_golang_flutter/lib下创建helloworld文件夹,
    grpc_golang_flutter目录下执行以下命令生成dart的proto接口文件:

    protoc --proto_path=golang_server/helloworld --dart_out=grpc:flutter_client/lib/helloworld golang_server/helloworld/helloworld.proto
    

    main.dart文件使用以下代码覆盖(初步控制台实现打印输出,待完善):

    import 'package:flutter/material.dart';
    import 'package:flutter_client/helloworld/helloworld.pbgrpc.dart';
    import 'package:grpc/grpc.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    getRemoteData() async {
      final name = "hello";
      final channel = ClientChannel(
        '192.168.3.4',
        port: 50051,
        options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
      );
      final stub = GreeterClient(channel);
      try {
        var response = await stub.sayHello(HelloRequest()..name = name);
        print('服务端返回信息: ${response.message}');
      } catch (e) {
        print('Caught error: $e');
      }
      await channel.shutdown();
    }
    
    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          floatingActionButton: FloatingActionButton(
            onPressed: getRemoteData,
            tooltip: '获取grpc数据',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    运行结果如下:


    image.png

    在系统发布时,android手机需要在android/app/src/main目录下的AndroidManifest.xml文件中<manifest标签下加入以下内容:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    相关文章

      网友评论

          本文标题:使用gRPC实现golang服务与flutter客户端交互

          本文链接:https://www.haomeiwen.com/subject/akpaoqtx.html