Spring Cloud Gateway - Load Balancer
- 현재 2개의 서비스가 있음 8081,8082
- 클라이언트에서 api gateway를 통과하여 요청을 보내면 유레카에 전달되어 위치정보를 전달받고 해당 정보로 포워딩 시켜준다.
- gateway yml파일 각 라우트에 uri 서비스 이름으로 변경
server:
port: 8080
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: <http://localhost:8761/eureka>
spring:
application:
name: gateway-service
cloud:
gateway:
default-filters:
- name: GlobalFilter
args:
baseMessage: Spring Cloud Gateway Filter
preLogger: true
PostLogger: true
routes:
- id: first-service
uri: lb://my-first-service
predicates:
- Path=/first-service/**
filters:
#- AddRequestHeader=first-request, first-request-header2
#- AddResponseHeader=first-response, first-response-header2
- CustomFilter
- id: first-service
uri: lb://my-second-service
predicates:
- Path=/second-service/**
filters:
#- AddRequestHeader=second-request, second-request-header2
#- AddResponseHeader=second-response, second-response-header2
- CustomFilter
- userService yml파일
server:
# 0? ?? ?? ??? ?????? ??! ?? ??? ???? ??? ?? ??? ??.
port: 0
spring:
application:
name: ${NAME}
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true
# Eureka ??? ?? ?????? ??? ????? ??? ???
# ???? ?? -> fetch-registry = true ?? ? ??? ???? ??
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:8761/eureka
- uri: lb://my-first-service 이렇게 이름으로 정보를 가져오게 되면 포트정보나 기타 정보가 변경된다 하더라도 정상적으로 가져올 수 있다.
유저 서비스에서 랜덤 포트로 first 2개 second 2개를 띄워보자
- 포트 번호를 반환하는 api
private final Environment env;
@GetMapping("first-service/check")
public String check(HttpServletRequest request){
log.info("Server port={}",request.getServerPort());
return String.format("HI Port %s",env.getProperty("local.server.port"));
}
- 라운드 로빈 방식으로 차례차례 해당 서버에 할당되었음을 확인!
'DevOps > MSA' 카테고리의 다른 글
Apache kafka (0) | 2023.05.24 |
---|---|
Spring Cloud Gateway - Filter 적용 (0) | 2023.05.14 |
Spring Cloud Gateway (0) | 2023.05.14 |
Spring Cloud 와 Eureka (0) | 2023.05.14 |