[Spring Cloud] API Gateway 생성, 테스트

jsieon97·2023년 3월 9일
0

API Gateway 프로젝트 생성

dependencies

  • Lombok
  • Eureka Discovery Client
  • Gateway

application.yml

server:
  port: 8000

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

spring:
  application:
    name: apigateway-service
  cloud:
    gateway:
      routes:
        - id: first-service
          uri: http://localhost:8081/
          predicates:
            - Path=/first-service/**
        - id: second-service
          uri: http://localhost:8082/
          predicates:
            - Path=/second-service/**

연결할 Service 2개 생성

서로 다른 프로젝트로 생성

dependencies

  • Spring Web
  • Lombok
  • Eureka Discovery Client

application.yml

server:
  port: 8081

spring:
  application:
    name: my-first-service

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
server:
  port: 8082

spring:
  application:
    name: my-second-service

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

테스트를 위한 Controller 설정

// FirstServiceController.java

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/first-service")
public class FirstServiceController {

    @GetMapping("/welcome")
    public String welcome() {
        return "Welcome to the First service.";
    }
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/second-service")
public class SecondServiceController {

    @GetMapping("/welcome")
    public String welcome() {
        return "Welcome to the Second service";
    }
}

Test

port 8000으로 연결해도 Gateway를 통해 연결되는 모습을 확인할 수 있다.

profile
개발자로써 성장하는 방법

0개의 댓글