从Docker中的另一个微服务调用微服务 [英] Call Microservice from another Microservice within Docker

查看:691
本文介绍了从Docker中的另一个微服务调用微服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows中的docker上运行的C#中创建了多个微服务,我需要从另一个微服务调用微服务,所以我使用这种方式进行调用:

I created several Microservices in C# that are running on docker in windows, I need to call Microservice from another Microservice so I used this way to call:

    [HttpGet("GetOrder/{Object_ID}")]
    public Order GetOrder (int id)
    {
        string Baseurl = "http://189.29.0.100/";
        …..

        using (var client = new HttpClient())
        {
            //Passing service base url  
            client.BaseAddress = new Uri(Baseurl);

            client.DefaultRequestHeaders.Clear();
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
            borrowerData = await client.GetStringAsync("api/order/" + Id.ToString());

        }

       …
    }

我在组合文件中使用了修复IP,如下所示:

I used the fix IP in Composed file as follows:

 orderservice:
    environment:
     - ASPNETCORE_ENVIRONMENT=Development
   ports:
  - "80"
networks:
  default:
    ipv4_address: 189.29.0.100

问题是当我们在VM中部署此项目时,如何使其与这些Ips一起工作?

The problem is when we deploy this project in VM, how to make it work with these Ips?

推荐答案

Docker会在您的主机上创建一个虚拟网络,并且无法使用这些IP从Docker外部路由服务.但是在虚拟网络内部,所有服务都可以使用其名称(http://otherservice/)相互访问. 如果要访问VM上所有已部署的微服务,则需要在docker compose内部使用一个反向代理.例如,将请求分发给其他服务的Nginx服务.

Docker compose create a virtual network on you host and the services are not routable from outside of docker with those IPs. But inside the virtual network all services can access to each other with their names (http://otherservice/). If you want to access to all deployed microservices on VM you will need a reverse proxy inside your docker compose. For example an nginx service that dispatch request to other services.

这篇关于从Docker中的另一个微服务调用微服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