implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
spring:
application:
name: eureka-service //애플리케이션의 이름을 정의한다.
server:
port: 8761
eureka:
client:
register-with-eureka: false //자신을 유레카 서버에 등록할지 여부 유레카 서버이므로 false
fetch-registry: false //다른 유레카 서버로 부터 등록된 서비스 목록을 가져오지 않는다.
service-url:
defaultZone: http://localhost:8761/eureka/ //eureka 클라이언트가 등록할 eureka 서버의 url을 설정한다.
instance:
hostname: localhost //유레카 서버의 호스트 이름 설정, 클라이언트들에게 자신을 알릴때 사용
@SpringBootApplication
@EnableEurekaServer
public class MsaPlatformApplication {
public static void main(String[] args) {
SpringApplication.run(MsaPlatformApplication.class, args);
}
}
./gradlew build -x test //테스트를 제외하고 jar 파일 생성
FROM openjdk:17-jdk-alpine
WORKDIR /app
COPY /build/libs/msa-platform-0.0.1-SNAPSHOT.jar /app/msa-platform.jar // 호스트 시스템 있는 파일 컨테이너 내부로 복사
ENTRYPOINT ["java", "-jar", "/app/msa-platform.jar"] // 컨테이너가 실행되면 항상 실행될 명령어 지정 자바 프로그램 실행
eureka-service:
build:
context: ./server/
dockerfile: Dockerfile
container_name: eureka-service
ports:
- "8761:8761"
networks:
- msa-network
hub-service:
build:
context: ./hub_service/
dockerfile: Dockerfile
container_name: hub-service
ports:
- "19600:19600"
depends_on:
- eureka-service
- hub-db
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://hub-db:5432/hub_base
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: postgres
restart: always
networks:
- msa-network
- hub-network
networks:
- msa-network
네트워크를 맞춰줘야 eureka와 통신을 할 수 있다. 여러 서비스 간 통신이 필요할 때 같은 네트워크에 속하게 된다.
build:
context: ./server/
dockerfile: Dockerfile
container_name: eureka-service
호스트 포트번호와 컨테이너 내부의 포트번호를 매핑한다.
ports:
- "19600:19600"
eureka:
client:
register-with-eureka: true # 클라이언트가 Eureka 서버에 자신을 등록하도록 설정
fetch-registry: true # Eureka 서버에서 다른 서비스들의 정보 목록을 가져오도록 설정
service-url:
defaultZone: http://eureka-service:8761/eureka/ # Eureka 서버의 컨테이너 이름 사용
instance:
prefer-ip-address: true # IP 주소 기반으로 인스턴스를 알리도록 설정 (선택 사항)
이런식으로 설정해야 한다.
http://eureka-service:8761/eureka/