Skip to content

Try gRPC Swift in a Google Compute Engine VM

Tim Burks edited this page Apr 29, 2019 · 11 revisions

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.

Create a GCE virtual machine.

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!

Set up your virtual machine to run Swift.

Open a shell on your VM.

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.

Use apt-get to install packages needed to run Swift.

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

Create a ~/local directory for some tools that we'll download.

cd
mkdir -p local

Add $HOME/local/bin to your path by adding the following at the end of your .bashrc.

export PATH=.:$HOME/local/bin:$PATH

After you've done the above step, reload .bashrc.

source ~/.bashrc

Download and install Swift in ~/local. Note that your VM is running Ubuntu 16.04.

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

Download and install the protocol buffer compiler in ~/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

Try grpc-swift.

Download the grpc-swift repository.

git clone https://github.com/grpc/grpc-swift

Build the Echo example.

cd grpc-swift
make

If you have build errors, try this instead of make:

swift build -Xcc -ISources/BoringSSL/include/openssl -Xlinker -lz

Start the Echo server.

.build/debug/Echo serve &

Test the server with the Echo client.

.build/debug/Echo get
.build/debug/Echo expand
.build/debug/Echo collect
.build/debug/Echo update

You've done it!

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.