MSA 학습(3) - Service discovery

엉무개·2021년 8월 24일
0

MSA

목록 보기
4/12

Service Discovery

  • MSA 환경에서는 서비스 상호간 API를 호출하게 되는데 IP가 동적으로 할당되는 클라우드 환경에서 클라이언트가 서비스 인스턴스를 찾을 수 있도록 해줌

1. Eureka Server 프로젝트 생성


2. 기본 서버 설정 (application.yml)

server:
  port: 8761
spring:
  application:
    name: discovery-service
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
#    개발시 false 운영시 true
    server:
      enable-self-preservation: false
      eviction-interval-timer-in-ms: 3000 #하트비트 수신점검

@EnableEurekaServer 어노테이션 추가 후 실행

3. 서버 실행 확인


4. Eureka Client 프로젝트 생성


5. Client 설정 (application.yml)

server:
  port: 9001
spring:
  application:
    name: user-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

6. Application 실행

@EnableDiscorveryClient 어노테이션 추가 후 실행


7. 서버 웹 새로고침 후 서비스 확인

8. 랜덤포트를 위해 port:0 수정 후 instance_id 설정

server:
  port: 0
spring:
  application:
    name: user-service
eureka:
  instance:
    instance-id: ${spring.cloud.client.hostname}:${spring.application.instance_id:${random.value}}
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

9. application시작, mvn spring-boot:run 입력하여 인스턴스 동시 실행 후 인스턴스 확인

10. mysql, JPA, eureka client 시 yml

server:
  port: 0
spring:
  application:
    name: product-service
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: 1234
    url: jdbc:mysql://localhost:3306/product-service?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
  jpa:
    database: mysql
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    generate-ddl: true
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        format_sql: true
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka
  instance:
    instance-id: ${spring.cloud.client.hostname}:${spring.application.instance_id:${random.value}}
#    운영시 삭제
    lease-renewal-interval-in-seconds: 1 # 디스커버리한테 1초마다 하트비트 전송  (기본 30)
    lease-expiration-duration-in-seconds: 2 # 디스커버리는 서비스 등록 해제 하기 전에 마지막 하트비트에서부터 2초 기다림
profile
엉덩이가 무거운 개발자

0개의 댓글