Rest Template LoadBalanced 추가 문서 작성
This commit is contained in:
6
document/communication/feign_client.md
Normal file
6
document/communication/feign_client.md
Normal file
@@ -0,0 +1,6 @@
|
||||
[이전 장(링크)](https://imprint.tistory.com/226?category=1069520) 에서는 Rest Template을 사용해서 마이크로서비스들 간의 통신(Communication)을 구현해보았다.
|
||||
이번 장에서는 Spring Cloud Netflix의 Feign Client를 사용하여 마이크로서비스 간의 통신을 구현해본다.
|
||||
모든 소스 코드는 [깃허브 (링크)](https://github.com/roy-zz/spring-cloud) 에 올려두었다.
|
||||
|
||||
---
|
||||
|
||||
@@ -238,7 +238,7 @@ public class OrderServiceImpl implements OrderService {
|
||||

|
||||
|
||||
**참고**
|
||||
요청 시 사용된 curl은 아래와 같다.
|
||||
1. 요청 시 사용된 curl은 아래와 같다.
|
||||
|
||||
```bash
|
||||
$ curl --location --request GET 'http://localhost:8000/user-service/users/f312a60f-e24d-4862-933b-97add5269f0c' \
|
||||
@@ -246,6 +246,39 @@ $ curl --location --request GET 'http://localhost:8000/user-service/users/f312a6
|
||||
--header 'Cookie: JSESSIONID=63BF99CFFAF313D37DB45A38641D7228'
|
||||
```
|
||||
|
||||
2. 변동되는 IP 대응
|
||||
|
||||
우리는 설정정보에 주문 서비스의 IP주소를 적어두었다.
|
||||
|
||||
```yaml
|
||||
order:
|
||||
url-prefix: http://127.0.0.1:8000/order-service
|
||||
get-orders-path: /%s/orders
|
||||
```
|
||||
|
||||
하지만 설정 정보에 이렇게 적어놓는 경우 변동되는 서버 IP를 대응할 수 없으며 Scale-Out 되어 새로운 IP가 추가되어도 적용할 수 없다.
|
||||
이전에 LoadBalancer에서 사용한 것과 같이 IP + Port 조합이 아닌 마이크로서비스의 이름으로 설정 정보를 구성할 수 있다.
|
||||
|
||||
RestTemplate를 빈으로 등록하는 단계에서 @LoadBalancer라는 애노테이션을 추가한다.
|
||||
|
||||
```java
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate getRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
```
|
||||
|
||||
설정정보에 IP + Port 로 구성된 주소 부분을 마이크로서비스 이름으로 변경한다.
|
||||
|
||||
```yaml
|
||||
order:
|
||||
url-prefix: http://order-service/order-service
|
||||
get-orders-path: /%s/orders
|
||||
```
|
||||
|
||||
이렇게 구성하는 경우 IP가 변동되거나 Scale-Out되어 새로운 IP가 추가되더라도 우리는 별다른 작업없이 적용이 가능하다.
|
||||
|
||||
---
|
||||
|
||||
지금까지 Rest Template을 사용하여 마이크로서비스간 통신하는 방법에 대해서 알아보았다.
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.roy.springcloud.userservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
@@ -21,6 +22,7 @@ public class UserServiceApplication {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate getRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user