Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation


Netty

Netty Client

Scalable TCP client application built with Spring Boot & Netty

Spring Boot 2.5.2 Netty 4.1.65.Final Java 11 License MIT PRs Welcome


📋 Table of Contents


📖 About

Netty Client is a lightweight, non-blocking TCP client application that demonstrates how to build high-performance network clients using the Netty framework within a Spring Boot environment.

The client connects to a remote TCP server, sends structured RequestData payloads, and processes ResponseData replies through a custom Netty pipeline — all with automatic connection retry logic for resilience.

✨ Features

  • Non-blocking I/O — leverages Netty's event-driven architecture
  • 🔄 Automatic retry — gracefully waits for the server with increasing delays (up to 10 attempts)
  • 🧩 Custom pipeline — dedicated encoder/decoder for protocol handling
  • 📦 Spring Boot integration — runs as a managed component with devtools hot-reload
  • 🛡️ Graceful shutdown — proper resource cleanup via Netty's EventLoopGroup

🏗️ Architecture

┌─────────────────────────────────────────────┐
│              Netty Client App                │
│                                             │
│  ┌──────────────┐    ┌──────────────────┐   │
│  │  Spring Boot  │    │    Netty Core     │   │
│  │ (Application) │───▶│                  │   │
│  └──────────────┘    │  ┌────────────┐   │   │
│                      │  │  Bootstrap  │   │   │
│                      │  └──────┬─────┘   │   │
│                      │         │         │   │
│                      │  ┌──────▼──────┐  │   │
│                      │  │   Channel    │  │   │
│                      │  │  Pipeline    │  │   │
│                      │  │              │  │   │
│                      │  │ ┌──────────┐ │  │   │
│                      │  │ │RequestData│ │  │   │
│                      │  │ │ Decoder   │ │  │   │
│                      │  │ ├──────────┤ │  │   │
│                      │  │ │ResponseData│ │  │   │
│                      │  │ │ Encoder   │ │  │   │
│                      │  │ ├──────────┤ │  │   │
│                      │  │ │  Client   │ │  │   │
│                      │  │ │  Handler  │ │  │   │
│                      │  │ └──────────┘ │  │   │
│                      │  └──────────────┘  │   │
│                      └──────────────────┘   │
└─────────────────────┬───────────────────────┘
                      │
                      │ TCP (port 5002)
                      ▼
              ┌──────────────────┐
              │    TCP Server     │
              │  (localhost:5002) │
              └──────────────────┘

Data Flow

  1. Startup — Spring Boot launches NettyClient via CommandLineRunner
  2. Connection — attempts to connect to localhost:5002 with retry logic
  3. Handshake — on successful connection, ClientHandler.channelActive() fires
  4. SendRequestData (int + string) is written and flushed through the pipeline
  5. Encode/Decode — custom ResponseDataEncoder and RequestDataDecoder transform bytes ↔ objects
  6. Response — server reply is printed to console, connection closes

📂 Project Structure

src/
├── main/
│   └── java/com/batsandrey/demo/
│       ├── DemoApplication.java          # Spring Boot entry point
│       ├── NettyClient.java              # Netty client with retry logic
│       ├── decoder/
│       │   └── RequestDataDecoder.java   # Inbound byte → RequestData
│       ├── encoder/
│       │   └── ResponseDataEncoder.java  # Outbound ResponseData → byte
│       ├── entity/
│       │   ├── request/
│       │   │   └── RequestData.java      # Request model (int + string)
│       │   └── response/
│       │       └── ResponseData.java     # Response model (int value)
│       └── handler/
│           └── ClientHandler.java        # Channel event handler
└── test/
    └── java/com/batsandrey/demo/
        └── DemoApplicationTests.java     # Spring context test

✅ Prerequisites

Tool Version
☕ Java 11+
🔨 Maven 3.6+

🚀 Getting Started

1️⃣ Clone

git clone https://github.com/andriibats/client-netty.git
cd client-netty

2️⃣ Build

./mvnw clean package

If mvnw is not available, use:

mvn clean package

3️⃣ Run

java -jar target/demo-0.0.1-SNAPSHOT.jar

Or directly with Maven:

./mvnw spring-boot:run

Note: The client will attempt to connect to localhost:5002. If no server is running, it will retry up to 10 times with increasing delays (2s → 4s → 6s → ...) and log a warning instead of crashing.


🎯 Usage

The client is designed to work with a TCP server listening on port 5002. Once connected, it automatically:

  1. Sends a RequestData object:
    • intValue: 123
    • stringValue: "all work and no play makes jack a dull boy"
  2. Waits for a ResponseData reply from the server
  3. Prints the response to standard output
  4. Closes the connection

Example Log Output

WARN  [main] NettyClient: Connection to localhost:5002 failed (attempt 1/10). Retrying in 2000ms...
WARN  [main] NettyClient: Connection to localhost:5002 failed (attempt 2/10). Retrying in 4000ms...
INFO  [main] NettyClient: Connected to localhost:5002 on attempt 3
ResponseData(intValue=456)

⚙️ Configuration

Key constants are defined in NettyClient.java:

Parameter Default Description
HOST localhost Target server hostname
PORT 5002 Target server port
MAX_RETRIES 10 Maximum connection attempts
BASE_DELAY_MS 2000 Initial retry delay (× attempt number)

💡 Tip: For production use, consider externalizing these values to application.yml.


🛠️ Built With

Library Version Purpose
Spring Boot 2.5.2 Application framework & dependency injection
Netty 4.1.65.Final Non-blocking network I/O framework
Lombok 1.18.30 Boilerplate code reduction
Maven Build & dependency management

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. 🍴 Fork the project
  2. 🌿 Create your feature branch (git checkout -b feature/amazing-feature)
  3. 💾 Commit your changes (git commit -m 'Add some amazing feature')
  4. 📤 Push to the branch (git push origin feature/amazing-feature)
  5. 🔀 Open a Pull Request

📄 License

This project is licensed under the MIT License — see the LICENSE file for details.


Made with ❤️ by andriibats

About

Simple Netty client

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages