๋ง์ดํฌ๋ก์๋น์ค ํ๊ฒฝ์์ ์๋น์ค์ ๋ฑ๋ก ๋ฐ ์์น ์กฐํ๋ฅผ ๊ด๋ฆฌํ๋ ์๋น์ค ๋์ค์ปค๋ฒ๋ฆฌ ์๋ฒ
๋ค๋ฅธ ์๋น์ค๋ฅผ HTTP๋ก ํธ์ถํ ๋ ์ธํฐํ์ด์ค ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉํ ์ ์๋ ์ ์ธํ REST Client
๋ง์ดํฌ๋ก์๋น์ค ํ๊ฒฝ์์๋
ํ๋์ ์๋น์ค๊ฐ ์ฌ๋ฌ ์ธ์คํด์ค๋ก ์คํ๋ ์ ์์
์ด ๊ฒฝ์ฐ
Eureka๋ฅผ ์ฌ์ฉํ๋ฉด
FeignClient๋ฅผ ์ฌ์ฉํ๋ฉด
์ฆ
์๋น์ค ์์น ๊ด๋ฆฌ + ์๋น์ค ๊ฐ ํธ์ถ์ ๋จ์ํํ ์ ์์
ํด๊ฒฐํ๋ ๋ฌธ์
์๋น์ค ์์น ๊ด๋ฆฌ ๋ฌธ์ ํด๊ฒฐ
์๋น์ค ์ธ์คํด์ค ํ์ฅ ๋์
์๋น์ค ๊ฐ HTTP ํธ์ถ ์ฝ๋ ๋จ์ํ
๋ก๋๋ฐธ๋ฐ์ฑ ์ง์
์๋น์ค ๋ฑ๋ก ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ ์ค์ ์๋ฒ ์ญํ ์ํ
์์ ๊ตฌ์ฑ
Product Service๋ฅผ 3๊ฐ ์คํํ๋ฉด
Eureka์๋ ๋์ผ ์๋น์ค ์ด๋ฆ์ผ๋ก 3๊ฐ์ ์ธ์คํด์ค ๋ฑ๋ก ์ฒ๋ฆฌ
๊ฐ ์๋น์ค๊ฐ ์คํ๋ ๋
Eureka Server์ ์์ ์ ์์น ๋ฑ๋ก
์์
๊ฐ ์๋น์ค๋ ์คํ ์ Eureka์ ๋ฑ๋ก๋๊ณ
๋ค๋ฅธ ์๋น์ค๋ Eureka๋ฅผ ํตํด ์์น ์กฐํ ๊ฐ๋ฅ
๋ค๋ฅธ ์๋น์ค๋ฅผ ํธ์ถํ ๋
์ธํฐํ์ด์ค ์ ์ธ๋ง์ผ๋ก REST ํธ์ถ ์ฒ๋ฆฌ
์ง์ URL์ ์กฐํฉํ์ง ์๊ณ
์๋น์ค ์ด๋ฆ ๊ธฐ๋ฐ ํธ์ถ ์ฒ๋ฆฌ
FeignClient๋ Eureka์ ์ฐ๋ํ์ฌ
๋ฑ๋ก๋ ์๋น์ค ์ธ์คํด์ค ๋ชฉ๋ก์ ์กฐํํ๊ณ ํธ์ถ ์ํ
FeignClient๋ ์๋น์ค ํธ์ถ ์
์ฌ๋ฌ ์ธ์คํด์ค ์ค ํ๋๋ฅผ ์ ํํ์ฌ ์์ฒญ ์ ๋ฌ
์ฆ
Order Service โ Eureka ์กฐํ โ Product Service ์ธ์คํด์ค ์ ํ โ ํธ์ถ
์ฌ๋ฌ ์ธ์คํด์ค๊ฐ ์กด์ฌํ ๊ฒฝ์ฐ
๊ธฐ๋ณธ์ ์ผ๋ก Round Robin ๋ฐฉ์์ผ๋ก ์์ฒญ ๋ถ๋ฐฐ
Round Robin
Product Service๊ฐ Order Service๋ณด๋ค ๋จผ์ ์คํ๋๋ ๊ฒ์ด ์์ ์
์ด์
๋ฐ๋ผ์
Product Service ์คํ โ Order Service ์คํ ์์ ๊ถ์ฅ
์ ์ฒด ํ๋ฆ
Client โ Order Service โ FeignClient โ Eureka ์กฐํ โ Product Service
์ธ๋ถ ํ๋ฆ
์ํคํ ์ฒ ํ๋ฆ
Client
โ Order Service
โ Eureka Server
โ Product Service
Product Service ์ธ์คํด์ค
์๋น์ค ๊ตฌ์ฑ
Product Service๋ ๋์ผํ ์ ํ๋ฆฌ์ผ์ด์
์
๋ค๋ฅธ ํฌํธ๋ก ์คํํ์ฌ 3๊ฐ์ ์ธ์คํด์ค ๊ตฌ์ฑ
์์ ํฌํธ
Order Service ํธ์ถ
http://localhost:19091/order/1
์์ฒญ์ ์ฌ๋ฌ ๋ฒ ํธ์ถํ๋ฉด
์๋ต ํฌํธ๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฒ์ ํ์ธ ๊ฐ๋ฅ
์ด๋ฅผ ํตํด Round Robin ๋ก๋๋ฐธ๋ฐ์ฑ ๋์ ํ์ธ
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
@RestController
@RequestMapping("/products")
public class ProductController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/{id}")
public String getProduct(@PathVariable Long id) {
return "Product " + id + " info!!!! From port : " + serverPort;
}
}
๊ฐ Product ์ธ์คํด์ค๋
์์ ์ด ์คํ๋ ํฌํธ ๋ฒํธ๋ฅผ ํจ๊ป ๋ฐํ
์ด๋ฅผ ํตํด ์ด๋ค ์ธ์คํด์ค๊ฐ ์๋ตํ๋์ง ํ์ธ ๊ฐ๋ฅ
@FeignClient(name = "product-service")
public interface ProductFeignClient {
@GetMapping("/products/{id}")
String getProduct(@PathVariable("id") Long id);
}
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
private final ProductFeignClient productFeignClient;
@GetMapping("/{id}")
public String getOrder(@PathVariable Long id) {
return productFeignClient.getProduct(id);
}
}
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
spring:
application:
name: product-service
server:
port: 19092
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
Product Service๋
๊ฐ์ ์ ํ๋ฆฌ์ผ์ด์
์ ์ฌ๋ฌ ํฌํธ๋ก ์คํํ์ฌ
์ฌ๋ฌ ์ธ์คํด์ค๋ฅผ ๊ตฌ์ฑ ๊ฐ๋ฅ
์์
spring:
application:
name: order-service
server:
port: 19091
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
yml์ ๋ค์ฌ์ฐ๊ธฐ ๊ธฐ๋ฐ์ผ๋ก
์ค์ ์ ๊ณ์ธต ๊ตฌ์กฐ ํํ ๊ฐ๋ฅ
์์
spring:
application:
name: product-service
properties๋ผ๋ฉด
spring.application.name=product-service
์ค์ ์ด ๋ง์์ง์๋ก
yml์ด ๊ตฌ์กฐ ํ์
์ด ์ฌ์
ํนํ ๋ค์ ์ค์ ์ด ๋ง์ ๋ ์ ๋ฆฌ
Spring Boot๋
๋ ๋ค ์ง์
์ฆ properties๋ฅผ ๋ชป ์ฐ๋ ๊ฒ์ด ์๋๋ผ
์ค์ ๊ด๋ฆฌ ํธ์์ฑ ๋๋ฌธ์ yml์ ๋ ๋ง์ด ์ฌ์ฉ
yml์ ๊ณต๋ฐฑ ๊ธฐ๋ฐ ๋ฌธ๋ฒ
๋ณดํต 2์นธ ๋ค์ฌ์ฐ๊ธฐ ์ฌ์ฉ
์์
spring:
application:
name: order-service
ํญ ๋์ ๊ณต๋ฐฑ ์ฌ์ฉ ๊ถ์ฅ