din_S

din_S

Running different instances of one application on multiple ports using libcluster

I want to run different instances of the same app (not copies of the same app) on the same server (clustering) using different ports, so I used libcluster library and did the following (in application.ex):

def start(_type, _args) do
    import Supervisor.Spec
    # Define Topologies for clustering
    topologies = [
      example: [
        strategy: Cluster.Strategy.Gossip
      ]
    ]
    # List all child processes to be supervised
    children = [
      # For Clustering
      {Cluster.Supervisor, [topologies, [name: MyApp.ClusterSupervisor]]},
      # Start the endpoint when the application starts
      supervisor(MyAppWeb.Endpoint, []),
      {ConCache, [name: :my_cache, ttl_check_interval: false]}
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    result = Supervisor.start_link(children, opts)
end

I even tried this:

topologies = [
      example: [
        strategy: Cluster.Strategy.Gossip,
        config: [hosts: [:"a@127.0.0.1", :"b@127.0.0.1",:"b@127.0.0.1",:"d@127.0.0.1"]],
      ]
    ]

but when I tried the following on the server (Ubuntu 18):

 PORT=3000 elixir --sname a -S mix phx.server

then

 PORT=3001 elixir --sname b -S mix phx.server

I get the following error

** (Mix) Could not start application my_app: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: MyAppWeb.Endpoint
    ** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, MyAppWeb.Endpoint.HTTP}
        ** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
            ** (EXIT) {:listen_error, MyAppWeb.Endpoint.HTTP, :eaddrinuse}


what should I do to make it work.

Marked As Solved

kokolegorille

kokolegorille

I have tested this on a Phoenix without libcluster and it works for me.

I also have a libcluster, swarm project, but it uses Cowboy instead of Phoenix.

This might not help You, but I use Epmd strategy, and my config is standard to libcluster doc.

config :libcluster, topologies: [
  example_1: [
    strategy: Cluster.Strategy.Epmd,
    config: [hosts: [
      :node1@localhost,
      :node2@localhost,
      :node3@localhost,
    ]],
  ],

  example_2: [
    strategy: Cluster.Strategy.Gossip,
  ]
]

and I start nodes like this

$ iex --sname node1@localhost -S mix
$ PORT=4001 iex --sname node2@localhost -S mix
$ PORT=4002 iex --sname node3@localhost -S mix

It works, but this is not using Phoenix, but Cowboy.

Last Post!

Mack_Akdas

Mack_Akdas

Hi Din_s,
I see a replication, maybe irrelevant to what you are looking for, but this should not be there, shouldn’t that not be a ‘c’ ?

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement