Elixir / erlang适用于微服务方法? [英] Where does Elixir/erlang fit into the microservices approach?

查看:285
本文介绍了Elixir / erlang适用于微服务方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在做dockor组合的实验,以便部署多个协作的微服务器。我可以看到微服务提供的许多好处,现在有一个很好的工具来管理它们,我认为这不是很难跳入微型服务车。





例如,如果我想使用码头工具,因为它提供了开发平价,那么酏剂如何适应?鉴于docker容器是不可变的,我失去了进行热代码升级的能力,对吧?我的意思是说,我可以用Elixir编写微服务器,并使用它们,就像用任何其他语言一样,多语言是无论如何,微服务的好处之一是,但是我并没有获得使用OTP平台的全部优势,所以我认为纯合作的erlang应用程序是更为优化的方式,使用中间队列在不同的(或不是)

解决方案

这是一个非常开放的问题,但我会尝试说明为什么Elixir / Erlang可能是最好的平台用于开发分布式系统(无论您是否使用微服务器)。



首先,我们从一些背景开始。 Erlang VM及其标准库是为构建分布式系统而设计的,这一点真的出现了。据我所知,这是用于此用例的唯一运行时间和VM的生产设计。



应用程序



例如,您已经暗示了应用程序。在Erlang / Elixir中,代码包装在应用程序内,其中:


  1. 以单位启动和停止。启动和停止系统是一个启动所有应用程序的问题。

  2. 提供统一的目录结构和配置API(不是XML!)。如果您已经使用并配置了OTP应用程序,那么您将了解如何使用任何其他应用程序。

  3. 包含您的应用程序监督树,所有进程(按进程我的意思是VM进程这是计算的轻量级线程)和他们的状态

这种设计的影响是巨大的。这意味着Elixir开发人员在编写应用程序时有更明确的方法:


  1. 他们的代码如何启动和停止

  2. 作为应用程序的一部分的进程是什么,因此什么是应用程序状态

  3. 这些进程将如何反应并在发生崩溃或发生事件时受到影响错误

不仅如此,这种抽象的工具是伟大的。如果您安装了Elixir,请打开iex并输入::observer.start()。除了显示有关实时系统的信息和图表外,您还可以杀死随机进程,查看其内存使用情况,状态等。以下是在Phoenix应用程序中运行此示例:





这里的区别在于应用程序和进程为您提供了一个抽象来说明您的生产代码 。许多语言提供了主要用于代码组织的包,对象和模块,而不会在运行时系统上反映。如果你有一个类属性或一个单例对象:你如何理解可以操纵它的实体?如果您有内存泄漏或瓶颈,您如何找到负责该实体的实体?



如果您询问运行分布式系统的人员,那就是一种洞察力他们想要和Erlang / Elixir一起作为构建块。



通讯



所有这一切只是开始真的构建分布式系统时,需要选择通信协议和数据串行器。很多人选择HTTP和JSON,当你想到它,是一个非常冗长和昂贵的组合,用于执行真正的RPC调用。



使用Erlang / Elixir你已经有一个通讯协议和序列化机制开箱即用。如果你想让两个机器相互通信,你只需要给他们名字,确保他们有相同的秘密,而且你已经完成了。



Jamie在Erlang Factory 2015上谈到了这一点,他们如何利用这个来构建游戏平台: https://www.youtube.com/watch?v=_i6n-eWiVn4



如果你想使用HTTP和JSON,那么这样很好,像Phoenix这样的插件和框架这样的图书馆也将保证你在这方面也很有成效。



Microservices



到目前为止,我还没有谈到微服务器。那是因为到目前为止,他们并不重要。您已经在孤立的非常微小的进程周围设计系统和节点。如果您愿意,请致电他们nanoservices!



不仅如此,它们也被打包到应用程序中,它们将它们组合为可以作为单元启动和停止的实体。如果您有应用程序A,B和C,然后要将它们部署为[A,B] + [C]或[A] + [B] + [C],那么这样做很少有麻烦以其固有的设计。或者更好的是,如果您想避免将微服务部署的复杂性添加到系统中,您可以在同一个节点中部署它们。



而且,在一天结束,如果您使用Erlang分布式协议运行所有这些,您可以在不同的节点中运行它们,只要您通过 {: node @ network,:name} 而不是:name



