Reputation: 61
I think I got a simple question and have not found a solution on the web yet. I have 1 project with multiple solutions in it. I have put them all into a docker compose file. Each service has his own port: http://localhost:8081, http://localhost:8082 and so on...
I have service A on port http://localhost:8081 and service B on port http://localhost:8082.
I try to call an endpoint in service B from service A with HttpClient. The default way of making a Rest call. I call http://localhost:8081/ServiceA/testcall.
I tried to call an external API : https://jsonplaceholder.typicode.com/posts and this works. But when I try to call my own endpoint I get the following error:
Internal Server Error 500
I debugged it but do not get a more detailed message.
I tried:
SERVICE A CONTROLLER
namespace A_service.Controllers
{
[Route("[controller]")]
[ApiController]
public class ServiceAController : ControllerBase
{
private readonly IServiceARepository _serviceARepository;
public ServiceAController(IServiceARepository serviceARepository)
{
_serviceARepository= serviceARepository;
}
[HttpGet]
public async Task<IActionResult> JustASimpleGetCall()
{
// This call works fine
return("Hello World");
}
[HttpGet("testcall")]
public async Task<IActionResult> GetSomethingFromOtherService()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://service-b:8080/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("ServiceB/simplefunction");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
}
return Ok("Succesful");
}
SERVICE B CONTROLLER
namespace B_service.Controllers
{
[Route("[controller]")]
[ApiController]
public class ServiceBController : ControllerBase
{
private readonly IServiceBRepository _serviceBRepository;
public ServiceBController(IServiceBRepository serviceBRepository)
{
_serviceBRepository= serviceBRepository;
}
[HttpGet("simplefunction")]
public IActionResult ServiceBFunction()
{
// To simplify, we just return a
// Declaration of the array
string[] str1;
// Initialization of array
str1 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };
return Ok(str1);
}
version: '3.0'
services:
serviceA:
build:
context: .
dockerfile: serviceA/Dockerfile
ports:
- "8081:80"
serviceB:
build:
context: .
dockerfile: serviceB/Dockerfile
environment:
- SERVER_URL=http://service-b:8080
ports:
- "8082:80"
Upvotes: 0
Views: 1575
Reputation: 427
if you want to get to serviceB from serviceA the url is http://serviceB:(application port) it seems that your application goes on port 80 (at least that is what written inside the docker-compose.yml). if you can get to serviceB from your computer like localhost:8082 that means that the port is configured to 80. if the port in the application is configured to 8080 that means the port you'll need to write is 8080.
Upvotes: 1