Try to use gRPC on iOS

Brief introduction

gRPC, google remote procedure call frame, the transport protocol used HTTP2, serialization protocol protobuf.
gRPC use HTTP2 Transfer Protocol protobuf sequence of binary data, have extremely high efficiency, low resource utilization.
github repository address: https: //github.com/grpc/grpc, the repository contains multiple languages on shared core C library src / core (C ++, Python , Ruby, Objective-C, PHP, C #) to achieve the gRPC library source code.
gRPC some of the features:

  • Simple service definition
  • Work across languages ​​and platforms
  • Quick Start and extended
  • Two-way streaming and authentication integration

Use GRPC several core Step
1) Defining a service defined services (in .proto file and this is the main protocol backstage interaction)
2) Generating grpc code generation grpc Code
3) Writing a server-side preparation of certain services provided to the customer service end use (similar interface)
4) Implementation realized Server service
5) writing a client to write client-side code (integrated GRPC)
6) AN rpc calling code (Interface) calls based on RPC protocol (.proto document agreed protocol)
advantages: the client flow and take advantage of advanced features links to help conserve bandwidth and reduce the number of TCP link, saving CPU usage, and battery life.

Objective-C Quick Start

system requirement

  • The minimum iOS version 7.0, i.e. iOS version> = 7.0
  • OS X 10.11 and above, i.e., OS X 10.11> = 10.11

Prerequisite
  1, CocoaPods> = 1.0 Check Cocoapods Version: POD -version
  $ CocoaPods the install the sudo GEM
  $ POD Setup
  the PS: CocoaPods is iOS development, development macOS package dependency management tools, such as Java effect of Maven, nodejs of NPM.
  2, Xcode> = 7.2

  

  3, Homebrew
  $ Ruby -e "$ (curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  PS: Homebrew package management system is a free and open source, with to simplify the software installation process on the system macOS
  . 4, autoconf, automake, libtool, PKG-config
  $ BREW the install autoconf automake libtool PKG-config

GRPC install plug-ins and binary library file

From https://github.com/grpc/grpc download gRPC source, unpacked into the grpc directory:
$ the make
encountered an error: error is very simple, prompt system lacks cares bag

From https://github.com/c-ares/c-ares After downloading cares, extract placed under / grpc / third_party / cares / cares directory, re-make, but also being given a ~, suggested that the lack OpenSSL, Le me to install a lack of a, when is the head ah! grpc of third_party in a warehouse just given the address of the library, but when third_party required to actually compile the source code when all the files.

Effective Solution: Git for source code (requires network good):
$ git clone --recursive -b v1.23.0 https://github.com/grpc/grpc.git
$ cd GRPC
$ git submodule Update --init
$ the make
$ [sudo] make install
after completing the installation will gRPC header files in / usr / local / include, in the program may include the use of direct, link libraries are installed in / usr / local / lib in the link for the program compiled successfully use .

 

Installation protoc compiler

$ brew tap grpc/grpc
$ brew install protobuf

Run Server

For this example application, we need gRPC server running on the local computer. gRPC Objective-C API supports the creation gRPC client, but does not support creating gRPC server. So, we build and run C ++ server in the same repository:
$ cd examples / cpp / the HelloWorld
$ the make
$ ./greeter_server &

C ++ server process greeter_server run up ~

Run Client

1) generate client libraries and dependencies
$ cd ../../objective-c/helloworld
$ POD install

2) Run the client App
$ Open HelloWorld.xcworkspace
This will open the application using Xcode project. Xcode window by pressing the upper left corner of the "Run" button to run the application in the iOS simulator. You can check the calling code main.m in, and view the results in Xcode console. Code contains the string "Objective-C" is sent to the local server HLWHelloRequest. Server response HLWHelloResponse, which contains a string "Hello Objective-C", and then output to the console.

Congratulations! You just use gRPC successfully run the iOS simulator -C ++ client server applications.

iOS real test

Insert iphone, Xcode recognize iphone, and selected as the current device configuration information engineering signature, you can build run!

Since my Mac OS is running on the physical machine virtual machine, while the physical machine using a wired fiber-optic network, and iphone phones used by wireless network is not in the same LAN, it can not network connectivity.
My laptop (Windows7 x64) using a wireless network, ip address 192.168.2.15, iphone mobile phone ip address 192.168.2.231, is in the same wireless LAN, you can run the gRPC C ++ HelloWorld Server program on the laptop 192.168.2.15, but the program requires Windows version of gRPC, compiled Windows version of gRPC and use its HelloWorld Demo program can refer to the article "grpc1.18.0 c ++ windows visual studio 2017 (vs2017) version of the compiler (entry)" and "grpc1.18.0 c ++ windows visual studio 2017 (vs2017) version Helloworld examples and analysis. "

I show here a direct final result:
1) GRPC the HelloWorld C ++ Server

Note: Due to the server program is compiled using VS2017 x64 out, run-time and some need zlib.dll VS2017 runtime library, so you need to install VS2017 in order to ensure that the run (only C ++ developers to install some components can be).
Little knowledge : listen address of the server is "0.0.0.0:50051",0.0.0.0 on the server side it means that all IPV4 addresses on the machine, if a service has multiple IP address (192.168.1.2 and 10.1.1.12) then if we listen address is 0.0.0.0 then we set either through IP192.168.1.2 are still 10.1.1.12 can access the service.
2) gRPC Object-C HelloWorld Client
server address will be connected by Xcode @ "localhost.15: 50051" modify @ "192.168.2.15:50051" , after selecting iOS real machine is connected, run!

IOS tests used real machine, borrowed with:

Congratulations! You just use gRPC real success running iOS Client -C ++ server applications.

reference

Guess you like

Origin www.cnblogs.com/MakeView660/p/11512519.html