using rabbitmq and netty library to implement rpc protocol

23
Tho Luong SILILAB Vietnam Hanoi, March 1, 2014 Rubichat

Upload: tho-q-luong-luong

Post on 15-Jul-2015

457 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Using RabbitMQ and Netty library to implement RPC protocol

Tho Luong

SILILAB Vietnam

Hanoi, March 1, 2014

Rubichat

Page 2: Using RabbitMQ and Netty library to implement RPC protocol

Introduction2

Rubichat:

A Realtime Communication Service

Allow website owner to communicate with customer

more easily

Page 3: Using RabbitMQ and Netty library to implement RPC protocol

Rubichat: customer & operator3

Page 4: Using RabbitMQ and Netty library to implement RPC protocol

Introduction4

Rubichat:

A Realtime Communication Service

Allow website owner to communicate with customer

more easily

Easy to setup:

Page 5: Using RabbitMQ and Netty library to implement RPC protocol

Architecture

RabbitMQ

Customer and operatorFacebook userX user

X1 X2 crane crane

tractor tractor

Java, php, spring, hibernate, amqp,

nodejs, zend framework, netty

sensor Apache

?

MySQL, Redis and MongoDB

Page 6: Using RabbitMQ and Netty library to implement RPC protocol

Issue6

Develop rubiweb project in PHP using Zend

Framework

Some services were implemented in Java (30%)

Should we implement other services in Java or PHP

or both of them?

3 solutions

Page 7: Using RabbitMQ and Netty library to implement RPC protocol

Solution #17

Reimplement all services in PHP

Some services will be implemented in both Java

and PHP not a big problem

PHP and Zend work well with MySQL, do they

still work well with MongoDB and Redis?

Just some services use MongoDB and Redis

not a big problem

Page 8: Using RabbitMQ and Netty library to implement RPC protocol

Solution #18

When Java wants to use a service that is

implemented in PHP port this implementation to

Java?

Some services are hard to implement, ex: access

control service

Solution #1: complicated and not easy for

maintainance.

Page 9: Using RabbitMQ and Netty library to implement RPC protocol

Solution #29

Beauty and maintainability are #1 priority

All services will be implemented in Java

PHP calls Java service implementations remotely

via RabbitMQ

PHP Java

e1

e2

req

resp

OK

Page 10: Using RabbitMQ and Netty library to implement RPC protocol

Solution #210

Problem: how to receive response properly?

PHP -01

PHP-02Resp-02

Resp-01

Mission impossible

Page 11: Using RabbitMQ and Netty library to implement RPC protocol

Solution #211

How about using one queue for each PHP request?

PHP-02

PHP-01

PHP-n

e….. …..

At least, one “dirty” queue is produced after each

PHP request lifecycle

No, RabbitMQ must manage a lot of queues.

Page 12: Using RabbitMQ and Netty library to implement RPC protocol

Solution #312

RPC: PHP invokes Java API remotely

PHPJava

Server

X protocol

Issues:

1. System architecture

2. Protocol selection

3. Technology selections for Java Server

Flappy

Page 13: Using RabbitMQ and Netty library to implement RPC protocol

System architecture13

Option 1: flappy accesses database directly

PHPJava

Server

X protocol

Flappy

MySQ

L

Mong

oDBRedis

Option 2: flappy serves PHP connections, access

service implentations via RabbitMQ

PHPJava

Server

Flappy

Rab

bit

MQ

MySQ

L

Mong

oDBRedis

Tractor

Tractor

…..

…..

Page 14: Using RabbitMQ and Netty library to implement RPC protocol

Protocol selection14

Common RPC protocols

XML RPC

JSON RPC

Protobuf (google)

Thrift (facebook)

SOAP

Flappy uses XMPP (RFC 3920,RFC 3921) message

format

Easy to control

Familiar to Rubichat developers

Page 15: Using RabbitMQ and Netty library to implement RPC protocol

XMPP in JSON format15

Stanza: IQ

Stanza: M

Page 16: Using RabbitMQ and Netty library to implement RPC protocol

Binary data format16

Data type: 1 byte

RABBIT_TRACTOR_IN,

RABBIT_TRACTOR_OUT, FLAPPY_IN,

FLAPPY_OUT, NA

Length: int: 4 byte

Body

lengthtype body

1 4 length

Page 17: Using RabbitMQ and Netty library to implement RPC protocol

Technologies selection17

Socket library: Netty, a great library for network

application

Twitter uses Netty to improve search engine

performance. (3x faster)

Interceptor design in Tractor server is influenced by

Netty

Page 18: Using RabbitMQ and Netty library to implement RPC protocol

Netty advantages18

Asynchronous I/O: decouple threads from Client

Socket Connections

Rich Buffer data structure

Advanced Components for More Rapid

Development

Event Model based on the Interceptor Chain

Pattern

Page 19: Using RabbitMQ and Netty library to implement RPC protocol

Test result19

Create 100 users, find them and measure time:

Service Query Tractor(ms) Client (ms) ∆ (ms)

Create 100 5196 5665 469

Find 1 187 203 16

Service Query Tractor(ms) Client (ms) ∆ (ms)

Create 100 4414

Find 1 34

(New FlappyClient implementation: use single connection(March 26, 2014))

Page 20: Using RabbitMQ and Netty library to implement RPC protocol

20

Demo:

1. Flappy Server

2. Tractor

Page 21: Using RabbitMQ and Netty library to implement RPC protocol

Solution disadvantages21

response

Flappy

PHP

Java Server

request

.phpModel

ToJSON()

.jsonTo

PhpModel()

.jsonTo

JavaModel()

.javaModel

ToJSON()

Write data-mappers

manually

Page 22: Using RabbitMQ and Netty library to implement RPC protocol

Future work22

Tool for generating data mappers

Optimize PHP FlappyClient and develop clients for

other languages

Page 23: Using RabbitMQ and Netty library to implement RPC protocol

Thank you!

23