0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · 강의

Eureka 및 Consul 연동

Eureka나 Consul과 같은 서비스 레지스트리에 등록하고 서비스를 검색하도록 Spring Cloud Gateway를 설정해 보세요.

Eureka 및 Consul 연동은(는) CoddyKit의 무료 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What is Service Discovery?

In a microservices architecture, services are often dynamic. They can scale up or down, and their network locations (IP addresses, ports) can change frequently.

Service discovery is a mechanism that helps applications and services find each other without needing to hardcode their network locations. It's like a dynamic phone book for your services!

Service Registries: Eureka & Consul

A service registry is a central server that maintains a list of available service instances and their network locations.

  • Netflix Eureka: A very popular choice, especially within the Spring Cloud ecosystem. Services register themselves with Eureka.
  • HashiCorp Consul: Another robust option, offering service discovery along with a distributed key-value store and health checks.

Both allow services to register themselves and discover others.

Gateway's Need for Discovery

Spring Cloud Gateway acts as the entry point to your microservices.

Instead of configuring routes with fixed URLs for each backend service, the Gateway can query a service registry:

  • "Where is the 'user-service' running right now?"
  • "Give me an available instance of 'product-service'."

This enables flexible, dynamic, and resilient routing, as the Gateway doesn't need to know service locations upfront.

Sample Service: Eureka Client Config

First, let's look at how a simple Spring Boot service is configured to register itself with a Eureka server.

We'll use a service named hello-service. Its application.yml will point to the Eureka server's default zone.

spring:
  application:
    name: hello-service
server:
  port: 8081
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

Enabling Eureka Client in Service

To make our hello-service actually register with Eureka, we need to add the @EnableEurekaClient annotation to its main application class.

This annotation activates the Eureka Discovery Client functionality.

package com.example.helloservice;

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;

@EnableEurekaClient
@SpringBootApplication
@RestController
public class HelloServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloServiceApplication.class, args);
    }

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

Gateway Configuration for Eureka

Now, let's configure our Spring Cloud Gateway to connect to the same Eureka server. The Gateway itself also acts as a Eureka client to discover other services.

Its application.yml will specify the Eureka server URL and its own application name.

server:
  port: 8080
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost
spring:
  application:
    name: api-gateway

Enabling Discovery in Gateway

Similar to the backend service, the Gateway's main application class needs an annotation to enable its discovery capabilities.

Use @EnableDiscoveryClient to tell Spring Boot to activate the discovery client for the Gateway.

package com.example.apigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }
}

Routing via Service ID

With discovery enabled, we can define Gateway routes using a service's registered ID instead of a fixed URL.

The lb:// prefix (for 'load balancer') tells the Gateway to use its discovery client to find instances of the specified service name (e.g., hello-service) and then route the request.

spring:
  cloud:
    gateway:
      routes:
        - id: hello_route
          uri: lb://hello-service
          predicates:
            - Path=/hello/**

The Request Flow

Let's trace a request to http://localhost:8080/hello:

  1. The Gateway (listening on port 8080) receives the request.
  2. It matches the /hello/** path predicate to the hello_route.
  3. The Gateway uses its Eureka client to look up an available instance of hello-service.
  4. Eureka returns the address of an instance (e.g., localhost:8081).
  5. The Gateway forwards the request to http://localhost:8081/hello.
  6. The hello-service processes the request and returns a response, which the Gateway then sends back to the client.

Consul: Another Option

While Eureka is widely used, Consul by HashiCorp is another powerful service discovery and configuration management tool.

To use Consul, you'd typically add the Spring Cloud Consul Discovery dependency and configure your Gateway/services to point to the Consul agent (usually running on port 8500).

The principle remains the same: the Gateway uses service IDs (e.g., lb://my-service) to dynamically discover and route to services registered in Consul.

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: api-gateway

Gateway Discovery Check

You've configured your Spring Cloud Gateway to use Eureka for service discovery, and you have a service named my-service registered in Eureka.

Which uri configuration would correctly route requests to my-service via discovery?

Lesson Summary

In this lesson, you learned about:

  • The importance of service discovery in dynamic microservice environments.
  • How Eureka and Consul serve as central service registries.
  • Configuring both backend services and Spring Cloud Gateway as discovery clients.
  • Defining Gateway routes using service IDs (e.g., lb://service-name) for dynamic routing.

This integration is fundamental for building scalable and resilient microservice architectures. Next, we'll explore more dynamic routing rules and predicates.

자주 묻는 질문

“Eureka 및 Consul 연동” 강의는 무료인가요?

네 — “Eureka 및 Consul 연동” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의 전체를 잠금 해제할 수 있습니다. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의에는 총 4개의 강의가 포함되어 있습니다.

“Eureka 및 Consul 연동”에서 뭘 배우나요?

Eureka나 Consul과 같은 서비스 레지스트리에 등록하고 서비스를 검색하도록 Spring Cloud Gateway를 설정해 보세요. 브라우저에서 직접 실행하는 실습 코드로 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“Eureka 및 Consul 연동” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Eureka 및 Consul 연동
  2. 서비스 검색을 사용한 동적 라우팅
  3. Spring Cloud LoadBalancer를 사용한 부하 분산
  4. lb:// URI 및 검색 로케이터
← API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)(으)로 돌아가기