I am working with Spring Cloud. I have four services in my spring boot application college-service
, student-service
, eureka-server
and api-gateway
. I am trying to call college-service
and student-service
with API Gateway. While I'm calling My college-service
from API Gateway that's work fine but student-service
is not working. While I am trying to get student-serivice
, I get error 404
not found. I'm new guy in Spring Cloud. Here down is my code.
Here down is my urls, Where in last url I get 404
URL | Status |
---|---|
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 |
Colleger Service
Entity
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
}
College 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
Student Controller Entity
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 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/**
CodePudding user response:
Predicate for your student-service
is to match all the requests that start with student(Path=/student/**
). However you are you calling the student-service
with the request that starts with college(/college/student/CLG01
). This request will me matched to college-service
since you set the predicate for this service as Path=/college/**
. /college/**
path matches /college/student/CLG01
path.
Possible solutions:
- Change your
StudentController
request mapping from/college
to/student
- Use different predicate for your student service, such as
Host
- Set specific path predicate and change the order of routes:
routes:
- id: student-service
uri: lb://student-service
predicates:
- Path=/college/student/**
- id: college-service
uri: lb://college-service
predicates:
- Path=/college/**