यह प्रोजेक्ट Server-Sent Events (SSE) का उपयोग करके Spring Boot WebFlux के साथ HTTP स्ट्रीमिंग दिखाता है। इसमें दो एप्लिकेशन शामिल हैं:
- Calculator Server: एक रिएक्टिव वेब सेवा जो कैलकुलेशन करती है और SSE के माध्यम से परिणाम स्ट्रीम करती है
- Calculator Client: एक क्लाइंट एप्लिकेशन जो स्ट्रीमिंग एंडपॉइंट का उपयोग करता है
- Java 17 या उससे ऊपर
- Maven 3.6 या उससे ऊपर
java/
├── calculator-server/ # Spring Boot server with SSE endpoint
│ ├── src/main/java/com/example/calculatorserver/
│ │ ├── CalculatorServerApplication.java
│ │ └── CalculatorController.java
│ └── pom.xml
├── calculator-client/ # Spring Boot client application
│ ├── src/main/java/com/example/calculatorclient/
│ │ └── CalculatorClientApplication.java
│ └── pom.xml
└── README.md
-
Calculator Server एक
/calculateendpoint that:- Accepts query parameters:
a(number),b(number),op(operation) - Supported operations:
add,sub,mul,div - Returns Server-Sent Events with calculation progress and result
- Accepts query parameters:
-
The Calculator Client connects to the server and:
- Makes a request to calculate
7 * 5एंडपॉइंट प्रदान करता है - स्ट्रीमिंग प्रतिक्रिया को उपयोग करता है
- प्रत्येक इवेंट को कंसोल पर प्रिंट करता है
- Makes a request to calculate
टर्मिनल खोलें और सर्वर डायरेक्टरी में जाएं:
cd calculator-server
mvn clean package
mvn spring-boot:runसर्वर http://localhost:8080 पर शुरू हो जाएगा
आपको इस तरह का आउटपुट दिखाई देगा:
Started CalculatorServerApplication in X.XXX seconds
Netty started on port 8080 (http)
एक नया टर्मिनल खोलें और क्लाइंट डायरेक्टरी में जाएं:
cd calculator-client
mvn clean package
mvn spring-boot:runक्लाइंट सर्वर से कनेक्ट होगा, कैलकुलेशन करेगा, और स्ट्रीमिंग परिणाम दिखाएगा।
cd calculator-server
mvn clean package
java -jar target/calculator-server-0.0.1-SNAPSHOT.jarcd calculator-client
mvn clean package
java -jar target/calculator-client-0.0.1-SNAPSHOT.jarआप वेब ब्राउज़र या curl का उपयोग करके भी सर्वर का परीक्षण कर सकते हैं:
यहाँ जाएं: http://localhost:8080/calculate?a=10&b=5&op=add
curl "http://localhost:8080/calculate?a=10&b=5&op=add" -H "Accept: text/event-stream"जब क्लाइंट चलाया जाता है, तो आपको स्ट्रीमिंग आउटपुट कुछ इस तरह दिखेगा:
event:info
data:Calculating: 7.0 mul 5.0
event:result
data:35.0
add- Addition (a + b)sub- Subtraction (a - b)mul- Multiplication (a * b)div- Division (a / b, returns NaN if b = 0)
Parameters:
a(required): First number (double)b(required): Second number (double)op(required): Operation (add,sub,mul,div)
Response:
- Content-Type:
text/event-stream - कैलकुलेशन प्रगति और परिणाम के साथ Server-Sent Events लौटाता है
उदाहरण अनुरोध:
GET /calculate?a=7&b=5&op=mul HTTP/1.1
Host: localhost:8080
Accept: text/event-stream
उदाहरण प्रतिक्रिया:
event: info
data: Calculating: 7.0 mul 5.0
event: result
data: 35.0
-
पोर्ट 8080 पहले से उपयोग में है
- पोर्ट 8080 का उपयोग कर रहे किसी अन्य एप्लिकेशन को बंद करें
- या
calculator-server/src/main/resources/application.yml
-
Connection refused
- Make sure the server is running before starting the client
- Check that the server started successfully on port 8080
-
Parameter name issues
- This project includes Maven compiler configuration with
-parametersflag - If you encounter parameter binding issues, ensure the project is built with this configuration
- This project includes Maven compiler configuration with
- Press
Ctrl+Cin the terminal where each application is running - Or use
mvn spring-boot:stopमें सर्वर पोर्ट बदलें यदि बैकग्राउंड प्रक्रिया के रूप में चला रहे हैं
- Spring Boot 3.3.1 - एप्लिकेशन फ्रेमवर्क
- Spring WebFlux - रिएक्टिव वेब फ्रेमवर्क
- Project Reactor - रिएक्टिव स्ट्रीम लाइब्रेरी
- Netty - नॉन-ब्लॉकिंग I/O सर्वर
- Maven - बिल्ड टूल
- Java 17+ - प्रोग्रामिंग भाषा
कोड को इस तरह बदलने की कोशिश करें:
- और गणितीय ऑपरेशन जोड़ें
- अमान्य ऑपरेशनों के लिए त्रुटि हैंडलिंग शामिल करें
- अनुरोध/प्रतिक्रिया लॉगिंग जोड़ें
- प्रमाणीकरण लागू करें
- यूनिट टेस्ट जोड़ें
अस्वीकरण:
यह दस्तावेज़ AI अनुवाद सेवा Co-op Translator का उपयोग करके अनुवादित किया गया है। हम सटीकता के लिए प्रयासरत हैं, लेकिन कृपया ध्यान दें कि स्वचालित अनुवादों में त्रुटियाँ या असंगतियाँ हो सकती हैं। मूल दस्तावेज़ अपनी मूल भाषा में अधिकारिक स्रोत माना जाना चाहिए। महत्वपूर्ण जानकारी के लिए, पेशेवर मानव अनुवाद की सलाह दी जाती है। इस अनुवाद के उपयोग से उत्पन्न किसी भी गलतफहमी या गलत व्याख्या के लिए हम जिम्मेदार नहीं हैं।