Spring Cloud:API Gateway routing not working

8iwquhpp  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(63)

我正在使用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|

学院服务

  • 实体 *
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/**

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 并更改路由顺序:

routes:
      - id: student-service
        uri: lb://student-service
        predicates:
        - Path=/college/student/**
      - id: college-service
        uri: lb://college-service
        predicates:
        - Path=/college/**

字符串

t1rydlwq

t1rydlwq2#

我也遇到了同样的问题。在尝试了很多事情之后,我发现这个问题是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”。

相关问题