Spring Cloud + MSA (4)

김준영·2023년 3월 6일
2

Spring Cloud + MSA

목록 보기
4/9

전에 작성했던 코드들과 설명을 기반으로 로드 밸런서를 간단하게 해볼것이다.

흐름


  1. 사용자가 gateway 도메인으로 요청을 보낸다.
    • ex: localhost:8000/first service/welcome
  2. gateway는 discovery service를 해주는 Eureka Server에서 요청과 맞는 uri를 찾고 그 요청에 맞는 서비스에게 보낸다.

gateway


eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka

게이트웨이 뿐만 아니라 다른 서비스들도 yml파일에 위처럼 작성한다. 유레카 서버에 등록

등록 후 localhost:8761로 들어가면 위처럼 3가지 서비스가 등록되었다고 나와있다.

예전에는 게이트웨이에 서비스들의 주소를 작성했다면 유레카 서버를 등록한 지금은,

      routes:
        - id: first-service
          uri: lb://FIRST-SERVICE
          predicates:
            - Path=/first-service/**
          filters:
#            - AddRequestHeader=first-request, first-request-header2
#            - AddResponseHeader=first-response, first-response-header2
            - CustomFilter

위에처럼 작성하여 uri를 lb 즉, 로드밸런서에서 찾도록 한다.

서비스 여러 개 기동


  1. 인텔리제이에서 VM Options -> -Dserver.port=[]
  2. $ mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=9003'
  3. $ mvn clean complie package
    $ java -jar -Dserver.port=9004 ./target/user-service-0.0.1-SNAPSHOT.jar
profile
ㅎㅎ

1개의 댓글

comment-user-thumbnail
2023년 3월 8일

멋지다 준영이~

답글 달기