Spring Cloud Gateway 没有重定向到其他应用程序

问题描述 投票:0回答:1

我有一个在端口 9001 上公开的电影 API,我正在尝试在端口 9000 上构建一个网关 API 以将请求重定向到电影 API。 Java 17,操作系统 Win10

这里是电影 API 的代码。

控制器:

@RestController
@RequestMapping("/api/movies")
public class MoviesAPI {
    private final Logger log = LoggerFactory.getLogger(MoviesAPI.class);

    private final MovieRepository movieRepository;

    public MoviesAPI(MovieRepository movieRepository) {
        this.movieRepository = movieRepository;
    }

    @GetMapping
    public List<Movie> getAll(){
        log.info("REST request to get all movies");
        return movieRepository.findAll();
    }

    @PostMapping
    public Optional<Movie> saveMovie(@RequestBody Movie movie){
        log.info("REST request to save movie : {}", movie);
        movie.setLastUpdatedTime(Instant.now());
        return Optional.of(movieRepository.save(movie));
    }

    @GetMapping("/{id}")
    public Optional<Movie> getMovieById(@PathVariable UUID id){
        log.info("REST request to get movie by id : {}", id);
        return movieRepository.findById(id);
    }

    @DeleteMapping("/{id}")
    public void deleteMovie(@PathVariable UUID id) {
        log.info("REST request to delete movie by id : {}", id);
        if (movieRepository.existsById(id)) {
            movieRepository.deleteById(id);
        } else {
            throw new RuntimeException("Movie with ID " + id + " not found");
        }
    }
}

application.yml:

server:
  port: 9001
spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: movies1
# swagger-ui custom path
springdoc:
  swagger-ui:
    path: /swagger-ui.html

主要应用类:

@SpringBootApplication
public class SmartProxyJavocApplication {

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

}

这是网关API的代码。

控制器:

@RestController
@RequestMapping("/gateway")
public class GatewayFallback {

    private final Logger log = LoggerFactory.getLogger(GatewayFallback.class);

    @GetMapping("/all")
    public String getAccount() {
        return "yes";
    }

}

application.yml:

spring:
  cloud:
    gateway:
      routes:
        - id: route1
          # where we send traffic
          uri: http://localhost:9001/api/movies
          predicates:
            - Path=/gateway/all
server:
  port: 9000

主要应用类:

@SpringBootApplication
public class ProxyApplication {

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

}

我尝试了几个路径值,例如:

/gateway/all/**, /gateway/**
,更改网关的服务器端口(8080)。根据我读过的教程和文章,代码看起来不错,但我仍然无法弄清楚问题是什么。

java spring spring-cloud spring-cloud-gateway
1个回答
0
投票
spring:
  cloud:
    gateway:
      routes:
      - id: route1
      # where we send traffic
        uri: http://localhost:9001/api/movies
        predicates:
        - Path=/movies/**
        filters:
          - RewritePath=/movies/(?<segment>.*),/api/movies/$\{segment}

这是最终有效的配置。 这是我从 Baeldung link 找到并适应我的案例的示例。非常感谢 spencergibb 提供 SetPath 和 RewritePath 的指导。

我需要 /movies/** 中的所有内容,才能转到另一个应用程序中的 /api/movies/** ,现在它可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.