Spring Cloud API Gateway路由异常:student-service访问返回404求助
Spring Cloud微服务API Gateway调用student-service出现404问题排查
我使用Spring Cloud开发微服务,项目包含college-service、student-service、eureka-server和api-gateway四个服务。通过API Gateway调用业务服务时,college-service可正常访问,但调用student-service时出现404 NotFound错误。测试URL及结果如下:
| URL | 状态 |
|---|---|
| http://localhost:9001/college/CLG01 | 200 OK |
| http://localhost:9002/college/student/CLG01 | 200 OK |
| http://localhost:9003/college/CLG01 | 200 OK |
| http://localhost:9003/college/student/CLG01 | 404 NotFound |
以下是各服务的代码及配置信息:
College Service
实体类
public class College { private String clgId; private String clgName; private String clgCity; List<Student> students; public College(String clgId, String clgName, String clgCity, List<Student> students) { this.clgId = clgId; this.clgName = clgName; this.clgCity = clgCity; this.students = students; } public College(String clgId, String clgName, String clgCity) { this.clgId = clgId; this.clgName = clgName; this.clgCity = clgCity; } // getter and setter } public class Student { private Long stId; private String stName; private String stEmail; private String clgId; public Student(Long stId, String stName, String stEmail, String clgId) { super(); this.stId = stId; this.stName = stName; this.stEmail = stEmail; this.clgId = clgId; } // getter and setter }
Controller
@RestController @RequestMapping("/college") public class CollegeController { @Autowired private CollegeService collegeService; @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/{clgId}", method = RequestMethod.GET) public ResponseEntity<College> getColleges(@PathVariable("clgId") String clgId) { College college = collegeService.getCollege(clgId); List student = restTemplate.getForObject("http://student-service/college/student/" + college.getClgId(), List.class); college.setStudents(student); return ResponseEntity.ok(college); } }
application.yml
server: port: 9001 spring: application: name: college-service eureka: instance: hostname: localhost
Student Service
实体类
public class Student { private Long stId; private String stName; private String stEmail; private String clgId; public Student(Long stId, String stName, String stEmail, String clgId) { super(); this.stId = stId; this.stName = stName; this.stEmail = stEmail; this.clgId = clgId; } // getter and setter }
Controller
@RestController @RequestMapping("/college") public class StudentCotroller { @Autowired private StudentService studentService; @RequestMapping(value = "/student/{clgId}", method = RequestMethod.GET) public ResponseEntity<List<Student>> getStudents(@PathVariable("clgId") String clgId) { return ResponseEntity.ok(studentService.getStudents(clgId)); } }
application.yml
server: port: 9002 spring: application: name: student-service eureka: instance: hostname: localhost
Eureka Server
application.yml
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false server: waitTimeInMsWhenSyncEmpty: 0
API Gateway Service
application.yml
server: port: 9003 eureka: instance: hostname: localhost spring: application: name: api-gateway cloud: gateway: routes: - id: college-service uri: lb://college-service predicates: - Path=/college/** - id: student-service uri: lb://student-service predicates: - Path=/student/**
问题原因
API Gateway的路由配置存在匹配逻辑错误:
- student-service的接口路径为
/college/student/{clgId},但网关配置的student-service路由断言是Path=/student/**,无法匹配/college/student/CLG01请求。 - 该请求会被college-service的路由
Path=/college/**匹配,转发到college-service,但college-service无对应接口,因此返回404。
解决方案
调整API Gateway的路由顺序和断言规则,让/college/student/**的请求优先转发到student-service:
修改后的API Gateway application.yml:
server: port: 9003 eureka: instance: hostname: localhost spring: application: name: api-gateway cloud: gateway: routes: - id: student-service uri: lb://student-service predicates: - Path=/college/student/** - id: college-service uri: lb://college-service predicates: - Path=/college/**
调整后:
- 访问
http://localhost:9003/college/student/CLG01时,会先匹配到student-service的路由,转发到对应接口返回200。 - 访问
http://localhost:9003/college/CLG01时,匹配college-service的路由,正常返回结果。
另外确认student-service已正确注册到Eureka Server(从直接访问测试结果看注册状态正常)。
内容的提问来源于stack exchange,提问作者Faheem azaz Bhanej
相关产品推荐
相关产品推荐

