전체 내용은 Github에 있습니다 :)
Netflix Eureka Server HA
이전 포스팅에서 Eureka에 대한 개요를 살펴보았다.
이번엔 Eureka Server에서 HA 구성을 어떻게 하는지 살펴보자.
Peer Awareness
- Eureka Cluster에서 Eureka Server는
peer
라고 표현하고 아래와 같은 행위를Peer Awareness
라고 부른다. - 각각의 Eureka Server는 Eureka Client를 이용하여
Register
와Fetch Registry
작업이 이루어진다. - 또한
Register
,Renew
등 모든 Operation이 발생하면 다른 Peer에게 동일한 요청을 시도한다.
아래와 같이 PeerAwareInstanceRegistryImpl를 살펴보자.
Register
요청에 대하여 자신의 Registry를 업데이트하고 replicateToPeers()
를 통해 다른 Peer에게Register with replica
요청을 보낸다.(즉 POST /eureka/v2/apps/appID
요청과 헤더에 Replica 속성을 true)
@Singleton
public class PeerAwareInstanceRegistryImpl extends AbstractInstanceRegistry implements PeerAwareInstanceRegistry {
...
@Override
public void register(final InstanceInfo info, final boolean isReplication) {
int leaseDuration = Lease.DEFAULT_DURATION_IN_SECS;
if (info.getLeaseInfo() != null && info.getLeaseInfo().getDurationInSecs() > 0) {
leaseDuration = info.getLeaseInfo().getDurationInSecs();
}
super.register(info, leaseDuration, isReplication);
replicateToPeers(Action.Register, info.getAppName(), info.getId(), info, null, isReplication);
}
...
}
Eureka Server HA 구성하기
아래와 같이 Spring Profile 속성을 활용하여 peer1-3을 구성해보자
application.yaml
---
spring:
profiles: peer-1
application:
name: eureka-server-clustered
server:
port: 3001
eureka:
instance:
hostname: peer-1-server.com
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:3002/eureka/,http://localhost:3003/eureka/
---
spring:
profiles: peer-2
application:
name: eureka-server-clustered
server:
port: 3002
eureka:
instance:
hostname: peer-2-server.com
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:3001/eureka/,http://localhost:3003/eureka/
---
spring:
profiles: peer-3
application:
name: eureka-server-clustered
server:
port: 3003
eureka:
instance:
hostname: peer-3-server.com
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:3001/eureka/,http://localhost:3002/eureka/
docker-compose를 이용하여 3개의 Server를 실행하자
docker-compose.yaml
version: '3.1'
services:
eureka-peer-1:
build:
context: ../../eureka-server
dockerfile: Dockerfile
image: service-discovery/eureka-server
hostname: eureka-peer-1
container_name: eureka-peer-1
ports:
- 3001:3001
environment:
- SPRING_PROFILES_ACTIVE=peer-1
- EUREKA_CLIENT_SERVICE-URL_DEFAULT-ZONE=http://eureka-peer-2:3002/eureka/,http://eureka-peer-3:3003/eureka/
eureka-peer-2:
image: service-discovery/eureka-server
hostname: eureka-peer-2
container_name: eureka-peer-2
ports:
- 3002:3002
environment:
- SPRING_PROFILES_ACTIVE=peer-2
- EUREKA_CLIENT_SERVICE-URL_DEFAULT-ZONE=http://eureka-peer-1:3001/eureka/,http://eureka-peer-3:3003/eureka/
eureka-peer-3:
image: service-discovery/eureka-server
hostname: eureka-peer-3
container_name: eureka-peer-3
ports:
- 3003:3003
environment:
- SPRING_PROFILES_ACTIVE=peer-3
- EUREKA_CLIENT_SERVICE-URL_DEFAULT-ZONE=http://eureka-peer-1:3001/eureka/,http://eureka-peer-2:3002/eureka/
account-service:
build:
context: ../../account
dockerfile: Dockerfile
image: service-discovery/account
container_name: account-service
restart: always
environment:
- STARTUP_WAIT=5
- EUREKA_CLIENT_SERVICE-URL_DEFAULT-ZONE=http://eureka-peer-1:3001/eureka/,http://eureka-peer-2:3002/eureka/,http://eureka-peer-3:3003/eureka/
ports:
- 3100:3100
depends_on:
- eureka-peer-1
- eureka-peer-2
- eureka-peer-3
article-service:
build:
context: ../../article
dockerfile: Dockerfile
image: service-discovery/article
container_name: article-service
restart: always
environment:
- STARTUP_WAIT=5
- EUREKA_CLIENT_SERVICE-URL_DEFAULT-ZONE=http://eureka-peer-1:3001/eureka/,http://eureka-peer-2:3002/eureka/,http://eureka-peer-3:3003/eureka/
ports:
- 3200:3200
depends_on:
- eureka-peer-1
- eureka-peer-2
- eureka-peer-3
- account-service
'Spring Cloud' 카테고리의 다른 글
Spring Cloud Service Discovery - Netflix Eureka (1) (0) | 2020.09.02 |
---|