CodeSOD: Bunny Bunny
When you deploy any sort of complicated architecture, like microservices, you also end up needing to deploy some way to route messages between all the various bits and bobs in your application. You could build this yourself, but you'll usually use an off-the-shelf product, like Kafka or RabbitMQ.
This is the world Tina lives in. They have a microservice-based architecture, glued together with a RabbitMQ server. The various microservices need to connect to the RabbitMQ, and thus, they need to be able to check if that connection were successful.
Now, as you can imagine, that would be a built-in library method for pretty much any MQ client library, but if people used the built-in methods for common tasks, we'd have far fewer articles to write.
Tina's co-worker solved the "am I connected?" problem thus:
def are_we_connected_to_rabbitmq(): our_node_ip = get_server_ip_address() consumer_connected = False response = requests.get("http://{0}:{1}@{2}:15672/api/queues/{3}/{4}".format( self.username, self.password, self.rabbitmq_host, self.vhost, self.queue)) if response and response.status_code == 200: js_response = json.loads(response.content) consumer_details = js_response.get('consumer_details', []) for consumer in consumer_details: peer_host = consumer.get('channel_details', {}).get( 'peer_host') if peer_host == our_node_ip: consumer_connected = True break return consumer_connected
To check if our queue consumer has successfully connected to the queue, we send an HTTP request to one of RabbitMQ's restful endpoints to find a list of all of the connected consumers. Then we check to see if any of those consumers has our IP address. If one does, that must be us, so we must be connected!
[Advertisement] ProGet can centralize your organization's software applications and components to provide uniform access to developers and servers. Check it out!