0Pricing
Spring Boot 4 Microservices & REST APIs · درس

تنفيذ اكتشاف الخدمات باستخدام Eureka

هيّئ Netflix Eureka واستخدمه لتسجيل الخدمات واكتشافها ديناميكيًا.

تنفيذ اكتشاف الخدمات باستخدام Eureka درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 1 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 3 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Service Discovery?

In a microservices architecture, services often need to communicate with each other. But how does one service find another?

  • Dynamic IPs: Service instances can scale up/down, getting new IP addresses.
  • Load Balancing: Multiple instances of a service need to be discovered.
  • Manual Configuration: Hardcoding addresses is fragile and not scalable.

Service discovery solves this by providing a central registry where services register themselves.

Introducing Netflix Eureka

Netflix Eureka is a powerful solution for service discovery in microservices. It's a REST-based service that is primarily used in the AWS cloud for locating services to achieve load balancing and failover of middle-tier services.

Eureka consists of two main components:

  • Eureka Server: The central registry that holds information about all registered services.
  • Eureka Client: A component embedded in each microservice that registers with the Eureka Server.

Setting Up Eureka Server: Dependencies

First, let's set up our Eureka Server. You'll need a new Spring Boot project. Add the following dependency to your pom.xml (for Maven) or build.gradle (for Gradle):

Maven:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

This starter brings in all necessary components for running a Eureka discovery server.

Configuring Your Eureka Server

Next, configure your Eureka Server in src/main/resources/application.yml. We need to tell the server NOT to register itself with Eureka, as it is the server!

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  instance:
    hostname: localhost
  • server.port: 8761 is the default port for Eureka.
  • register-with-eureka: false prevents the server from registering as a client.
  • fetch-registry: false prevents the server from trying to fetch the registry.

Eureka Server Application Code

Finally, enable the Eureka Server functionality by adding @EnableEurekaServer to your main Spring Boot application class:

Try running this example:

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);
  }
}

Accessing the Eureka Dashboard

After running your EurekaServerApplication, you can open your web browser and navigate to http://localhost:8761.

You should see the Eureka dashboard. Initially, it will show no registered service instances. This dashboard is a great way to monitor which services are currently active and registered with your Eureka Server.

Registering Services: The Eureka Client

Now that our Eureka Server is running, let's register a microservice with it. This microservice will act as a Eureka Client.

When a client service starts up, it registers itself with the Eureka Server, providing its network location. Other services can then query the Eureka Server to find the client's location.

Setting Up Eureka Client: Dependencies

Create another Spring Boot project for your client service (e.g., a 'Hello Service'). Add the following dependency to its pom.xml or build.gradle:

Maven:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

This dependency provides the necessary tools for your service to act as a Eureka client and register with the server.

Configuring Your Eureka Client

Configure your client service in its application.yml. It needs to know where the Eureka Server is located:

server:
  port: 8081 # Or any other unique port

spring:
  application:
    name: my-hello-service # Unique service name

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  • spring.application.name is how your service will be identified in Eureka.
  • defaultZone points to your Eureka Server's URL.

Eureka Client Application Code

To enable the client service to register with Eureka, add the @EnableDiscoveryClient annotation to its main class. We'll also add a simple REST endpoint.

Try running this example (ensure Eureka Server is running first!):

package com.coddykit.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

  @GetMapping("/hello")
  public String hello() {
    return "Hello from Eureka Client!";
  }
}

Quick Check

Which annotation should you use on a Spring Boot application's main class to enable it to register as a client with a Eureka Server?

Recap: Eureka Service Discovery

You've taken a big step into microservices by understanding and implementing Eureka Service Discovery!

  • Service Discovery solves the problem of services finding each other dynamically.
  • Eureka Server acts as the central registry.
  • Eureka Clients (your microservices) register with the server.
  • Use @EnableEurekaServer for the server.
  • Use @EnableDiscoveryClient for client services.

This setup allows your microservices to communicate without hardcoding addresses, making your architecture more resilient and scalable.

الأسئلة الشائعة

هل درس «تنفيذ اكتشاف الخدمات باستخدام Eureka» مجاني؟

نعم — نص درس «تنفيذ اكتشاف الخدمات باستخدام Eureka» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 3 دروس في المجموع.

ماذا ستتعلم في «تنفيذ اكتشاف الخدمات باستخدام Eureka»؟

هيّئ Netflix Eureka واستخدمه لتسجيل الخدمات واكتشافها ديناميكيًا. تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 3.

كم من الوقت يستغرق درس «تنفيذ اكتشاف الخدمات باستخدام Eureka»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تنفيذ اكتشاف الخدمات باستخدام Eureka
  2. موازنة التحميل من جانب العميل باستخدام Ribbon
  3. بوابة API باستخدام Spring Cloud Gateway
← العودة إلى Spring Boot 4 Microservices & REST APIs