Пытаюсь настроить роутинг в Gateway в properties прописываю адрес и куда перенаправлять, не могу понять логику работы.
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);
}
При доступе через http://localhost:8081/teachers
, все ок, возвращается JSON с нужного сервиса(id service-student). А вот если делать Path=/services/**
и пробовать достучаться http://localhost:8081/services/hello
доступа нет. Как правильно создать конфиг?
/services/
. Если вам он не нужен на втором сервисе - то вы должны использовать фильтрrewritepath