Configuración de aplicaciones


@palmerabollo · 2015

Japanese spider crab (高脚蟹)

Hardcoded

var http = require('http');

var port = 9000;

http.createServer(function (req, res) {
  res.write('Hello World ' + port);
  res.end();
}).listen(port);

Env

var http = require('http');

var port = process.env.PORT;

http.createServer(function (req, res) {
  res.write('Hello World ' + port);
  res.end();
}).listen(port);

Argv

var http = require('http');

var port = process.argv[2];

http.createServer(function (req, res) {
  res.write('Hello World ' + port);
  res.end();
}).listen(port);

Fichero

var http = require('http'),
    fs = require('fs');

var config = JSON.parse(fs.readFileSync('config.json'));

var port = config.port;

http.createServer(function (req, res) {
  res.write('Hello World ' + port);
  res.end();
}).listen(port);

Librerías: nconf, convict (node.js)

Configuration Server

curl -s http://10.95.213.90:8000/port
var http = require('http'),
    ConfigurationResolver = require('rest-confidence-client');

var resolver = new ConfigurationResolver('http://localhost:8000');

resolver.load('port', function(err, port) {
  if (err) {
    process.exit(1);
  }

  http.createServer(function (req, res) {
    res.write('Hello World ' + port);
    res.end();
  }).listen(port);
})

Configuration Server

Almacén Clave-Valor Distribuido


consul · consul.io

etcd · coreos.com/etcd

CAP

curl -s http://10.95.213.90:8500/v1/kv/port | jq
curl -X PUT http://10.95.213.90:8500/v1/kv/port -d '9000'
var http = require('http'),
    Consul = require('consul-node');

var consul = new Consul({host: 'localhost', port: 8300});

consul.kv.get('port', function(err, port) {
  if (err) {
    process.exit(1);
  }

  http.createServer(function (req, res) {
    res.write('Hello World ' + port);
    res.end();
  }).listen(port);
})

Lo mejor de los dos mundos (I)

var http = require('http');

var port = process.env.PORT || 9000;

console.log('server start with port %d', port);

http.createServer(function (req, res) {
  res.write('Hello World ' + port);
  res.end();
}).listen(port);
./envconsul -consul=10.95.213.90:8500 -prefix config node server-env.js

Lo mejor de los dos mundos (II)

./consul-template -consul 10.95.213.90:8500 -template "demo.consul.tmpl:demo.conf:cat demo.conf"
  • consul-template corre como servicio
  • reinicia otros servicios al detectar cambios
  • la aplicación no cambia

Vault

Almacén de secretos

Debate

Manage Configuration

By Guido García

Manage Configuration

Architecture Community. Telefónica I+D

  • 1,077