什么是rpc調用
- restapi調用方式是對數據的crud. 常見的我們寫flash寫個api,或者借助django drf寫個標准的resetapi,一個url可以借助httpget post put delete實現數據的crud. 屬於應用層http調用了
那個文章說:
REST表示的是描述性狀態傳遞(representational state transfer),REST整個就是關於 客戶端和服務端之間的關系的,其中服務端要提供格式簡單的描述性數據,常用的是JSON和XML。
不知道和osi7層的表示層有關系沒
找了個django-microservice例子
訂單服務調用郵件服務
郵件服務是通過url暴漏的, 通過post方式收到參數,發出郵件
- rpc調用方式: 簡單理解為主機B可以調用主機A的函數. 屬於4層tcp調用,效率更高
hostA hostB
f1 f2
tcp:A:port f1 參數
python的rdc協議框架-zerorpc
注: 不需要安裝zeromq
import zerorpc
class Cooler(object):
""" Various convenience methods to make things cooler. """
def add_man(self, sentence):
""" End a sentence with ", man!" to make it sound cooler, and
return the result. """
return sentence + ", man!"
def add_42(self, n):
""" Add 42 to an integer argument to make it cooler, and return the
result. """
return n + 42
def boat(self, sentence):
""" Replace a sentence with "I'm on a boat!", and return that,
because it's cooler. """
return "I'm on a boat!"
s = zerorpc.Server(Cooler())
s.bind("tcp://0.0.0.0:4242")
s.run()
模擬遠程調用
$ zerorpc -j tcp://localhost:4242 add_42 1
43
grpc調用
服務端寫了個類: 暴露了個端口,將這個類通過tcp拋出去
客戶端訪問服務端的函數