## 4. API Gateway 설정 및 테스트

Young Hwan Kim·2022년 1월 3일
0

목표 : UserMicroservice와 Spring Cloud Gateway 연결

API Gateway 설정

1. application.yml

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:
      default-filters:
        - name: GlobalFilter
          args:
            baseMessage: Spring Cloud Gateway Global Filter
            preLogger: true
            postLogger: true
      routes:
        - id: user-service
          uri: lb://USER-SERVICE
          predicates:
            - Path=/user-service/**
  • Path=/user-service/** 로 요청이오면 유레카의 lb://USER-SERVICE로 매핑된다. 그래서 UserController의 url앞에 'user-service'를 붙여줘야 한다.

2. ApiGatewayApplication

@SpringBootApplication
public class ApigatewayServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApigatewayServiceApplication.class, args);
    }
}

테스트

  1. 유레카 > API Gateway -> UserService를 차례대로 실행한다

  2. 유레카에 접속 : http://localhost:8761/

  3. 접속을 위해 UserController를 수정해야 한다.

private final Environment env;
    private final UserService userService;
    private final Greeting greeting;

    @GetMapping("/user-service/health_check")
    public String status() {
        return String.format("It's working in User Service on PORT %s",
        env.getProperty("local.server.port"));
    }

    @GetMapping("/user-service/welcome")
    public String welcome() {
        //return env.getProperty("greeting.message");
        return greeting.getMessage();
    }
  1. UserService로 바로 접속 테스트 성공

  2. API Gateway로 접속하여 테스트 성공

profile
Back-End DEVELOPER ☁️

0개의 댓글