0Pricing
Spring Boot 4 Complete Guide · บทเรียน

การค้นหาและลงทะเบียนบริการ

นำการลงทะเบียนและการค้นหาบริการมาใช้โดยใช้ Eureka หรือ Consul เพื่อค้นหาตำแหน่งบริการแบบไดนามิก

การค้นหาและลงทะเบียนบริการ เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Complete Guide และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What is Service Discovery?

In microservices, applications are broken into many small, independent services. How do these services find each other to communicate? This is where Service Discovery comes in.

It's a key pattern that helps services locate each other automatically. Without it, managing service addresses manually would be a nightmare!

Why Manual Addresses Fail

Imagine you have a 'Product Service' and an 'Order Service'. If the Order Service needs to call the Product Service, it needs its network location (IP address and port).

  • Manual Configuration: Hardcoding addresses means constant updates if a service moves or scales.
  • Scaling Issues: When you add more instances of a service, how do others know about them?
  • Resilience: What if a service instance fails? Others need to know to avoid it.

Service discovery solves these problems dynamically.

Enter the Service Registry

A Service Registry is like a phone book for your microservices. It's a central database that holds the network locations of all running service instances.

When a service starts up, it registers itself with the registry. When another service needs to communicate, it queries the registry to find an available instance.

Popular service registries include Netflix Eureka, Consul, and Apache ZooKeeper.

How Service Registration Works

Services can register themselves in two main ways:

  • Client-Side Registration: The service itself registers and deregisters with the registry. This is common with Eureka.
  • Server-Side Registration: A third-party component (like a load balancer or a container orchestrator) registers services on their behalf.

Once registered, services also send periodic 'heartbeats' to the registry to confirm they are still alive and healthy.

Meet Netflix Eureka

Netflix Eureka is a popular service registry developed by Netflix. It's a simple, robust, and highly available registry for microservices.

Eureka consists of two main parts:

  • Eureka Server: The central registry that services register with.
  • Eureka Client: A library embedded in microservices to interact with the server (register, discover).

It's designed for resilience, prioritizing availability over consistency (AP in CAP theorem).

Building a Eureka Server

Let's create a basic Eureka Server. First, add the spring-cloud-starter-netflix-eureka-server dependency. Then, use @EnableEurekaServer on your main application class.

package com.coddykit.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Configuring Eureka Server

For our Eureka server to run correctly, we need to configure its port and tell it not to register itself (as it's the server). Add these lines to application.properties:

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

server.port=8761 is the default port for Eureka. The other two properties prevent the server from trying to register with itself, which isn't necessary.

Making a Service Discoverable

Now, let's make a microservice register itself with our Eureka Server. This service becomes a Eureka Client.

First, add the spring-cloud-starter-netflix-eureka-client dependency to your service's project. Then, annotate your main application class with @EnableEurekaClient.

This tells Spring Boot to enable Eureka client features for this application.

Configuring a Eureka Client

Here's how a simple microservice looks with Eureka Client enabled. Notice the @EnableEurekaClient. In its application.properties, you'd define its name and the Eureka server URL:

spring.application.name=product-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
package com.coddykit.productservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ProductServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServiceApplication.class, args);
    }

    @GetMapping("/products")
    public String getProducts() {
        return "List of products from Product Service";
    }
}

Discovering Services with Eureka

Once services are registered, other clients can discover them. Spring Cloud provides DiscoveryClient and RestTemplate (with @LoadBalanced) or WebClient to achieve this.

The client doesn't need to know the exact IP/port. It just asks Eureka for a service by its registered name (e.g., "product-service"). Eureka returns available instances, and a client-side load balancer picks one.

Check Your Understanding

You've learned about setting up a Eureka Server and registering a client. Which annotation is essential for turning a Spring Boot application into a Eureka client?

Recap: Service Discovery

We've explored Service Discovery, a vital pattern for microservices. We learned:

  • Why a central Service Registry is needed to avoid hardcoding addresses.
  • How Netflix Eureka acts as a registry with its Server and Client components.
  • How to set up a Eureka Server using @EnableEurekaServer.
  • How to register a microservice as a Eureka Client using @EnableEurekaClient and configure it.

Next, you'll learn about API Gateways, which often use service discovery to route requests.

คำถามที่พบบ่อย

บทเรียน “การค้นหาและลงทะเบียนบริการ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การค้นหาและลงทะเบียนบริการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การค้นหาและลงทะเบียนบริการ”

นำการลงทะเบียนและการค้นหาบริการมาใช้โดยใช้ Eureka หรือ Consul เพื่อค้นหาตำแหน่งบริการแบบไดนามิก คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Complete Guide หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Complete Guide บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การค้นหาและลงทะเบียนบริการ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Complete Guide นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Complete Guide ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. หลักการและการออกแบบไมโครเซอร์วิส
  2. การค้นหาและลงทะเบียนบริการ
  3. เกตเวย์ API ด้วย Spring Cloud Gateway
  4. การกำหนดค่ารวมศูนย์ด้วย Spring Cloud Config
← กลับไปที่ Spring Boot 4 Complete Guide