Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JINIers

json data → pub/sub 전송 본문

GCP/구성연습

json data → pub/sub 전송

JINIers 2022. 5. 12. 10:50

참고 : https://www.youtube.com/watch?v=xOtrCmPjal8 
감사합니다.선생님.. 덕분에 광명 찾았쯥니다 ㅜㅜ


1. pub/sub > create topic

 

topic id :  작성

create topic
topic 생성 확인


2. add a service account

iam&admin > service account > create service account > 

  • service account name : 작성하면 account id는 자동으로 생성
  • role : pub/sub admin

 

add service account

 


생성한 계정 클릭 > keys 탭 > add key > create key > key type : json > 다운로드 > 파일명 변경(pubsub-test-privatekey.json)

 

json key file 다운로드


vm instance > vm create : pubsub-test > edit > ssh 키 입력

* 참고 : https://jiniers.tistory.com/45 의 'ssk key값 추가'

ssh key 인증

 

json key file 이동


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()}')

 


복붙할 topic name

 

python3 publisher.py > 메시지 출력 확인

메시지 출력

 

pub/sub > topic 클릭 > messages > Select a Cloud Pub/Sub Subscription to pull messages from 선택 > pull

하면 메시지가 뙇!!! 뜸!!!!

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()}')

**attribute  추가 후

그럼 이렇게 json 데이터가 같이 뜬다.

 

 

json data로 받기 test


 

후 이제 이 받은 json data를 pub/sub으로 보내면 돼 ㅜ

복습 끝

 

Comments