You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/elixir/pages/mix-and-otp/config-and-distribution.md
+8-9Lines changed: 8 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,7 @@ Run `iex -S mix` and you will see the following message printed:
62
62
63
63
Run tests, without killing the development server, and you will see it running on port 4040.
64
64
65
-
Our change was straight-forward. We used `Application.fetch_env!/2` to read the entry for `port` in `:kv`'s environment. We explicitly used `fetch_env!/2` (instead of `get_env/2` or `fetch_env`) because it will raise if the port wasn not configured (therefore our app wouldn't even boot).
65
+
Our change was straight-forward. We used `Application.fetch_env!/2` to read the entry for `port` in `:kv`'s environment. We explicitly used `fetch_env!/2` (instead of `get_env/2` or `fetch_env`) because it will raise if the port was not configured (preventing the app from booting).
66
66
67
67
## Compile vs runtime configuration
68
68
@@ -143,7 +143,7 @@ iex> flush()
143
143
:ok
144
144
```
145
145
146
-
In other words, we can spawn processes in other nodes, hold to their PIDs, and then send messages to them as if they were running on the same machine. That's the *location transparency* principle. And because everything we have built so far was built on top of messaging passing, we should be able to adjust our key-value store to become a distributed one with little work.
146
+
In other words, we can spawn processes in other nodes, hold onto their PIDs, and then send messages to them as if they were running on the same machine. That's the *location transparency* principle. And because everything we have built so far was built on top of messaging passing, we should be able to adjust our key-value store to become a distributed one with little work.
It returns `nil`. However, if you run `KV.lookup_bucket("shopping")` in `bar@computer-name`, it will return the proper bucket. In other words, the nodes can communicate with each other, but buckets spawned in one node are not visibly to the other.
178
+
It returns `nil`. However, if you run `KV.lookup_bucket("shopping")` in `bar@computer-name`, it will return the proper bucket. In other words, the nodes can communicate with each other, but buckets spawned in one node are not visible to the other.
179
179
180
180
This is because we are using [Elixir's Registry](`Registry`) to name our buckets, which is a **local** process registry. In other words, it is designed for processes running on a single node and not for distribution.
181
181
182
-
Luckily, Erlang ships with a distributed registry called [`:global`](`:global`), which is support by default as a naming registry. All we need to do is to replace the `via/1` function in `lib/kv.ex` from this:
182
+
Luckily, Erlang ships with a distributed registry called [`:global`](`:global`), which is support by default as a naming registry. All we need to do is replace the `via/1` function in `lib/kv.ex` from this:
And there you go! By simply changing which naming registry we used, we now have a distributed key value store. You can even try using `telnet` to configure to the different ports and validate that changes in one session are visible to other one. Exciting!
203
+
And there you go! By simply changing which naming registry we used, we now have a distributed key value store. You can even try using `telnet` to connect to the servers on different ports and validate that changes in one session are visible in the other one. Exciting!
204
204
205
205
## Node discovery and dependencies
206
206
207
-
There is one essential ingredient to wrap up our distributed key-value store. In other for the `:global` registry to work, we need to make sure the nodes are connected to each other. When we run `:erpc` call passing the node name:
207
+
There is one essential ingredient to wrap up our distributed key-value store. In order for the `:global` registry to work, we need to make sure the nodes are connected to each other. When we run `:erpc` call passing the node name:
@@ -260,14 +260,13 @@ Open up `config/runtime.exs` and add this to the bottom:
260
260
```elixir
261
261
nodes =
262
262
System.get_env("NODES", "")
263
-
|>String.split(",")
264
-
|>Enum.reject(&&1=="")
263
+
|>String.split(",", trim:true)
265
264
|>Enum.map(&String.to_atom/1)
266
265
267
266
config :kv, :nodes, nodes
268
267
```
269
268
270
-
We fetch the environment variable, split it on ",", discard all empty strings, and then convert each entry to an atom, as node names are atoms.
269
+
We fetch the environment variable, split it on "," while discarding all empty strings, and then convert each entry to an atom, as node names are atoms.
271
270
272
271
Now, in your `start/2` callback, we will add this to of the `start/2` function:
0 commit comments