Spring Cloud:API Gateway routing not working

8iwquhpp  于 2024-01-06  发布在  Spring
关注(0)|答案(2)|浏览(143)

我正在使用Spring Cloud。我的Sping Boot 应用程序中有四个服务college-servicestudent-serviceeureka-serverapi-gateway。我试图使用API Gateway调用college-servicestudent-service。当我从工作正常的API Gateway调用My college-service时,student-service不工作。当我试图获取student-serivice时,我得到错误404未找到。我是Spring Cloud的新人。下面是我的代码。

  • 下面是我的网址,在最后一个网址我得到404*

| 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| 400 Not Found|

学院服务

  • 实体 *
  1. public class College {
  2. private String clgId;
  3. private String clgName;
  4. private String clgCity;
  5. List<Student> students;
  6. public College(String clgId, String clgName, String clgCity, List<Student> students) {
  7. this.clgId = clgId;
  8. this.clgName = clgName;
  9. this.clgCity = clgCity;
  10. this.students = students;
  11. }
  12. public College(String clgId, String clgName, String clgCity) {
  13. this.clgId = clgId;
  14. this.clgName = clgName;
  15. this.clgCity = clgCity;
  16. }
  17. // getter and setter
  18. }
  19. public class Student {
  20. private Long stId;
  21. private String stName;
  22. private String stEmail;
  23. private String clgId;
  24. public Student(Long stId, String stName, String stEmail, String clgId) {
  25. super();
  26. this.stId = stId;
  27. this.stName = stName;
  28. this.stEmail = stEmail;
  29. this.clgId = clgId;
  30. }
  31. // getter and setter
  32. }

字符串

  • 学院管理员 *
  1. @RestController
  2. @RequestMapping("/college")
  3. public class CollegeController {
  4. @Autowired
  5. private CollegeService collegeService;
  6. @Autowired
  7. private RestTemplate restTemplate;
  8. @RequestMapping(value = "/{clgId}", method = RequestMethod.GET)
  9. public ResponseEntity<College> getColleges(@PathVariable("clgId") String clgId) {
  10. College college = collegeService.getCollege(clgId);
  11. List student = restTemplate.getForObject("http://student-service/college/student/" + college.getClgId(),
  12. List.class);
  13. college.setStudents(student);
  14. return ResponseEntity.ok(college);
  15. }
  16. }

  • application.yml*
  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: college-service
  6. eureka:
  7. instance:
  8. hostname: localhost

学生服务

  • 学生控制器**实体 *
  1. public class Student {
  2. private Long stId;
  3. private String stName;
  4. private String stEmail;
  5. private String clgId;
  6. public Student(Long stId, String stName, String stEmail, String clgId) {
  7. super();
  8. this.stId = stId;
  9. this.stName = stName;
  10. this.stEmail = stEmail;
  11. this.clgId = clgId;
  12. }
  13. // getter and setter
  14. }
  1. @RestController
  2. @RequestMapping("/college")
  3. public class StudentCotroller {
  4. @Autowired
  5. private StudentService studentService;
  6. @RequestMapping(value = "/student/{clgId}", method = RequestMethod.GET)
  7. public ResponseEntity<List<Student>> getStudents(@PathVariable("clgId") String clgId) {
  8. return ResponseEntity.ok(studentService.getStudents(clgId));
  9. }
  10. }
  • application.yml*
  1. server:
  2. port: 9002
  3. spring:
  4. application:
  5. name: student-service
  6. eureka:
  7. instance:
  8. hostname: localhost

Eureka 服务器

  • application.yml*
  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. server:
  10. waitTimeInMsWhenSyncEmpty: 0

API网关服务

  • application.yml*
  1. server:
  2. port: 9003
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. spring:
  7. application:
  8. name: api-gateway
  9. cloud:
  10. gateway:
  11. routes:
  12. - id: college-service
  13. uri: lb://college-service
  14. predicates:
  15. - Path=/college/**
  16. - id: student-service
  17. uri: lb://student-service
  18. predicates:
  19. - Path=/student/**

slwdgvem

slwdgvem1#

您的student-service的 predicate 是匹配所有以student(Path=/student/**)开头的请求。然而,您正在使用以college(/college/student/CLG01)开头的请求调用student-service。此请求将与college-service匹配,因为您将此服务的 predicate 设置为Path=/college/**/college/**路径匹配/college/student/CLG01路径。
可能的解决方案:
1.将StudentController请求Map从/college更改为/student
1.为学生服务使用不同的 predicate ,例如Host
1.设置特定路径 predicate 并更改路由顺序:

  1. routes:
  2. - id: student-service
  3. uri: lb://student-service
  4. predicates:
  5. - Path=/college/student/**
  6. - id: college-service
  7. uri: lb://college-service
  8. predicates:
  9. - Path=/college/**

字符串

展开查看全部
t1rydlwq

t1rydlwq2#

我也遇到了同样的问题。在尝试了很多事情之后,我发现这个问题是API网关中的依赖问题。
这是不好的:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-gateway-mvc</artifactId>
  4. <version>4.1.0</version>
  5. </dependency>

字符串
这是正确的:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-gateway</artifactId>
  4. <version>4.1.0</version>
  5. </dependency>


注意不要有最后一个没有“-mvc”。

展开查看全部

相关问题