我正在使用Spring Cloud。我的Sping 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*
| 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|
学院服务
- 实体 *
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
型
Eureka 服务器
- application.yml*
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
型
API网关服务
- 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/**
型
2条答案
按热度按时间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 并更改路由顺序:
字符串
t1rydlwq2#
我也遇到了同样的问题。在尝试了很多事情之后,我发现这个问题是API网关中的依赖问题。
这是不好的:
字符串
这是正确的:
型
注意不要有最后一个没有“-mvc”。