Spring Cloud Zuul 网关配置:访问静态资源模块
- 在 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>
- 在 application.yml 文件中配置 zuul 网关的路由规则,将静态资源的请求路由到对应的静态资源模块:
zuul:
routes:
static:
path: '/static/**'
url: 'http://localhost:8081/static/'
- 在静态资源模块的 Controller 中添加对应的接口,用于返回静态资源:
@RestController
@RequestMapping('/static')
public class StaticResourceController {
@GetMapping('/{fileName}')
public ResponseEntity<byte[]> getFile(@PathVariable String fileName) throws IOException {
// 读取静态资源文件并返回 ResponseEntity
}
}
- 启动 zuul 网关和静态资源模块,访问 http://localhost:8080/static/fileName 即可获取静态资源。
原文地址: https://www.cveoy.top/t/topic/nu5i 著作权归作者所有。请勿转载和采集!