Quickstart
Quickstart¶
The following sections are outlined to illustrate the installation of the package and the initial creation process of a consumer and a producer until a first RPC request is made.
To illustrate it we are going to work with a base example creating a consumer named foobar and a producer named foobar_client.
Installation¶
Package is distributed under PyPi, so it can be easily installed with pip:
pip install -U guirpc
To install it directly from source:
git clone https://github.com/guiloga/guirpc
touch setup.py
echo "import setuptools \
setuptools.setup()" > setup.py
python3 setup.py install
Manager¶
A command-line manager application called guirpc is provided with the installation of the package. It has commands that will handle the initialization of pre-created fixtures as starting templates which will allow to simplify a initial setup of a consumer or a producer.
To check the installation and to take a look at the the list of possible commands:
guirpc --help
Init a consumer¶
In order to setup the initialization of a consumer server application
run the following command:
Note
We will call it foobar
consumer in our base example.
guirpc initconsumer foobar
This will generate an .ini configuration file associated with the consumer and a package structure like that one:
foobar
├── __init__.py
├── server.py
foobar.ini
Init a producer¶
client stub
that will call the consumer methods
that we have created in the previous step run the following command:Note
We will call it foobar_client
producer in our base example.
guirpc initproducer foobar_client
This will generate an .ini configuration file associated with the producer and package structure like that one:
foobar_client
├── __init__.py
├── client.py
foobar_client.ini
Make an RPC request¶
Message Broker¶
docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:management-alpine
Preliminary notes¶
Note
Alternatively to avoid declaring the connection parameters in the configuration file, the connection uri can be globally set by the AMQP_URI environment variable. (i.e AMQP_URI=”amqp/guest:guest@localhost:5672/%2F”)
Configure and Run the Server Consumer¶
echo "export CONSUMER_CONFIG=<path-to-foobar.ini>" >> ~/.bashrc
guirpc runconsumer
Configure the Client Stub¶
echo "export PRODUCER_CONFIG=<path-to-foobar_client.ini>" >> ~/.bashrc
Note
The guirpc.amqp.utils.ClientConnector class accepts an argument that is the name of the environment variable that points the the .ini configuration file. The default variable name is PRODUCER_CONFIG.
Let’s put it all together¶
registered FaaS
in our consumer.#!/usr/bin/env python
from foobar_client import client
def make_request1():
sum_body = {'foo': 5, 'bar': 12}
print(f"RPC Request to 'foobar_sum'")
print(f"\tbody: {sum_body}")
x_resp = client.foobar_sum(sum_body)
if x_resp.is_error:
print(f"\tResponse error: {x_resp.error_message}")
print(f"\tResponse success: {x_resp.object}")
res = x_resp.object['result']
assert res == 17
def make_request2():
sentence = 'foobar!'*5 # 10 foo bar counts
print(f"RPC Request to 'foobar_count'")
print(f"\tbody: {sentence}")
x_resp = client.foobar_count(sentence)
if x_resp.is_error:
print(f"\tResponse error: {x_resp.error_message}")
print(f"\tResponse success: {x_resp.object}")
res = int(x_resp.object)
assert res == 10
def main():
make_request1()
make_request2()
if __name__ == '__main__':
main()
Note
Look at the consumer log stream to see how the messages are received; making an acknowledgement when it is received immediately by the consumer and then passing trough the registered function (consuming it) and sending a reply to the client.