• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

섹션 2.API Gateway Service 중 Spring Cloud Gateway-프로젝트 생성 부분 중 수행이 안됩니다.

23.12.15 03:01 작성 23.12.15 14:46 수정 조회수 795

0

강의와 똑같이 수행했다고 생각은 하는데, http://localhost:8081/first-service/welcome과 localhost:8082/second-service/welcome 를 개별적으로 호출했을 떄는 문제 없이 호출됩니다.

그런데 http://localhost:8000/first-service/welcome을 호출하면 404로 문구가 나오네요.

강의를 계속 봤는데 다른 부분 찾기가 어렵네요. 어느 부분에서 오류가 났는지 확인이 가능할까요?

 

firstController.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 {

    /*
    RequestMapping과 GetMapping이 조합이 되어서 
    호출됨
     */
    @GetMapping("/welcome")
    public String welcome(){
        return "Welcome to the First service";

    }
}

 

secondController.java

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

    /*
    RequestMapping과 GetMapping이 조합이 되어서 
    호출됨
     */
    @GetMapping("/welcome")
    public String welcome(){

        return "Welcome to the Second service";

    }

}

 apigateway-service 프로젝트의 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.4.2</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>apigateway-service</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>apigateway-service</name>
   <description>Demo project for Spring Boot</description>
   <properties>
      <java.version>17</java.version>
      <spring-cloud.version>2023.0.0</spring-cloud.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
               <excludes>
                  <exclude>
                     <groupId>org.projectlombok</groupId>
                     <artifactId>lombok</artifactId>
                  </exclude>
               </excludes>
            </configuration>
         </plugin>
      </plugins>
   </build>

</project>

 

apigateway-service 프로젝트의 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
          url: http://localhost:8081/
          predicates:
            - Path=/first-service/**
        - id: second-service
          url: http://localhost:8082/
          predicates:
            - Path=/second-service/**

답변 2

·

답변을 작성해보세요.

0

안녕하세요, 이도원입니다.

답변이 늦어 죄송합니다. 다른 분들이 먼저 도움이 주셨네요.

이메일로 보내주신 소스 코드를 확인해 보니, 몇가지 수정하셨으면 하는 부분이 있는 답글 남겨 드립니다.

  • spring boot와 spring cloud의 버전을 확인 해 보시기 바랍니다. spring cloud 2023.0.0 버전을 사용하실려면 spring boot 는 3.x 버전으로 올려서 사용하셔 하며, 2.4.x 버전에 맞는 버전을 찾기는 어려울 것 같으니, spring boot 2.7.x를 사용하시면서 spring cloud 2021.0.3 버전을 사용하시면 좋을 것 같습니다. spring cloud 2023.0.0을 계속 사용하실 거면 spring boot 3.1.x 이상으로 버전을 변경해서 사용하시기 바랍니다.

  • spring cloud gateway mvc 디펜던시 대신 spring cloud gateway 디펜던시로 사용해 보시기 바랍니다.

  • 보내주신 소스 코드에는 spring cloud service discovery에 등록되지 않도록 환경 설정이 되어 있습니다. apigateway-service 설정에 보면 location:8761로 되어 있는 부분이 있는데, service registry(eureka 등)에 등록하지 않도록 설정하신 게 맞는지 확인해 보시기 바랍니다. (물론 사용하지 않고도 실행은 가능합니다만 현재소스 코드를 수정하셔야 합니다)

  • apigateway-service에 설정 파일 내용 중 spring.cloud.routes 부분에 url이 아니라 uri 로 변경하셔야 합니다. 이 부분이 오류로 인식 되지 않은 이유는 spring cloud에 맞는 버전이 기동되지 못해서, 체크 되지 않는 것 같습니다.

나머지 수정 된 코드는 밑에 올려진 소스 등으로 수정하여 실행 가능합니다. 보내주신 코드를 수정 한 내용은 메일로 다시 보내드렸습니다.

감사합니다.

0

21jongg님의 프로필

21jongg

2023.12.17

maven pom.xml에 spring-cloud-stater-gateway-mvc만 있으므로 아래와 같은 property를 작성하시면 되시고,



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

또는 spring-cloud-starter-gateway를 dependency에 추가하시면 될거같습니다.

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
    </dependency>

답변 감사드립니다.

위와 같이 유레카 부분을 지우고 pom.xml에 spring-cloud-starter-gateway를 추가 했는데 여전히 호출이 안되네요.

21jongg님의 프로필

21jongg

2023.12.17

유레카 클라이언트는 유레카(discovery-service)와 연동하기 위한거라서 있어야합니다.

결국 pom.xml에 spring cloud gateway, eureka client, lombok을 디펜던시로 설정하면 되실거같습니다.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
		<spring-cloud.version>2023.0.0</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-gateway</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>io.projectreactor</groupId>
			<artifactId>reactor-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

위 pom.xml에서 dependency부분만 참고해주세요.

답변 감사드립니다. 아래와 같이 수정하니 동작하네요.

application.yml

server:
  port: 8000

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

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

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>apigateway-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>apigateway-service</name>
    <description>apigateway-service</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>