[Greach 2016] Down The RabbitMQ Hole

Post on 14-Apr-2017

346 views 0 download

transcript

DOWN THE

HOLE

@alotor

Alonso Torres@alotor

Spring Integration

WEB SOCKETS

Browser

/GET

200 OK

SEND X

BTW, Your thing

SEND Y

Browser

SUBSCRIBE X

X1

X2

X3

Browser

Web Sockets

Web standard for bi-directional communication between the browser and a Web Server

● Non-blocking asynchronous communication

● Push notifications

OK, maybe you already knew this

Asynchronous communication?

Pub/sub?

But...

● Robust

● Reliable

● Scalable

● Interoperable (as in microservices?)

● Just fast

● Easy to develop (spoiler alert!)

IT’S AWESOME!!

Logic

Logic

WebSockets????

How do I transform

RabbitMQ

in a WebSocket server

rabbitmq-plugins enable rabbitmq_stomp

STOMP

● Simple Text Protocol

● Give semantics to the WebSockets

● For example:

○ CONNECT

○ SEND

○ SUBSCRIBE

STOMP

Logic

Logic

WebSockets

https://github.com/alotor/greach2016-rabbitmq

STOMPWebSockets AMQP

Twitter StreamingAPI

STOMPWebSockets AMQP

Twitter StreamingAPI

@Service

class MainService {

@Autowired

WorkerGateway workerGateway

public void startTopic(Map message) {

new TwitterWorker(auth, message.topic).fetchTweets {

String topic, String json ->

def tweet = jsonSerializer.fromJson(json.bytes)

workerGateway.broadcastMessage(topic, tweet)

}

}

}

STOMPWebSockets AMQP

Twitter StreamingAPI

STOMPWebSockets AMQP

Twitter StreamingAPI

@Configuration

@EnableWebSocketMessageBroker

public class WebSocketConfig

extends AbstractWebSocketMessageBrokerConfigurer {

@Override

public void configureMessageBroker(MessageBrokerRegistry config) {

def brokerRelay =

config.enableStompBrokerRelay("/queue", "/topic")

// Configure the relay data

}

@Override

public void registerStompEndpoints(StompEndpointRegistry registry) {

registry

.addEndpoint("/stomp")

.setAllowedOrigins("*") // CORS configuration

}

STOMPWebSockets AMQP

Twitter StreamingAPI

service-activator

jsonSerializer.fromJson transformer

request

inbound-adapter

mainService.startTopic

Spring Integration

@Bean

public IntegrationFlow amqpInboundGateway(

ConnectionFactory connectionFactory,

JsonSerializer jsonSerializer,

MainService mainService) {

IntegrationFlows

.from(Amqp.inboundAdapter(connectionFactory, "request"))

.transform(jsonSerializer, "fromJson")

.handle(mainService, "startTopic")

.get();

}

gateway

jsonSerializer.toJsontransformer

topic

outbound-adapter

workerGateway.broadcastMessage

Spring Integration

@MessagingGateway(defaultRequestChannel = "responseChannel")

interface WorkerGateway {

void broadcastMessage(

@Header("routingKey") String topic, @Payload Map message)

}

@Service

class MainService {

@Autowired

WorkerGateway workerGateway

public void startTopic(Map message) {

new TwitterWorker(auth, message.topic).fetchTweets {

String topic, String json ->

def tweet = jsonSerializer.fromJson(json.bytes)

workerGateway.broadcastMessage(topic, tweet)

}

}

}

@Bean

public IntegrationFlow amqpOutboundGateway(

AmqpTemplate amqpTemplate,

JsonSerializer jsonSerializer) {

IntegrationFlows

.from("responseChannel")

.transform(jsonSerializer, "toJson")

.handle(Amqp.outboundAdapter(amqpTemplate)

.exchangeName("amq.topic")

.routingKeyExpression("headers.routingKey"))

.get()

}

● Security (JWT, tokens…)

● Infrastructure hell (docker!)

The bad news

https://github.com/alotor/greach2016-rabbitmq

@alotor

Questions?