我正在使用 Spring Cloud。我的 Spring Boot 应用程序中有四个服务
college-service
、student-service
、eureka-server
和 api-gateway
。我正在尝试使用 API Gateway 调用 college-service
和 student-service
。当我从 API Gateway 调用 My college-service
时,它工作正常,但 student-service
不起作用。当我尝试获取 student-serivice
时,出现错误 404
未找到。我是 Spring Cloud 的新人。这是我的代码。
下面是我的网址,在最后一个网址中我得到 404
网址 | 状态 |
---|---|
http://localhost:9001/college/CLG01 | 200 可以 |
http://localhost:9002/学院/学生/CLG01 | 200 可以 |
http://localhost:9003/college/CLG01 | 200 可以 |
http://localhost:9003/学院/学生/CLG01 | 400 未找到 |
实体
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
}
学院控制器
@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
学生控制器 实体
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
}
@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
application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
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/**
student-service
的谓词是匹配所有以student(Path=/student/**
)开头的请求。但是,您致电 student-service
并提出以大学开头的请求(/college/student/CLG01
)。由于您将此服务的谓词设置为 college-service
,因此该请求将与 Path=/college/**
匹配。 /college/**
路径与 /college/student/CLG01
路径匹配。
可能的解决方案:
StudentController
请求映射从 /college
更改为 /student
Host
routes:
- id: student-service
uri: lb://student-service
predicates:
- Path=/college/student/**
- id: college-service
uri: lb://college-service
predicates:
- Path=/college/**
我也有同样的问题。经过尝试很多事情后,我发现问题是 API 网关中的依赖问题。
这很糟糕:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway-mvc</artifactId>
<version>4.1.0</version>
</dependency>
这是正确的:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>4.1.0</version>
</dependency>
注意最后一个没有“-mvc”。