我创建了一个基于Apache Thrift的Ruby Web服务器,但是客户端(也用于ruby,用于单元测试)拒绝工作并不断告诉我Thrift::TransportException: Could not connect to 127.0.0.1:8001: Connection refused - connect(2) for 127.0.0.1:8001
或Thrift::TransportException: end of file reached
。尝试了一堆不同的服务器实现和传输,但这似乎不起作用。
服务器运行时,lsof -i :8001
显示
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 19073 lafickens 9u IPv4 0x878e6fd36b981a71 0t0 TCP *:vcom-tunnel (LISTEN)
所以我认为服务器正在运行。
这是服务器代码:
class Server
attr_reader :name
def initialize(name)
@LOGGER = Logger.new $stdout
@name = name
@started = false
@processor = ::Thrift::MultiplexedProcessor.new
@processor.register_processor 'User Service', Thrift::UserService::Processor.new(Handlers::UserServiceHandler.new)
@processor.register_processor 'Sync Service', Thrift::SyncService::Processor.new(Handlers::SyncServiceHandler.new)
end
def start
@transport = ::Thrift::ServerSocket.new(Options.get('port'))
@transport_factory = ::Thrift::BufferedTransportFactory.new
@protocol_factory = ::Thrift::CompactProtocolFactory.new
@server = ::Thrift::ThreadPoolServer.new @processor, @transport, @transport_factory, @protocol_factory
@server_thread = Thread.new {
@server.serve
}
@started = true
@LOGGER.info('Server started successfully')
end
def stop
return if @server_thread.nil?
@server_thread.exit
@transport.close
@started = false
@LOGGER.info('Server stopped successfully')
end
def restart
stop
start
end
def started?
@started
end
end
客户端代码(实际上是单元测试)
class TestUserServiceHandler < Test::Unit::TestCase
def setup
@server = Billboard::Server.new 'test handler'
@server.start
@port = Billboard::Options.get 'port'
@transport = ::Thrift::BufferedTransport.new(::Thrift::Socket.new('127.0.0.1', @port))
@binary_protocol = ::Thrift::BinaryProtocol.new @transport
@multiplexed_protocol = ::Thrift::MultiplexedProtocol.new @binary_protocol, 'mprotocol'
@client = Billboard::Thrift::UserService::Client.new @multiplexed_protocol
@transport.open
end
def teardown
@server.stop
@transport.close
end
def test_authenticate
@client.authenticate('test', 'test')
end
# And other tests...
end
提前致谢。