用thrift搭乙個單程序服務

2022-03-06 14:34:03 字數 3658 閱讀 1817

首先寫乙個服務的功能類,例如print 一些東西

hellohandler.py

#!/bin/python

#encoding=utf8

import os

import sys

import json

import hashlib

import logging

import time

import random

import traceback

import signal

import re

from datetime import datetime

cur_dir = os.path.dirname(os.path.abspath(__file__))

#載入thrift介面的路徑

thrift_path = os.path.sep.join([cur_dir, 'utils', 'thrift', 'gen-py'])

class hellohandler():

def __init__(self):

self.name = none

def helloworld(self,name):

self.name = name

print name

return name

if __name__ == '__main__':

name = "haha"

tt_obj = hellohandler()

tt_obj.helloworld(name)

time.sleep(3)

服務端程式:hello_server.py

#!/bin/python

#encoding=utf8

import os

import sys

import json

import hashlib

import logging

import pymongo

import time

import traceback

import signal

import urllib

import urllib2

import mimetypes

import re

import configparser

from datetime import datetime

from bson import json_util

from collections import defaultdict

from multiprocessing import process

from hellohandler import hellohandler

from ie_service import ieservice

from thrift.transport import tsocket

from thrift.transport import ttransport

from thrift.protocol import tbinaryprotocol

from thrift.protocol import tcompactprotocol

from thrift.server import tserver

import hellohandler

cur_dir = os.path.dirname(os.path.abspath(__file__))

cf = configparser.configparser()

conf_fn = os.path.sep.join([cur_dir, '../conf', 'thrift.conf'])

port = cf.read(conf_fn)

port = int(cf.get('thrift', 'port')) #這裡設定埠

if __name__ == '__main__':

'''level=logging.info \

level=logging.warning \

filename=cur_dir+'/var/sf_server.log' \

'''logging.basicconfig( \

level=logging.warning \

, format='%(thread)d %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' \

)handler = hellohandler()

processor = ieservice.processor(handler)

transport = tsocket.tserversocket(port=port)

tfactory = ttransport.tbufferedtransportfactory()

pfactory = tbinaryprotocol.tbinaryprotocolfactory()

server = tserver.tthreadedserver(processor, transport, tfactory, pfactory)

print 'starting server'

server.serve()

manager.join()

print 'done'

ie_server.thrift

namespace py ie_service

service ieservice

string helloworld(1:string req_json);

客戶端程式

client.py

#!/bin/python

#encoding=utf8

import sys

from ie_service import *

from ie_service import ieservice

from thrift import thrift

from thrift.transport import tsocket

from thrift.transport import ttransport

from thrift.protocol import tbinaryprotocol

import json

try:

transport = tsocket.tsocket('localhost', 62127) #埠為服務端設定的埠

transport = ttransport.tbufferedtransport(transport)

protocol = tbinaryprotocol.tbinaryprotocol(transport)

client = ieservice.client(protocol)

transport.open()

req_dic =

print 'start'

res = client.helloworld(json.dumps(req_dic))

print res

transport.close()

except thrift.texception as e:

print 'exceptino'

print e

用sc create 建立乙個服務

用sc create 建立乙個服務 增加到登錄檔中 如下 在命令列模式下執行 注意這裡的格式,後面是必須空一格的,否則會出現錯誤。在提示建立成功後,可以直接輸入 net start testservice 來啟動服務,或者可以直接在 管理工具 的 服務 中直接啟動。c documents and s...

5 2 搭乙個zookeeper集群

在第乙個已經啟動起來的zookeeper,如果停止,用.zkserver.sh stop 如果要搭集群,那麼一定要修改配置檔案 在conf裡面的zoo.cfg裡修改 vim zoo.cfg 詳細解釋 zookeeper的預設配置檔案為zookeeper conf zoo sample.cfg,需要將...

自己來搭乙個vue專案

準備npm node cli 檢查方式 npm v node v vue version shift 滑鼠右鍵 開啟 powershell 如果沒許可權 管理員身份開啟powershell set executionpolicy remotesigned 選擇yvue create 專案名 空格選中...