+ All Categories
Home > Software > [Greach 2016] Down The RabbitMQ Hole

[Greach 2016] Down The RabbitMQ Hole

Date post: 14-Apr-2017
Category:
Upload: alonso-torres
View: 346 times
Download: 0 times
Share this document with a friend
42
DOWN THE HOLE @alotor
Transcript
Page 1: [Greach 2016] Down The RabbitMQ Hole

DOWN THE

HOLE

@alotor

Page 2: [Greach 2016] Down The RabbitMQ Hole

Alonso Torres@alotor

Page 3: [Greach 2016] Down The RabbitMQ Hole
Page 4: [Greach 2016] Down The RabbitMQ Hole

Spring Integration

WEB SOCKETS

Page 5: [Greach 2016] Down The RabbitMQ Hole

Browser

/GET

200 OK

Page 6: [Greach 2016] Down The RabbitMQ Hole

SEND X

BTW, Your thing

SEND Y

Browser

Page 7: [Greach 2016] Down The RabbitMQ Hole

SUBSCRIBE X

X1

X2

X3

Browser

Page 8: [Greach 2016] Down The RabbitMQ Hole

Web Sockets

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

● Non-blocking asynchronous communication

● Push notifications

Page 9: [Greach 2016] Down The RabbitMQ Hole

OK, maybe you already knew this

Page 10: [Greach 2016] Down The RabbitMQ Hole

Asynchronous communication?

Pub/sub?

But...

Page 11: [Greach 2016] Down The RabbitMQ Hole

● Robust

● Reliable

● Scalable

● Interoperable (as in microservices?)

● Just fast

● Easy to develop (spoiler alert!)

IT’S AWESOME!!

Page 12: [Greach 2016] Down The RabbitMQ Hole

Logic

Logic

WebSockets????

Page 13: [Greach 2016] Down The RabbitMQ Hole

How do I transform

RabbitMQ

in a WebSocket server

Page 14: [Greach 2016] Down The RabbitMQ Hole

rabbitmq-plugins enable rabbitmq_stomp

Page 15: [Greach 2016] Down The RabbitMQ Hole
Page 16: [Greach 2016] Down The RabbitMQ Hole

STOMP

● Simple Text Protocol

● Give semantics to the WebSockets

● For example:

○ CONNECT

○ SEND

○ SUBSCRIBE

Page 17: [Greach 2016] Down The RabbitMQ Hole

STOMP

Logic

Logic

WebSockets

Page 18: [Greach 2016] Down The RabbitMQ Hole
Page 19: [Greach 2016] Down The RabbitMQ Hole

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

Page 20: [Greach 2016] Down The RabbitMQ Hole
Page 21: [Greach 2016] Down The RabbitMQ Hole
Page 22: [Greach 2016] Down The RabbitMQ Hole

STOMPWebSockets AMQP

Twitter StreamingAPI

Page 23: [Greach 2016] Down The RabbitMQ Hole
Page 24: [Greach 2016] Down The RabbitMQ Hole

STOMPWebSockets AMQP

Twitter StreamingAPI

Page 25: [Greach 2016] Down The RabbitMQ Hole

@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)

}

}

}

Page 26: [Greach 2016] Down The RabbitMQ Hole

STOMPWebSockets AMQP

Twitter StreamingAPI

Page 27: [Greach 2016] Down The RabbitMQ Hole

STOMPWebSockets AMQP

Twitter StreamingAPI

Page 28: [Greach 2016] Down The RabbitMQ Hole

@Configuration

@EnableWebSocketMessageBroker

public class WebSocketConfig

extends AbstractWebSocketMessageBrokerConfigurer {

@Override

public void configureMessageBroker(MessageBrokerRegistry config) {

def brokerRelay =

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

// Configure the relay data

}

Page 29: [Greach 2016] Down The RabbitMQ Hole

@Override

public void registerStompEndpoints(StompEndpointRegistry registry) {

registry

.addEndpoint("/stomp")

.setAllowedOrigins("*") // CORS configuration

}

Page 30: [Greach 2016] Down The RabbitMQ Hole

STOMPWebSockets AMQP

Twitter StreamingAPI

Page 31: [Greach 2016] Down The RabbitMQ Hole

service-activator

jsonSerializer.fromJson transformer

request

inbound-adapter

mainService.startTopic

Spring Integration

Page 32: [Greach 2016] Down The RabbitMQ Hole

@Bean

public IntegrationFlow amqpInboundGateway(

ConnectionFactory connectionFactory,

JsonSerializer jsonSerializer,

MainService mainService) {

IntegrationFlows

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

.transform(jsonSerializer, "fromJson")

.handle(mainService, "startTopic")

.get();

}

Page 33: [Greach 2016] Down The RabbitMQ Hole

gateway

jsonSerializer.toJsontransformer

topic

outbound-adapter

workerGateway.broadcastMessage

Spring Integration

Page 34: [Greach 2016] Down The RabbitMQ Hole

@MessagingGateway(defaultRequestChannel = "responseChannel")

interface WorkerGateway {

void broadcastMessage(

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

}

Page 35: [Greach 2016] Down The RabbitMQ Hole

@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)

}

}

}

Page 36: [Greach 2016] Down The RabbitMQ Hole

@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()

}

Page 37: [Greach 2016] Down The RabbitMQ Hole
Page 38: [Greach 2016] Down The RabbitMQ Hole
Page 39: [Greach 2016] Down The RabbitMQ Hole
Page 40: [Greach 2016] Down The RabbitMQ Hole

● Security (JWT, tokens…)

● Infrastructure hell (docker!)

The bad news

Page 41: [Greach 2016] Down The RabbitMQ Hole
Page 42: [Greach 2016] Down The RabbitMQ Hole

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

@alotor

Questions?


Recommended