English 中文(简体)
春云: 不工作的网关
原标题:Spring Cloud : API Gateway routing not working

我正与山云合作。 我的春boot应用有四处服务:college-service,student-service,eureka-serverapi-gateway。 我正试图打电话到college-servicestudent-service。 虽然Im打着My college-service,但从APIC/ 工作罚款但<代码>student-service的网关没有工作。 虽然我试图获得<代码>student-serivice,但我发现有错误404。 我在春云中翻一番。 下面是我的法典。

www.un.org/Depts/DGACM/index_russian.htm 在上次午餐时,我获得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

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

Student Service

Student Controller

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/**
最佳回答

您的<代码>student-service的序号,与学生开始的所有申请相匹配()。 Path=/student/**。 不过,请打电话student-service,要求从学院开始(/college/student/CLG01)。 这项请求将与<编码>college-service相匹配。 自您确定这一服务的前提为<代码> Path=/college/**。 www.un.org/chinese/ga/president

可能的解决办法:

  1. Change your StudentController request mapping from /college to /student
  2. Use different predicate for your student service, such as Host
  3. 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/**
问题回答

我有同样的问题。 在试图做很多事情之后,我发现,这一问题在APIC关口是一个依赖问题。

This is bad:

<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>

Note don t had the last one dosen t have the "-mvc".

In your build.gradle file change the dependency from

implementation  org.springframework.cloud:spring-cloud-starter-gateway-mvc 

纽约总部

implementation  org.springframework.cloud:spring-cloud-starter-gateway 




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签