-
Notifications
You must be signed in to change notification settings - Fork 419
Try gRPC Swift in a Google Compute Engine VM
Google Compute Engine provides inexpensive and easy-to-configure virtual machines to anyone with a Google Cloud developer account. You can sign up for a free Google Cloud developer account here.
Visit console.cloud.google.com and use the menu to go to the Compute Engine > VM Instances section.
Then use the "Create Instance" link to go to a form where you can configure your VM.
- Verify that the region is somewhere near you (you might use us-west1-c in California).
- Use the default machine type. Smaller VMs might work, but Swift builds use a lot of memory.
- Change the boot disk from the default to the Ubuntu 16.04 LTS image.
- Under "Identity and API Access" select "Allow full access to all Cloud APIs".
- Under "Firewall" enable both HTTP and HTTPS traffic.
After you've made these settings, press the "Create" button!
After your VM has been created, click on the SSH link in the table of VMs that will appear. This will open a window with a shell on your new virtual machine.
sudo apt-get update
sudo apt-get install -y clang clang-3.8 lldb-3.8 libicu-dev libtool \
libcurl4-openssl-dev libbsd-dev build-essential \
libssl-dev uuid-dev curl unzip
cd
mkdir -p local
export PATH=.:$HOME/local/bin:$PATH
source ~/.bashrc
SWIFT_URL=https://swift.org/builds/swift-5.0.1-release/ubuntu1604/swift-5.0.1-RELEASE/swift-5.0.1-RELEASE-ubuntu16.04.tar.gz
echo $SWIFT_URL
curl -fSsL $SWIFT_URL -o swift.tar.gz
tar -xzf swift.tar.gz --strip-components=2 --directory=local
PROTOC_URL=https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/protoc-3.7.1-linux-x86_64.zip
echo $PROTOC_URL
curl -fSsL $PROTOC_URL -o protoc.zip
unzip protoc.zip -d local
git clone https://github.com/grpc/grpc-swift
cd grpc-swift
make
If you have build errors, try this instead of make
:
swift build -Xcc -ISources/BoringSSL/include/openssl -Xlinker -lz
.build/debug/Echo serve &
.build/debug/Echo get
.build/debug/Echo expand
.build/debug/Echo collect
.build/debug/Echo update
The four services that you've called demonstrate the four API modes of gRPC: Unary, Server Streaming, Client Streaming, and Bidirectional Streaming. See echo.proto for the service definition and main.swift and EchoProvider.swift for client and server implementations, respectively. As a gRPC user, that's all the code that you need to write! Everything else is in support libraries or generated by the Protocol Buffer and gRPC plugins.