본문 바로가기
SI 업무/MSA관련

5. Gateway with (lb://)

by 새로운걸 배우는게 너무 싫은 IT 복붙러 2024. 12. 25.
728x90

4 까지는 Discovery Server 에 MSA ( firstservice, sencodservice) 를 등록 안했음.

이제는 등록을 하고 gateway 가 Discover Server( = Eureka) 에서 정보를 읽어와서 MSA 를 찾는 구조로 변경 해본다.


firstservice, secondservice, msaDiscovery, apigateway-service 소스는 아래 git 에 있음.
https://github.com/suersehii1/study

 

우선 소스를 다 다운 받고 intellj 에서 해당 프로젝트를 다 연후

첫번째로 Discovery Sever(=Eureka) 를 start 시킨다.


Discovery Server 에 MSA 정보를 등록 해야 한다.
firstservice 와 secondservice 의 프로젝트를 intellj 에서 연후 , 

firstservice, secondservice 를 Eureka 에 등록 하도록 application.yml 의 설정을 변경한다.

 


firstservice 를 8081, 8082

secondservice 를 9091,9092 로  start 시킨다.

총 4개의 프로세스가 도는것임.

그냥 Intellj 의 editConfiuration 의 각각 2개씩 만들고 각각 실행 시킴

설정시 아래처럼 상황에 맞게 port 정보만 변경해 주면 됨.


4개의 프로세스가 Eureka 에 다 등록 되어 있는지 확인 한다

 


apigateway-service 프로젝트를 intellj 로 연다.
Gateway 를 Eureka 에 추가하기 위해 application.yml 파일을 수정한다.

 

4번 blog 에서는 specific 하게 route 정보가 fix 되었지만 이제는 lb:// 라는 걸로 eureka server에서 해당 이름을 찾아서

route 처리를 한다.

server:
  port: 8000

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

spring:
  application:
    name: apigateway-service
  cloud:
    gateway:
      routes:
        - id: first-service
          uri: lb://FIRSTSERVICE
          predicates:
            - Path=/first-service/**
        - id: second-service
          uri: lb://SECONDSERVICE
          predicates:
            - Path=/second-service/**

 


apigateway-service 까지 start 시킨후 http://localhost:8761 확인시, 총 5개의 service 가 보임.

firstservice2 개

secondservice2개

apigateway-service 1


여기서 한동안 apigateway-service가 eureka 에 등록이 되지 않아 골머리를 썩음.

시킨대로 했는데 안됨.

이유는

pom.xml 의 의존성을 잘못 넣었음.

gateway 프로젝트 구현을 위한 의존성에  Reactive Gateway 를 넣어야 하나

실수로 Gateway 를 집어 넣고, Spring web을 넣었음.

 

정리하면 Gateway 를 만들때 아래 3개만 넣으면 됨. (Reactvie Gateway 를 사용할때는 Spring web은 충돌남. 하여 빼야 함)

잘 된 의존성
잘못된 의존성

 

 


결과: localhost:8000/second-service/welcome  요청 하면

        -> 아래 apigateway-service 의 application.xml 정보를 가지고

           

             - id: second-service
               uri: lb://SECONDSERVICE
               predicates:
               - Path=/second-service/**

        -> discovery service 에서 정보를 읽어와 route 처리를 함.

 

728x90