반응형
본 내용은 인프런의 이도원 님의 강의 "Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)" 내용을 바탕으로 정리한 내용입니다.
Spring Cloud Bus란?
- 분산 시스템 환경에서 마이크로서비스 간의 이벤트를 전파하고 동기화하는 역할을 한다.
- 구성 변경 사항을 마이크로서비스 애플리케이션 전반에 걸쳐 효율적으로 전파할 때 유용하다.
- 경량 메시지 브로커를 사용하여 애플리케이션 간의 메시지를 전달한다.
- ConfigService가 가지고 설정 정보 변경시 재기동 없이 각 서비스에 변경사항을 반영할 수 있다.
AMQP
- 메시지 지향 미들웨어를 위한 개방형 표준 응용 계층 프로토콜
- Erlang, Eabbit MQ에서 사용
- RabbitMQ와 같은 브로커에서 주로 사용된다.
RabbitMQ
- 오픈 소스 메시지 브로커 소프트웨어로, 주로 AMQP를 구현한 시스템이다.
- 메시지 브로커는 시스템 간에 메시지를 전달하는 중간 역할을 하며, RabbitMQ는 이를 통해 비동기 작업을 효율적으로 처리할 수 있게 해준다.
- 초당 20+ 메시지를 소비자에게 전달한다.
Kafka란?
- 분산형 이벤트 스트리밍 플랫폼으로 실시간으로 데이터 스트림을 게시, 구독, 저장 및 처리할 수 있다.
- 초당 100k+ 이상의 이벤트를 처리 할 수 있다.
- 대용량에는 RabbitMQ보다 적합하다.
Kafka 프로젝트
- Apache Software Foundation이 스칼라 언어로 개발한 오픈 소스 메시지 브로커 프로젝트
- 분산형 스트리밍 플랫폼
- 대용량의 데이터를 처리 가능한 메시징 시스템
Spring Cloud Bus 구성 실습
파일 다운로드
- https://www.erlang.org/downloads 에서 erlang 설치
- https://www.rabbitmq.com/docs/download 에서 RabbitMQ 설치
- 환경변수에 해당 경로 추가
rabbitmq-plugins enable rabbitmq_management
- 해당 명령어로 RabbitMQ Management Plugin 설치
- RabbitMQ 관리 UI에 접속한다.
- URL: http://localhost:15672
- 기본 계정: guest / guest
ConfigService
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
- Spring Cloud Bus AMQP와 Actuator 종속성 추가한다.
application.yml
spring:
application:
name: ConfigService
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
- RabbitMQ 서버에 연결하기 위한 기본 설정 정보 추가
UserService
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- Spring Cloud Bus AMQP 종속성 추가한다.
application.yml
spring:
application:
name: UserService
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: refresh, health, beans, busrefresh
- RabbitMQ 연결 정보와 Actuator 엔드포인트 설정을 추가한다.
- endpoints에 busrefresh 를 추가하여 Spring Cloud Bus로 모든 연결된 애플리케이션 인스턴스의 설정을 리프레시한다.
bootstrap.yml
spring:
cloud:
config:
uri: http://127.0.0.1:8888
name: ConfigService
# profiles:
# active: dev
- ConfigService의 application.yml에 설정되어 있는 propertie 값을 읽어온다.
GatewayService
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
application.yml
spring:
application:
name: GatewayService
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: refresh, health, beans, httpexchanges, busrefresh
- RabbitMQ 서버에 연결하기 위한 기본 설정 정보 추가
- endpoints에 busrefresh 를 추가하여 Spring Cloud Bus로 모든 연결된 애플리케이션 인스턴스의 설정을 refresh한다.
결과
- http://127.0.0.1:8080/userService/login 호출하여 로그인하고 JWT 토큰 반환
- http://127.0.0.1:8080/userService/health_check 호출하여 200 메시지 확인
- YourSuperSecretKeyForJwtHS256Algorithm 토큰 시크릿 키를 YourSuperSecretKeyForJwtHS256Algorithm_changed_#1 로 변환
{
"name": "ConfigService",
"profiles": [
"default"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "file:/C:/MSA/native-file-lepo/application.yml",
"source": {
"token.expiration_time": 864000000,
"token.secret": "YourSuperSecretKeyForJwtHS256Algorithm_changed_#1",
"gateway.ip": "127.0.0.1"
}
}
]
}
- http://127.0.0.1:8888/ConfigService/default 호출 시 token.secret의 변환된 키를 가져온다.
- http://127.0.0.1:8080/userService/health_check 호출 시 변환되지 않은 시크릿키를 가져오기에 서버가 정상적으로 응답한다.
- http://127.0.0.1:8080/userService/actuator/busrefresh POST 방식으로 호출하여 204 코드는 정상 반환이지만 반환할 콘텐츠가 없다는 뜻이다.
- Spring Cloud Bus가 메시지 브로커를 통해 변경사항을 반영한다.
- http://127.0.0.1:8080/userService/health_check 호출시 변경된 시크릿키가 반영되는데 이때 GatewayService에도 변경된 키가 반영되며 이로 인해 인증이 실패하면서 401 코드가 나온다.
- http://127.0.0.1:8080/actuator/busrefresh 호출시에도UserService와 GatewayService가 동시에 변경된다.
RabittMQ 계정 추가
- RebiitMQ 계정을 추가해서 사용할 수 있다.
RabbitMQ와 Kafka 비교
특징 | RabbitMQ | Kafka |
---|---|---|
주요 프로토콜 | AMQP | 자체 프로토콜 |
성능 | 초당 20+ 메시지 | 초당 100k+ 이벤트 |
용도 | 작업 큐, 비동기 메시지 처리 | 실시간 스트림 처리, 로그 처리 |
주요 활용 | 단순 메시징 및 작업 처리 | 실시간 스트림 처리 및 대규모 데이터 처리 |
반응형
'Cloud > MSA' 카테고리의 다른 글
[MSA] Feign Client (0) | 2024.12.02 |
---|---|
[MSA] Java KeyStore(JKS) (0) | 2024.12.02 |
[MSA] Spring Cloud Config (0) | 2024.12.02 |
[MSA] MicroService 구현(로그인) (0) | 2024.11.27 |
[MSA] MicroService 구현(주문 서비스) (1) | 2024.11.27 |