plugins {
id 'java'
id 'org.springframework.boot' version '2.7.12'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'apiGateway'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.4")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
// spring cloud gateway
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
// spring cloud eureka
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
dependencyManagement {
imports {
// spring cloud version
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
tasks.named('test') {
useJUnitPlatform()
}
server:
port: 8000
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: ApiGateway-service
cloud:
gateway:
routes:
- uri: lb://user-service
predicates:
- Path=/user-service/**
- uri: lb://mate-service
predicates:
- Path=/mate-service/**
- uri: lb://product-service
predicates:
- Path=/product-service/**
- uri: lb://stock-service
predicates:
- Path=/stock-service/**
- uri: lb://order-service
predicates:
- Path=/order-service/**
register-with-eureka: true
→ 이 서비스가 Eureka 서버에 자신을 등록하도록 한다.fetch-registry: true
→ 이 서비스가 Eureka 서버에 등록된 모든 서비스의 정보를 가져와서 캐싱하도록 한다. 이를 통해 이 서비스는 다른 서비스를 찾을 수 있게 된다.service-url: defaultZone: http://localhost:8761/eureka
→ Eureka 서버의 URL을 지정한다. 이 서비스는 이 URL을 통해 Eureka 서버에 접속하게 됩니다.cloud.gateway.routes
→ Spring Cloud Gateway에서 사용하는 라우트 설정을 정의합니다. 각 라우트는 uri와 predicates를 가집니다.uri: lb://*-service
→ 라우트의 대상 URI를 지정합니다. 여기서 lb는 로드 밸런싱을 의미하며 요청이 로드 밸런싱 되어 일정하게 통신을 받게 된다.predicates: - Path=/*-service/**
→ 라우트가 적용될 조건을 뜻한다.plugins {
id 'java'
id 'org.springframework.boot' version '2.7.12'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'eureka'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.4")
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
// spring cloud eureka
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
dependencyManagement {
imports {
// spring cloud version
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
tasks.named('test') {
useJUnitPlatform()
}
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'user'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '11'
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2021.0.4")
}
dependencies {
// common module
implementation project(path: ':common')
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
testImplementation 'junit:junit:4.13.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
// Junit5
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
// Gson
implementation 'com.google.code.gson:gson:2.9.0'
// netflix-eureka
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:3.1.0'
// kafka
implementation 'org.springframework.kafka:spring-kafka'
}
test {
useJUnitPlatform()
}
server:
port: 8081
servlet:
context-path: /user-service
spring:
application:
name: user-service
profiles:
active: dev
eureka:
instance:
instance-id: ${spring.cloud.client.hostname}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka
servlet.context-path
→ 서버가 응답할 요청의 기본 경로를 지정한다.application.name
→ 여기서는 user-service라고 두었는데 이 부분이 Eureka에 등록되는 이름이다. 따라서 Gateway 서비스에서 routes.uri를 어플리케이션 이름으로 두었는데 Gateway가 Eureka에 등록된 어플리케이션 이름을 확인하기에 동일하게 두어야한다.eureka.instance.instance-id
→ Eureka 서버에 등록될 때 사용할 인스턴스의 ID를 지정합니다. 여기서는 호스트 이름과 인스턴스 ID, 그리고 랜덤 값 중 하나를 사용하여 유니크한 인스턴스 ID를 생성한다.@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
정리가 잘 된 글이네요. 도움이 됐습니다.