Elixir/Erlang在微服务方法中的位置是什么?[已关闭]

nsc4cvqm  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(164)

已关闭。此问题需要更多focused。当前不接受答案。
**想要改进此问题吗?**更新问题,使其仅关注editing this post的一个问题。

七年前就关门了。
Improve this question
最近,我一直在用docker compose做一些实验,以部署多个协作微服务。我可以看到微服务提供的许多好处,现在有了一个很好的工具集来管理它们,我认为跳进微服务的马车并不是非常困难。
但是,我也一直在试验Elixir,我非常喜欢它本身提供的好处。考虑到它鼓励将代码打包到多个解耦的应用程序中,并支持热代码升级,您将如何混合Docker和Elixir(或Erlang,就此而言)?
例如,如果我想使用docker,因为它提供dev-prod奇偶校验,那么elixir如何适应它呢?由于docker容器是不可变的,我失去了进行热代码升级的能力,对吗?蓝/绿色部署或canary版本呢?
我的意思是,我可以用Elixir编写微服务,并像使用其他语言一样使用它们,多语制是微服务的好处之一,但我没有得到使用OTP平台的全部好处,我想纯协作的Erlang应用程序比使用中间队列在使用不同(或非)语言编写的微服务之间进行通信要优化得多。

8zzbczxx

8zzbczxx1#

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 Open Telecom Platform (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:
  4. how their code is started and stopped
  5. what are the processes that make part of an application and therefore what is the application state
  6. 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. :)

相关问题