การกระจายโหลดฝั่งไคลเอ็นต์ด้วย Ribbon
ผสาน Ribbon เพื่อกระจายโหลดฝั่งไคลเอ็นต์ระหว่างอินสแตนซ์บริการหลายรายการ
การกระจายโหลดฝั่งไคลเอ็นต์ด้วย Ribbon เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 3 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Load Balancing?
In a microservices world, applications often have multiple copies (instances) of the same service running. This is for scalability and high availability.
Load balancing is the process of distributing incoming network traffic across these multiple instances. It ensures no single service instance gets overwhelmed.
- Scalability: Handle more users by adding more instances.
- High Availability: If one instance fails, others can take over.
- Performance: Distribute work evenly to reduce response times.
Client-Side vs. Server-Side LB
There are two main types of load balancing:
- Server-Side Load Balancing: A dedicated component (like Nginx or a hardware load balancer) sits in front of your services. Clients send requests to this component, and it forwards them to an appropriate service instance.
- Client-Side Load Balancing: The client application itself is responsible for knowing about all available service instances and choosing one for each request. This is where tools like Ribbon come in!
Ribbon: Your Client-Side Helper
Ribbon is a client-side load balancer that integrates seamlessly with Spring Cloud applications. It helps your client service pick the right instance of a target service.
How does Ribbon know about service instances? It works hand-in-hand with a service discovery mechanism, like Eureka. Eureka tells Ribbon where services are, and Ribbon decides which one to call.
Adding Load Balancing Dependency
To enable client-side load balancing in your Spring Boot application, you typically include the Spring Cloud LoadBalancer dependency. While 'Ribbon' is in the lesson title, Spring Cloud LoadBalancer is its modern successor and works similarly.
Add this to your pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>Load Balanced RestTemplate
The easiest way to use client-side load balancing is with Spring's RestTemplate. By simply adding the @LoadBalanced annotation to your RestTemplate bean, Spring configures it to use a load balancer.
When you make an HTTP call, you'll use the logical service name (e.g., my-service) instead of a specific IP address and port. The load balancer (Ribbon/Spring Cloud LoadBalancer) resolves this name to a real instance.
Client Calling a Service (Code)
Here's a simple Spring Boot client application that uses a load-balanced RestTemplate to call another service named my-service. For this to work, my-service must be registered with a service discovery server (like Eureka).
package com.coddykit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@RestController
public class MyClientApplication {
private final RestTemplate restTemplate;
// Inject the load-balanced RestTemplate
public MyClientApplication(@LoadBalanced RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public static void main(String[] args) {
SpringApplication.run(MyClientApplication.class, args);
}
// Define the load-balanced RestTemplate bean
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
// Endpoint to call 'my-service'
@GetMapping("/call-my-service")
public String callMyService() {
// Use the logical service ID 'my-service'
return restTemplate.getForObject("http://my-service/hello", String.class);
}
}Load Balancing Algorithms
Ribbon (and Spring Cloud LoadBalancer) can use different algorithms to decide which service instance to call. Some common ones include:
- Round Robin: Distributes requests sequentially to each instance (e.g., instance 1, then 2, then 3, then 1 again).
- Random: Picks a random instance for each request.
- Availability Filtering: Ignores instances that are marked as down or unresponsive.
- Weighted Response Time: Favors instances that have historically responded faster.
Configuring Load Balancing Rules
You can configure the load balancing rule for a specific service in your application.yml or application.properties. For example, to use a Round Robin rule for my-service (using older Ribbon config style):
my-service.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
While this specific syntax is for older Ribbon, the concept of configuring rules per service is similar in Spring Cloud LoadBalancer, often done via custom configurations or properties.
Benefits of Client-Side LB
Client-side load balancing offers several advantages in a microservices architecture:
- Simpler Infrastructure: No need for a separate hardware or software load balancer component.
- Direct Communication: Clients communicate directly with service instances, potentially reducing latency.
- Dynamic Scaling: Easily adapts to new service instances registering or deregistering with service discovery.
- Granular Control: The client can implement custom load balancing logic or apply different rules per service.
Quick Check on Load Balancing
You've learned about client-side load balancing. Let's test your understanding.
Recap: Client-Side Load Balancing
Great job! You've explored client-side load balancing with Ribbon (and its successor, Spring Cloud LoadBalancer).
- We learned that load balancing distributes traffic across service instances for scalability and availability.
- Client-side load balancing means the client itself chooses which instance to call, often using a logical service name.
- The
@LoadBalancedannotation on aRestTemplatebean is key for enabling this feature in Spring Boot. - Various algorithms like Round Robin can be used to distribute requests.
This approach makes your microservices more robust and efficient!
เรียนรู้ Java ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 24
- บทเรียน
- 93
คำถามที่พบบ่อย
บทเรียน “การกระจายโหลดฝั่งไคลเอ็นต์ด้วย Ribbon” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกระจายโหลดฝั่งไคลเอ็นต์ด้วย Ribbon” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 3 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกระจายโหลดฝั่งไคลเอ็นต์ด้วย Ribbon”
ผสาน Ribbon เพื่อกระจายโหลดฝั่งไคลเอ็นต์ระหว่างอินสแตนซ์บริการหลายรายการ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน
บทเรียน “การกระจายโหลดฝั่งไคลเอ็นต์ด้วย Ribbon” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การใช้ Eureka ค้นหาบริการ
- การกระจายโหลดฝั่งไคลเอ็นต์ด้วย Ribbon
- API Gateway ด้วย Spring Cloud Gateway