상세 컨텐츠

본문 제목

Eureka + Gateway

SpringCloud

by 성찬우 2022. 9. 6. 15:09

본문

한동안 감기 때문에 죽는 줄알았네요..
다들 감기 조심하십쇼..ㅠㅠ 

 

여태까지 유레카와 게이트웨이에 대해서 어느 정도 알아 보았는데 

게이트 웨이의 필터를 제외한 부분들을 한번 합쳐볼 생각이다. 

 

  • 1개의 Eureka Server(8761) + 3개의 Eureka Client(1개는 Gateway, 2개는 ms)
  • Gateway 에서의 로드밸런스 uri 설정 
  • ms에서의 포트번호(0) 설정 및 instance-id 설정

 

 

 

 

Eureka Server 및 Client 설정

 

 

  • Server의 경우는 딱 2개만 기억하면 될 것같다. 

@EnableEurekaServer 및 .yml 설정

server:
  port: 8761

spring:
  application:
    name: 이름 지어주기~  

eureka:
  client: 
    register-with-eureka: false #true라 하면 본인이 본일껄 등록하게 될거야
    fetch-registry: false #true라 하면 본인이 본일껄 등록하게 될거야

 

 

  • Client 설정은 어플리케이션 이름설정, eureka 설정, port번호 

ms의 경우 포트번호는 gateway 로드밸런서를 위한 0번

gateway의 경우 8000번으로 지어주었다. 

 

first-service

server:
  port: 0

spring:
  application:
    name: first-gateway-client
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    instance-id: ${spring.cloud.client.hostname}:${spring.application.instance_id:${random.value}}

second-service

server:
  port: 0

spring:
  application:
    name: second-gateway-client
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    instance-id: ${spring.cloud.client.hostname}:${spring.application.instance_id:${random.value}}

gateway

server:
  port: 8000

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

spring:
  application:
    name: apigateway-service
    
    
    
    ------------------여기서 부터는 ms 등록------------------------
    
    
---
spring:
  cloud:
    gateway:
      routes:
        - id: 1
          uri: lb://FIRST-GATEWAY-CLIENT       <<< uri 확인
          predicates:
            - Path=/first/**
          filters:
        - id: 2
          uri: lb://SECOND-GATEWAY-CLIENT
          predicates:
            - Path=/second/**
          filters:
            - CustomFilter

 

 

Gateway 로드밸런스 uri 확인

uri 를 확인해 보라는 것은  로드밸런서를 쓰기위해 

lb://어플리케이션이름

을 확인해야한다라는 것이다. 

 

port:0 및 instance-id : 설정 이후 port번호 랜덤 설정 확인하기
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    instance-id: ${spring.cloud.client.hostname}:${spring.application.instance_id:${random.value}}

port:0과함께 위와같이 설정을 해주었을 경우 우리는 하나의 어플리케이션을 다수 동작했을 때 

알아서 port번호가 할당된다. 

 

이를 확인해보기 위해서 

@GetMapping("/endPoint")
    public String end(HttpServletRequest request){
        int localPort = request.getLocalPort();
        log.info("this.port = {}", localPort);
        return String.format("port : %d", localPort);
    }

터미널로 여러개를 실행해서 port번호가 동적으로 할당이 되는지 확인해보고자 한다. 

 

결과 보기 

1. 인텔리제이로 실행하기 

run 버튼 누르기

2. 터미널로 실행하기 

mvn spring-boot:run 하기 

 

 

postman 으로 해당 경로에 요청을 넣어보면

Run 과 Terminal 에서 각각 번갈아 가며 포트번호를 할당하는 것을 확인 할 수 있다. 

관련글 더보기

댓글 영역