1. 在 pom.xml 中添加 zuul 和 web 依赖:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 在 application.yml 文件中配置 zuul 网关的路由规则,将静态资源的请求路由到对应的静态资源模块:
zuul:
  routes:
    static:
      path: '/static/**'
      url: 'http://localhost:8081/static/'
  1. 在静态资源模块的 Controller 中添加对应的接口,用于返回静态资源:
@RestController
@RequestMapping('/static')
public class StaticResourceController {

    @GetMapping('/{fileName}')
    public ResponseEntity<byte[]> getFile(@PathVariable String fileName) throws IOException {
        // 读取静态资源文件并返回 ResponseEntity
    }
}
  1. 启动 zuul 网关和静态资源模块,访问 http://localhost:8080/static/fileName 即可获取静态资源。
Spring Cloud Zuul 网关配置:访问静态资源模块

原文地址: https://www.cveoy.top/t/topic/nu5i 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录