Reputation: 45
I have two microservices and I want that one consumes of the other but I'm obtaining this mistake:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/testMicroservicio": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)] with root cause
java.net.ConnectException: Connection refused (Connection refused)
However if I execute the url in the browser, it works perfectly but if a microservice wants to access to the other microservice, I have this mistake.
Does someone kown why?
I'm consuming with: RestTemplate
I put some of code:
@RestController
public class MicroServiceController {
private final AddressService service;
private static final String URL_API_INFO = "http://localhost:8080/testMicroservicio";
private RestTemplate restTemplate = new RestTemplate();
private final static Logger log = Logger.getLogger("com.bernanetwork.web.controller.MicroServiceController");
@Autowired
public MicroServiceController(AddressService service) {
this.service = service;
}
@RequestMapping(value = "/micro-service-test")
public String consumidor() throws Exception {
log.info("----------------------------------------------------------------------------------------");
log.info("-------------------------Iniciando método consumidor------------------------------------");
log.info("----------------------------------------------------------------------------------------");
ResponseEntity <PruebasMicroservicio[]> response = restTemplate.getForObject(URL_API_INFO, PruebasMicroservicio[].class);
Arrays.asList(response.getBody()).forEach(info -> log.info("---"+info));
return "ok";
}
These microservices are running in Docker
Thanks so much.
Upvotes: 2
Views: 8450
Reputation: 51876
The problem is you are trying to connect from one service to the other using localhost
. This won't work as each container has it own IP and localhost will just point back to the caller of the request.
The stardard Docker way to connect containers, is to connect them to a Docker network.
docker network create mynet
docker run --network mynet --name container-1 ...
docker run --network mynet --name container-2 ...
Now container1 can communicate with container2 using http://container-2:8080
.
Upvotes: 8