Skip to content
Branch: master
Find file History
Latest commit 5eeeaf0 Aug 26, 2019
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
eureka Spring cloud gateway websocket (#5) Aug 26, 2019
gateway Spring cloud gateway websocket (#5) Aug 26, 2019
rabbitmq Spring cloud gateway websocket (#5) Aug 26, 2019
websocket-server Spring cloud gateway websocket (#5) Aug 26, 2019
.editorconfig Spring cloud gateway websocket (#5) Aug 26, 2019
.gitignore
README.md Update README.md Aug 26, 2019
docker-compose.yml
example.html
rabbit.yml Spring cloud gateway websocket (#5) Aug 26, 2019
ws-demo-1.gif Spring cloud gateway websocket (#5) Aug 26, 2019
ws-demo-2.gif Spring cloud gateway websocket (#5) Aug 26, 2019

README.md

Spring Cloud Gateway Load Balanced Websocket Demo

This project represents a bare-bones example of how to set up a load balanced websocket server utilizing Spring Boot Starter Websocket, and Spring Cloud Gateway, as well as a Message Broker to facilitate messages between multiple server instances.

The project uses Eureka for service discovery, however it is possible to manually specify a hostname and port in RouteLocationConfiguration.java rather than the service name for routing HTTP and Websocket requests through the gateway. Specifying instances by host and port like this however may preclude you from being able to effectively loadbalance requests.

The provided message broker is RabbitMQ, however it's possible to swap out the RabbitMQ server for the provided ActiveMQ server.

Project Structure

  • eureka
    • Spring Cloud Netflix Eureka - provides service discovery for enabled clients. This allows our applications to find each other by their spring.application.name property and the port they are configured to run on. With this our services can make requests to discoverable services with http://my-service-name rather than needing to know the hostname and port of the service
  • gateway
    • Spring Cloud Gateway - provides routing and load balancing for HTTP and Websocket connections through a single host and port (http://localhost:8080 as configured), and uses service discovery to route requests by service name.
  • server
    • Our example application which is serving a websocket endpoint for our client to connect to. This application has two profiles configured, a default profile running on port 8081 and a second profile on port 8082.
  • example.html
    • Our example "client" to connect to our websocket server.

Requirements

  • Docker
  • Web browser

Running

Using Docker Compose:

During startup be patient as eureka attempts to resolve location and port, as well as health for each of the discovery enabled applications (gateway, server [app1], and server [app2]). This could take some time for the discovery server and clients to begin accepting requests.

I've added a timed method to respond to the same websocket topic which the client is subscribed to to simulate push notifications from the server, either in response to a system action or in response to another user's action.

Demonstration of the project running

Websocket demonstration

Demonstration with second user in another window

Websocket demonstration with second user

You can’t perform that action at this time.