When using Spring MVC and Spring Cloud Eureka, how can I get Spring MVC to resolve my redirect URLs similar to how RestTemplate is able to?
For example, if I have two services, user-service and movie-service that are both registered with Spring Cloud Eureka, how can I get a controller in user-service to redirect to a movie-service endpoint, without hard coding the url?
@Controller
public class UserServiceController {
@PostMapping("/something")
public String performSomeAction() {
// I'd like movie service to be resolved to an instance that is registered with Eureka
return "redirect:http://movie-service/some-url
}
}
Is what I'm trying to do possible? I know Spring cloud is normally used with APIs, but to me it seems like it would make sense to be able to integrate your web applications with it as well.
@Controller
public class UserServiceController {
@Autowired
LoadBalancerClient loadBalancerClient;
@PostMapping("/something")
public String performSomeAction() {
ServiceInstance serviceInstance = loadBalancerClient.choose("movie-service");
if (serviceInstance != null) {
return "redirect:http://"+ serviceInstance.getHost() + ":" + serviceInstance.getPort() +"/some-url
}
// throw an error or something else
}
}