在springcloud的项目中其中一个模块负责存储前端的静态资源需要使用到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/cpnD 著作权归作者所有。请勿转载和采集!