我可以去此外,我希望我在此确信你。 :)


Lately I've been doing some experiments with docker compose in order to deploy multiple collaborating microservices. I can see the many benefits that microservices provide, and now that there is a good toolset for managing them, I think that it's not extremely hard to jump into the microservices wagon.

But, I have been experimenting with Elixir too, and I am quite fond of the benefits that provides by itself. Given that it encourages packing your code into multiple decoupled applications, and supports hot code upgrades, how would you mix docker with elixir (or erlang, for that matter)?

For example, if I want to use docker because it provides dev-prod parity, how does elixir fit in that? Given that docker containers are immutable, I lose the ability to do hot-code upgrades, right? What about blue/green deployments or canary releases?

I mean, I could just write microservices with Elixir and use them as if they were written in any other language, polyglotism is one of the benefits of microservices anyway, but then I'm not getting the full benefits of using the OTP platform, I guess that pure collaborative erlang applications are way more optimal that using intermediate queues to communicate between microservices written in different (or not) languages.

解决方案

This is a very open question but I will try to illustrate why Elixir/Erlang may be the best platform out there for developing distributed systems (regardless if you are working with microservices).

First, let's start with some background. The Erlang VM and its standard library were designed upfront for building distributed systems and this really shows up. As far as I know, it is the only runtime and VM used widely in production designed upfront for this use case.

Applications

For example, you have already hinted at "applications". In Erlang/Elixir, code is packaged inside applications which:

  1. are started and stopped as unit. Starting and stopping your system is a matter of starting all applications in it
  2. provide a unified directory structure and configuration API (which is not XML!). If you have already worked with and configured an OTP application, you know how to work with any other one
  3. contains your application supervision tree, with all processes (by process I mean "VM processes" which are lightweight threads of computation) and their state

The impact of this design is huge. It means that Elixir developers, when writing applications have a more explicit approach to:

  1. how their code is started and stopped
  2. what are the processes that make part of an application and therefore what is the application state
  3. how those process will react and be affected in case of crashes or when something goes wrong

Not only that, the tooling around this abstraction is great. If you have Elixir installed, open up "iex" and type: :observer.start(). Besides showing information and graphs about your live system, you can kill random processes, see their memory usage, state and more. Here is an example of running this in a Phoenix application:

The difference here is that Applications and Processes give you an abstraction to reason about your code in production. Many languages provides packages, objects and modules mostly for code organization with no reflection on the runtime system. If you have a class attribute or a singleton object: how can you reason about the entities that can manipulate it? If you have a memory leak or a bottleneck, how can you find the entity responsible for it?

If you ask anyone running a distributed system, that's the kind of insight that they want, and with Erlang/Elixir you have that as the building block.

Communication

All of this is just the beginning really. When building a distributed system, you need to choose a communication protocol and the data serializer. A lot of people choose HTTP and JSON which, when you think about it, is a very verbose and expensive combination for performing what is really RPC calls.

With Erlang/Elixir, you already have a communication protocol and a serialization mechanism out of the box. If you want to have two machines communicating with each other, you only need to give them names, ensure they have the same secret, and you are done.

Jamie talked about this at Erlang Factory 2015 and how they were able to leverage this to build a game platform: https://www.youtube.com/watch?v=_i6n-eWiVn4

If you want to use HTTP and JSON, that is fine too and libraries like Plug and frameworks like Phoenix will guarantee you are productive here too.

Microservices

So far I haven't talked about microservices. That's because, up to this point, they don't really matter. You are already designing your system and nodes around very tiny processes that are isolated. Call them nanoservices if you'd like to!

Not only that, they are also packaged into applications, which group them as entities that can be started and stopped as unit. If you have applications A, B and C, and then you want to deploy them as [A, B] + [C] or [A] + [B] + [C], you will have very little trouble in doing so due to their inherent design. Or, even better, if you want to avoid adding the complexity of microservices deployments into your system upfront, you can just deploy them altogether in the same node.

And, at the end of the day, if you are running all of this using the Erlang Distributed Protocol, you can run them in different nodes and they will be able to reach other as long as you refer to them by {:node@network, :name} instead of :name.

I could go further but I hope I have convinced you at this point. :)

这篇关于Elixir / erlang适用于微服务方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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