This project is a microservices-based trading system composed of several services that communicate through NATS. The architecture includes:
- Gateway Service: An API gateway that accepts client order requests and interacts with NATS and WebSocket clients.
- Order Service: Publishes order creation events to NATS.
- Trade Stream Service: Listens for order completion events and provides real-time updates via WebSocket.
- Broker Service: Processes, logs, and acknowledges order completion events.
Before getting started, ensure you have the following installed:
- Go 1.20 or higher
- Docker (to run the NATS server)
Make sure a NATS server instance is running before you start the services.
Clone the repository to your local machine:
git clone <repository-url>
cd <repository-directory>
Navigate to each service directory and initialize the Go modules:
cd gateway
go mod tidy
cd ../order-service
go mod tidy
cd ../trade-stream-service
go mod tidy
cd ../broker-service
go mod tidy
Run a NATS server using Docker. Use the following command:
docker run -d -p 4222:4222 -p 6222:6222 -p 8222:8222 --name nats-server nats:latest
This command starts the NATS server and maps the standard ports.
The Gateway Service serves as the entry point, accepting HTTP requests and interacting with NATS and WebSocket clients.
To run the Gateway Service:
cd gateway
go run main.go
Endpoints:
POST /order
: Accepts order requests, which are then published to NATS.
The Order Service listens for order creation events, processes them, and publishes order completion events to NATS.
To run the Order Service:
cd order-service
go run main.go
NATS Subscription:
- Subscribes to:
order.created
- Publishes to:
order.completed
The Trade Stream Service listens for order completion events and broadcasts them to connected WebSocket clients.
To run the Trade Stream Service:
cd trade-stream-service
go run main.go
WebSocket URL:
- WebSocket URL:
ws://localhost:8081/ws
The Broker Service processes and logs order completion events received from NATS.
To run the Broker Service:
cd broker-service
go run main.go
NATS Subscription:
- Subscribes to:
order.completed
You can also use Docker Compose to manage and run all the services together with the NATS server. To do so:
- Ensure Docker is running.
- Navigate to the root of the project directory where the
docker-compose.yml
file is located. - Run the following command:
docker-compose up --build
This will build and start all the services along with the NATS server.
- Gateway Service API:
http://localhost:8080/order
- Trade Stream WebSocket:
ws://localhost:8081/ws
- NATS Dashboard (if enabled):
http://localhost:8222