JINIers
json data → pub/sub 전송 본문
참고 : https://www.youtube.com/watch?v=xOtrCmPjal8
감사합니다.선생님.. 덕분에 광명 찾았쯥니다 ㅜㅜ
1. pub/sub > create topic
topic id : 작성
2. add a service account
iam&admin > service account > create service account >
- service account name : 작성하면 account id는 자동으로 생성
- role : pub/sub admin
생성한 계정 클릭 > keys 탭 > add key > create key > key type : json > 다운로드 > 파일명 변경(pubsub-test-privatekey.json)
vm instance > vm create : pubsub-test > edit > ssh 키 입력
* 참고 : https://jiniers.tistory.com/45 의 'ssk key값 추가'
3. build the python publisher
pubsub-test 인스턴스 생성 후 ssh >
ssh 접속 시 해야할 것
apt-get update
apt-get install pip python3
pip install flask
pip install flask-restx
pip install requests
pip install bcrypt
pip install PyJWT
pip install --upgrade google-cloud-pubsub
vim publisher.py
import os
from google.cloud import pubsub_v1
credentials_path = '/PATH/TO/YOU/PRIVATE/JSON/FILE/HERE/myFile.privateKey.json'
# 나는 /root에 넣어놨기 때문에 /root/pubsub-test-privatekey.json 을 넣어주었다.
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
publisher = pubsub_v1.PublisherClient()
topic_path = 'MY_TOPIC_NAME_HERE'
# pub/sub > topic > topic name 복사 > 붙여넣기
data = 'A garden sensor is ready!'
data = data.encode('utf-8')
attributes = {
'sensorName': 'garden-001'
, 'temperature': '75.0'
, 'humidity': '60'
}
future = publisher.publish(topic_path, data)
print(f'published message id {future.result()}')
python3 publisher.py > 메시지 출력 확인
pub/sub > topic 클릭 > messages > Select a Cloud Pub/Sub Subscription to pull messages from 선택 > pull
하면 메시지가 뙇!!! 뜸!!!!
흑흑 드디어 pub/sub에 데이터가 ㅜㅜ
하지만 아직 멀었음
내가 원하는건 json 출력..
4. python subscriber
vim subscriber.py
내용 추가
import os
from google.cloud import pubsub_v1
from concurrent.futures import TimeoutError
credentials_path = '/PATH/TO/YOU/PRIVATE/JSON/FILE/HERE/myFile.privateKey.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
timeout = 5.0 # timeout in seconds
subscriber = pubsub_v1.SubscriberClient()
subscription_path = 'MY_SUBSCRIPTION_NAME_HERE'
# pub/sub > subscriptions > subscription name 복사 > 붙여넣기
def callback(message):
print(f'Received message: {message}')
print(f'data: {message.data}')
if message.attributes:
print("Attributes:")
for key in message.attributes:
value = message.attributes.get(key)
print(f"{key}: {value}")
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f'Listening for messages on {subscription_path}')
with subscriber: # wrap subscriber in a 'with' block to automatically call close() when done
try:
# streaming_pull_future.result(timeout=timeout)
streaming_pull_future.result() # going without a timeout will wait & block indefinitely
except TimeoutError:
streaming_pull_future.cancel() # trigger the shutdown
streaming_pull_future.result() # block until the shutdown is complete
# pub/sub > subscriptions > subscription name 복사 > 붙여넣기
후후훟
5. adding JSON data
publiser.py 에 **attrebute 추가 전엔 json 데이터가 안뜬다. 그래서 파일에 **attrebute를 추가해준다.
# publisher.py 코드
# publisher.py
import os
from google.cloud import pubsub_v1
credentials_path = '/PATH/TO/YOU/PRIVATE/JSON/FILE/HERE/myFile.privateKey.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
publisher = pubsub_v1.PublisherClient()
topic_path = 'MY_TOPIC_NAME_HERE'
data = 'A garden sensor is ready!'
data = data.encode('utf-8')
attributes = {
'sensorName': 'garden-001',
'temperature': '75.0',
'humidity': '60'
}
future = publisher.publish(topic_path, data, **attributes)
print(f'published message id {future.result()}')
그럼 이렇게 json 데이터가 같이 뜬다.
후 이제 이 받은 json data를 pub/sub으로 보내면 돼 ㅜ
복습 끝
'GCP > 구성연습' 카테고리의 다른 글
Cloud Function (0) | 2022.06.16 |
---|---|
jupyterlab 이용 빅쿼리 호출하기(Bigquery → Vertex AI) (0) | 2022.05.26 |
[GCP] 기본 구성 aws → gcp 구성 변경_5-1(LB 404 not found 도메인 재연결) (0) | 2022.04.14 |
web-server, WAS server 설치하기 (0) | 2022.04.07 |
[GCP] 기본 구성 aws → gcp 구성 변경_6(ssh putty 접속) (0) | 2022.03.21 |