250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Spring Boot
- JDBC
- SpringBoot
- Open Source
- error
- MSSQL
- maven
- oracle
- AJAX
- Docker
- git
- Tomcat
- Thymeleaf
- Core Java
- Exception
- 문서
- myBatis
- Source
- MySQL
- 오픈소스
- Python
- PostgreSQL
- ubuntu
- JavaScript
- Eclipse
- IntelliJ
- STS
- jpa
- 설정
- spring
Archives
- Today
- Total
헤르메스 LIFE
[Spring Cloud Netflix] Eureka Service 샘플 본문
728x90
아래와 같은 환경을 구축하기 위해 Microservice 가 1개 이상 있어야 합니다.
Spring 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
https://hermeslog.tistory.com/761
728x90
'Spring Framework' 카테고리의 다른 글
[SpringBoot] MySQL/Maria 연결하기 (0) | 2024.08.21 |
---|---|
[Spring & Spring Boot] 스프링 JDK 버전 호환 (2) | 2024.05.11 |
[Log] Log4Jdbc 설정 (0) | 2024.02.23 |
[SpringBoot] Docker에 PostgreSQL 설치 / 연결하기 (2) | 2024.02.04 |
[Redis] Docker에 Redis 설치하기 + 비밀번호 (0) | 2024.01.30 |