Projeto para demonstracao das funcionalidade do PUBSUB com Python Autor : Claudio Lisboa Referencias: https://cloud.google.com/pubsub/docs
Preparacao do Ambiente GCP Desenvolvimento. Versao 1.00- 30/05/2020 Google Cloub Pub/Sub Pyhton configuracao de ambiente
Referência: https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
sudo apt-get update && sudo apt-get install google-cloud-sdk
Use o comando abaixo para executar várias tarefas comuns de configuração do SDK. entre elas: autorizar as ferramentas do SDK a acessar o Google Cloud Platform usando as credenciais da sua conta de usuário e definir a configuração padrão do SDK.
Para iniciar o SDK:
gcloud init
gcloud config set project projeto-pubsub
Exportar a chave de servico para funcionar as autenticacoes: https://cloud.google.com/docs/authentication/getting-started
Este passo é importante para configurar o local da chave de segurança IAM para autenticação das ferramentas do google
export GOOGLE_APPLICATION_CREDENTIALS="/home/lisboa/cglsoft/gcp_Carrefour/key.json"
sudo apt-get update
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
sudo apt install python3-pip
sudo pip3 install virtualenv
Step 4: Launch your Python 3 virtual environment, here the name of my virtual environment will be env3
virtualenv -p python3 env3
. env3/bin/activate ##### or source env3/bin/activate which does exactly the same thing
python -- version
this command will show you what is going on: the python executable you are using is now located inside your virtualenv repository
which python
Criacao do ambiente virtual PYTHON
python -m virtualenv env
Ativar o ambiente virtual
source env/bin/activate
Instalacao biblioteca Pub/Sub PYTHON
pip install --upgrade google-cloud-pubsub
Comandos SHELL para verificar os tópicos
gcloud pubsub topics create my-topic
gcloud pubsub subscriptions create my-sub --topic my-topic
Comandos pubsub Listar topicos e subscriptions
gcloud pubsub topics list
gcloud pubsub subscriptions list
gcloud info
Execucao em dois terminais diferentes para validar a solução de publicação e consumo de informações:
Check com os programas Publisher e Subscriber - Python:
python publisher3.py projeto-pubsub my-topic
python subscriber.py projeto-pubsub my-topic
Comandos terminal para PUB/SUB para apagar Subscriptions e TOPIC
gcloud pubsub subscriptions delete my-sub
gcloud pubsub topics delete my-topic
Claudio Lisboa
Copyright © 2008-2020, CGLSOFT Desenvolvimento. Released under the MIT License.