Open DAVIAMERICO242 opened 2 weeks ago
Solved using:
@Configuration
public class Balancer {
@Autowired
private AssinanteRepository assinanteRepository;
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("dynamic_route", r -> r.path("/**")
.filters(f ->{
// GETTING 'CNPJ' FROM COOKIE/HEADER TO MATCH PORT IN REPOSITORY
f.changeRequestUri(e -> {
HttpCookie cnpjCookie = e.getRequest().getCookies().getFirst("cnpj");
String cnpj;
if(cnpjCookie!=null){
cnpj = cnpjCookie.getValue();
}else{
cnpj = e.getRequest().getHeaders().getFirst("cnpj");
}
Integer port = assinanteRepository.findById(cnpj).get().getApiPORT();
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUri(e.getRequest().getURI());
String modifiedUri = uriBuilder.scheme("http").host("localhost").port(port).toUriString();
return Optional.of(URI.create(modifiedUri));
});
return f;
}).uri("http://localhost:9998")
).build();
//
}
}
Describe the bug I need to redirect URI port based on some repository, the repository works well, and it's not the problem, the problem is the default uri
http://localhost:9998
is always being fallen in, even ifportaBackend
is not null and differente from 9998, I tried so many ways and it's still getting the wrong doorSample