헤르메스 LIFE

[Spring Cloud Netflix] Eureka Service 샘플 본문

Spring Framework

[Spring Cloud Netflix] Eureka Service 샘플

헤르메스의날개 2024. 3. 27. 22:03
728x90

아래와 같은 환경을 구축하기 위해 Microservice 가 1개 이상 있어야 합니다.

Spring Cloud 아키텍처 관계도

출처 : https://spring.io/cloud


개발환경

JDK : Zulu JDK 17.0.10

SpringBoot 3.2.3

SpringBoot Eureka Client ( 현재 최신 버전 :4.1.0 )

Build Tools : Gradle


1. 프로젝트 생성

2. Service #01

- 간단한 Application 입니다.

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.4'
    id 'io.spring.dependency-management' version '1.1.4'
}

group = 'octopus'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2023.0.0")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

application.yml

server:
  port: 8081

spring:
  application:
    name: Eureka-Service-01

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${random.value}}
    prefer-ip-address: true

ServiceController.java

package octopus.service;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collections;
import java.util.Enumeration;

@RestController
@RequestMapping("/service01")
@Slf4j
public class ServiceController {
    Environment env;

    @Autowired
    public ServiceController(Environment env) {
        this.env = env;
    }

    @GetMapping("/welcome")
    public String welcome() {
        return "Service #01 입니다.";
    }

    @GetMapping("/message")
    public String message(@RequestHeader("first-request") String header) {
        log.info(header);
        return "Service #01 입니다.";
    }

    @GetMapping("/check")
    public String check(HttpServletRequest request) {
        Enumeration<String> headers = request.getHeaderNames();
        Collections.list(headers).stream().forEach(name -> {
            Enumeration<String> values = request.getHeaders(name);
            Collections.list(values).stream().forEach(value -> System.out.println(name + "=" + value));
        });

        log.info("Server port={}", request.getServerPort());

        log.info("spring.cloud.client.hostname={}", env.getProperty("spring.cloud.client.hostname"));
        log.info("spring.cloud.client.ip-address={}", env.getProperty("spring.cloud.client.ip-address"));

        return String.format("Service #01 입니다. PORT %s"
                , env.getProperty("local.server.port"));
    }
}

http://localhost:8081/service01/welcome


3. Service #02

- Service #01 을 참조해서 똑같이 생성 합니다.

application.yml

server:
  port: 8082

spring:
  application:
    name: Eureka-Service-02

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${random.value}}
    prefer-ip-address: true

ServiceController.java

package octopus.service;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collections;
import java.util.Enumeration;

@RestController
@RequestMapping("/service02")
@Slf4j
public class ServiceController {
    Environment env;

    @Autowired
    public ServiceController(Environment env) {
        this.env = env;
    }

    @GetMapping("/welcome")
    public String welcome() {
        return "Service #02 입니다.";
    }

    @GetMapping("/message")
    public String message(@RequestHeader("first-request") String header) {
        log.info(header);
        return "Service #02 입니다.";
    }

    @GetMapping("/check")
    public String check(HttpServletRequest request) {
        Enumeration<String> headers = request.getHeaderNames();
        Collections.list(headers).stream().forEach(name -> {
            Enumeration<String> values = request.getHeaders(name);
            Collections.list(values).stream().forEach(value -> System.out.println(name + "=" + value));
        });

        log.info("Server port={}", request.getServerPort());

        log.info("spring.cloud.client.hostname={}", env.getProperty("spring.cloud.client.hostname"));
        log.info("spring.cloud.client.ip-address={}", env.getProperty("spring.cloud.client.ip-address"));

        return String.format("Service #01 입니다. PORT %s"
                , env.getProperty("local.server.port"));
    }
}

http://localhost:8082/service02/welcome


Eureka Server 에 접속됐습니다.


https://hermeslog.tistory.com/755

 

[Spring Cloud Netflix] Eureka Server 개발

https://github.com/hermeswing/EureakServer GitHub - hermeswing/EureakServer: Spring Eureka Server Spring Eureka Server. Contribute to hermeswing/EureakServer development by creating an account on GitHub. github.com 개발환경 JDK : Zulu JDK 17.0.10 Sprin

hermeslog.tistory.com

https://hermeslog.tistory.com/761

 

[Exception] com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

개발환경 JDK : Zulu JDK 17.0.10 SpringBoot 3.2.3 SpringBoot Eureka Client ( 현재 최신 버전 :4.1.0 ) Build Tools : Gradle 다음과 같은 오류가 발생하였습니다. com.netflix.discovery.shared.transport.TransportException: Cannot execute r

hermeslog.tistory.com

 

728x90