I am trying to set up routing in Gateway in the properties I prescribe the address and where to redirect, I can not understand the logic of the work.

server: port: 8081 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka spring: application: name: gateway cloud: gateway: discovery: locator: enabled: true routes: - id: servicetest uri: http://localhost:8080/ predicates: - Path=/services/** - id: service-student uri: http://localhost:8082/ predicates: - Path=/** 

ServiceTest

  spring.application.name=ServiceTest server.port=8080 

@RestController public class TestController {

 @GetMapping("/hello") public String hello(){ return "Controller is work from " + this.getClass().getName().toString(); } 

}

service-student

 spring.datasource.url=jdbc:postgresql://localhost:5432/spring spring.datasource.username=postgres spring.datasource.password=root spring.jpa.generate-ddl=true spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true server.port=8082 spring.application.name=service-student eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka @Slf4j @RestController @RequestMapping("/") public class TeacherController { @Autowired TeacherRepository teacherRepository; @GetMapping("/teachers") public ResponseEntity<List<Teacher>> findAllTeachers(){ List<Teacher> groups = teacherRepository.findAll(); if(groups.isEmpty()){ return new ResponseEntity<>(groups, HttpStatus.NO_CONTENT); } return new ResponseEntity<>(groups,HttpStatus.OK); } 

When accessing via http://localhost:8081/teachers , everything is OK, JSON is returned from the desired service (id service-student). But if you do Path=/services/** and try to reach http://localhost:8081/services/hello is no access. How to create a config?

  • And service on 8080 is lifted? - Chubatiy
  • localhost: 8080 / services / hello returns something? Also try to temporarily remove the second route (id service-student) - Chubatiy
  • Well, that is, the problem is solved? And then many bukaf)) And better give the code in question (code mapping controllers with ports) Judging by the second controller you do not have enough mapping for /services/ . If you do not need it on the second service, then you should use the rewritepath filter - Chubatiy

1 answer 1

In general, if we have two requests

http: // localhost: 8081 / teachers

http: // localhost: 8081 / services / hello

And two services, respectively

http: // localhost: 8082 / teachers

http: // localhost: 8080 / hello

That setting will look like this

 server: port: 8081 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka spring: application: name: gateway cloud: gateway: discovery: locator: enabled: true routes: - id: servicetest uri: http://localhost:8080/ predicates: - Path=/services/{segment} filters: - SetPath=/{segment} - id: service-student uri: http://localhost:8082/ predicates: - Path=/** 

The trick is to substitute the path through the filter SetPath

Or you can not change the setting, but then change the mapping in the service by adding the @RequestMapping("/services") annotation to the class