Slides from our talk "Service discovery and configuration provisioning" presented by Mariusz Gil at PHP Benelux 2016
Apache Zookeeper or Consul are almost completely unknown in the PHP world, although its use solves a lot of typical problems. In a nutshell, they are a central services of provisioning configuration information, distributed synchronization and coordination of servers/processes. It simplifies the processes of application configuration management, so it is possible to change its settings and operation in real time (eg. feature flagging). During the presentation the typical cases of use of Zookeeper/Consul in PHP applications will be presented, both strictly web and workers running from the CLI.
11. <?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'northwind');
define('DB_USER', 'root');
define('DB_PASS', 'your_password');
// This connection allows your application to be used for multi-lingual websites.
function _connect_to_mysql($is_utf8 = null)
{
$mysql_link = mysql_connect(DB_HOST, DB_USER, DB_PASS)
or die("Could not connect to database server");
mysql_select_db(DB_NAME, $mysql_link)
or die("Could not select database");
if (is_null($is_utf8))
{
/* This sets collation for the connection to utf8_general_ci
because it is the default collation for utf8.
This enables multi-lingual capability in database.
*/
mysql_query("SET NAMES 'utf8'");
}
return $mysql_link;
}
12. <?php
// ...
// ** MySQL settings - You can get this info from your web host ** //
define('DB_NAME', 'database');
/** MySQL database username */
define('DB_USER', 'username');
/** MySQL database password */
define('DB_PASSWORD', 'password');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
13. <?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* @link https://codex.wordpress.org/Editing_wp-config.php
* @package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
define('DB_NAME', 'database');
/** MySQL database username */
define('DB_USER', 'username');
/** MySQL database password */
define('DB_PASSWORD', 'password');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
16. <?php
use PimpleContainer;
$container = new Container();
$container['db'] = function() {
$host = 'localhost';
$dbName = 'wordpress';
$user = 'root';
$pass = '';
return new PDO("mysql:host={$host};dbname={$dbName}", $user, $pass);
};
17. <?php
$dice = new DiceDice();
$rule = [
//Mark the class as shared so the same instance is returned each time
'shared' => true,
//The constructor arguments that will be supplied when the instance is created
'constructParams' => [
'mysql:host=127.0.0.1;dbname=mydb',
'username',
'password',
],
];
//Apply the rule to the PDO class
$dice->addRule('PDO', $rule);
//Now any time PDO is requested from Dice, the same instance will be returned
//And will have been constructed with the arguments supplied in 'constructParams'
$pdo = $dice->create('PDO');
18. <?php
$builder = new DIContainerBuilder();
$builder->addDefinitions([
'foo' => 'hello ',
'bar' => 'world',
]);
$builder->addDefinitions([
'foo' => DIdecorate(function ($previous, InteropContainerContainerInterface $container) {
return $previous . $container->get('bar');
}),
]);
$builder->addDefinitions([
'values' => [
'value 1',
'value 2',
],
]);
$builder->addDefinitions([
// with the same name
'stdClass' => DIobject('stdClass'),
// with name inferred
__NAMESPACE__ . 'ObjectDefinitionClass1' => DIobject(),
// with a different name
'object' => DIobject(__NAMESPACE__ . 'ObjectDefinitionClass1'),
]);
$container = $builder->build();
22. Is captcha feature enabled?
Is remember-be feature enabled?
Is remind-me feature enabled?
Is 2-factor authentication enabled?
Is brute-force protection enabled?
How many attempts per login allowed?
What is delay between failed logins?
Is hide-errors protection enabled?
…
Is login feature enabled at all?
23. What is database host?
What is database port?
What is database user?
What is database password?
What is database name?
What is database encoding?
Is database cache enabled?
What is cache host?
What is cache port?
Where the sessions are stored?
57. <?php
require '../vendor/autoload.php';
$sf = new SensioLabsConsulServiceFactory(array('base_url' => 'http://192.168.33.10:8500'));
$session = $sf->get('session');
$kv = $sf->get('kv');
// Start a session
$sessionId = $session->create()->json()['ID'];
// Lock a key / value with the current session
$lockAcquired = $kv->put('session/a-lock', 'a value', ['acquire' => $sessionId])->json();
if (false === $lockAcquired) {
$session->destroy($sessionId);
echo "The lock is already acquire by another node.n";
exit(1);
}
# YOUR CRITICAL SECTION CODE HERE...
$kv->delete('session/a-lock');
$session->destroy($sessionId);
67. <?php
if ($settings->get('recommendations/enabled')) {
# Fetch recommendations for user based on request
# ...
}
# curl -X PUT -d '0' http://192.168.33.10:8500/v1/kv/recommendations/enabled
# halt
# ADD MORE CPU/MEMORY
# curl -X PUT -d '1' http://192.168.33.10:8500/v1/kv/recommendations/enabled