rony2818 / gRPC-tutorial

This is an implementation of gRPC udemy tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gRPC [Golang] tutorial

This is an implementation of Udemy tutorial

  1. install GO:

     $ wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz
     $ sudo tar -C /usr/local -xzf go1.12.9.linux-amd64.tar.gz
     $ export PATH=$PATH:/usr/local/go/bin
    
  2. Install grpc-go:

     $ go get -u google.golang.org/grpc
    
  3. Install Protocol Buffers for GO:

     $ go get -u github.com/golang/protobuf/protoc-gen-go
    
  4. Install Protocol Buffers (protoc)

     $ curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protoc-3.9.1-linux-x86_64.zip
     $ unzip protoc-3.9.1-linux-x86_64.zip -d protoc3
     $ sudo mv protoc3/bin/* /usr/local/bin/ && sudo mv protoc3/include/* /usr/local/include/
     $ rm protoc-3.9.1-linux-x86_64.zip && rm -rf protoc3
    

Greeting example

  1. Compile .proto file to .go file:

     $ protoc greet/greetpb/greet.proto --go_out=plugins=grpc:.
    
  2. Run greet server:

     $ go run greet/greet_server/server.go
    
  3. Run greet client:

     $ go run greet/greet_client/client.go
    

Calculator example

  1. Compile .proto file to .go file:

     $ protoc calculator/calculatorpb/calculator.proto --go_out=plugins=grpc:.
    
  2. Run calculator server:

     $ go run calculator/calculator_server/server.go
    
  3. Run calculator client:

     $ go run calculator/calculator_client/client.go
    

(Update) How to run Sum RPC via REST:

  1. Download the following packages:

     $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
     $ go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
    
  2. Compile .proto file to .go file:

     $ protoc -I/usr/local/include -I. \
         -I$GOPATH/src \
         -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
         --go_out=plugins=grpc:. \
         calculator/calculatorpb/calculator.proto
    
  3. Generate reverse-proxy using protoc-gen-grpc-gateway:

     $ protoc -I/usr/local/include -I. \
         -I$GOPATH/src \
         -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
         --grpc-gateway_out=logtostderr=true:. \
         calculator/calculatorpb/calculator.proto
    

    It will generate calculator/calculatorpb/calculator.pb.gw.go file

  4. (Optional) Generate swagger definitions using protoc-gen-swagger:

     $ protoc -I/usr/local/include -I. \
         -I$GOPATH/src \
         -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
         --swagger_out=logtostderr=true:. \
         calculator/calculatorpb/calculator.proto
    

    It will generate calculator/calculatorpb/calculator.swagger.json file

  5. Run calculator server:

     $ go run calculator/calculator_server/server.go
    
  6. Run calculator gateway:

     $ go run calculator/calculator_server/gateway.go
    
  7. Send message via curl:

     $ curl -X POST -k http://127.0.0.1:8081/sum -d '{"first_number": 5, "second_number": 3}'
    

    And you will get response {"sum_result":8} as result

SSL certificate

  1. Install openssl:

     $ sudo apt-get install openssl
    
  2. Create needed certificates, keys:

     $ cd ssl
     $ chmod +x ./generate_files.sh
     $ ./generate_files.sh
    

    if you got Cannot open file .rnd error - create an empty .rnd file

Blog example

  1. Install MongoDB:

    • Download and install MongoDB from here

    • Create folder to store data:

        $ mkdir ~/mongodata
        $ mkdir ~/mongodata/db
      
    • Run MongoDB:

        $ /usr/bin/mongod --dbpath ~/mongodata/db
      
  2. Install UI for your MongoDB:

    • Download Robo 3T from here

    • Switch to download directory and run these commands:

        $ tar -xvzf robo3t*.tar.gz
        $ sudo mkdir /usr/local/bin/robomongo
        $ sudo mv robo3t*/* /usr/local/bin/robomongo
        $ cd /usr/local/bin/robomongo/bin
        $ sudo chmod +x robo3t
      
    • Open .bashrc file:

        $ sudo vim ~/.bashrc
      

      And add the following line to the end of the file:

        alias robomongo='/usr/local/bin/robomongo/bin/robo3t'
      
    • Reload it using the following command:

        $ source ~/.bashrc
      
    • Run robomongo from your terminal:

        $ robomongo
      
  3. Install MongoDB driver for Golang:

     $ go get go.mongodb.org/mongo-driver/mongo
    
Run Blog Example
  1. Compile .proto file to .go file:

     $ protoc blog/blogpb/blog.proto --go_out=plugins=grpc:.
    
  2. Run blog server:

     $ go run blog/blog_server/server.go
    
  3. Run blog client:

     $ go run blog/blog_client/client.go
    

About

This is an implementation of gRPC udemy tutorial


Languages

Language:Go 93.2%Language:Shell 6.8%