PHP code example of ahas / sentinel

1. Go to this page and download the library: Download ahas/sentinel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ahas / sentinel example snippets




// 创建 Sentinel 客户端
$sentinel = new \Sentinel\SentinelClient("localhost", 9090);

// 受保护的业务逻辑, sleep 200ms 模拟业务耗时。
echo "Killer Business!\n";
usleep(200 * 1000);

try {
	// 受保护的业务逻辑, sleep 200ms 模拟业务耗时。
	echo "Killer Business!\n";
	usleep(200 * 1000);
} finally {
}

// 获取受保护的资源访问入口。
$sentinelHelloEntry = $sentinel->entry("hello");

try {
	// 获取受保护的资源访问入口。
	// 注意: 必须定义一个变量保存入口对象, 否则入口对象将被自动销毁并释放资源。
	$sentinelHelloEntry = $sentinel->entry("hello");

	// 受保护的业务逻辑, sleep 200ms 模拟业务耗时。
	echo "Killer Business!\n";
	usleep(200 * 1000);
} finally {
    // 将保存入口对象的变量置为 null, 使不再有变量引用入口对象, 以销毁入口对象并释放资源。
    $sentinelHelloEntry = null;
}
text
$ php index.php 
Killer Business!
text
$ php index.php 
PHP Fatal error:  Uncaught Thrift\Exception\TException: TSocket: Could not connect to localhost:9090 (Connection refused [111])
text
$ for e in $(seq 5) ; do php index.php ; done
Killer Business!
Killer Business!
Killer Business!
Online user FULL! Try later...
Online user FULL! Try later...
sh
docker run --name=demo --net=host -v "${MY_PHP_APP}:/app/php" -d registry.cn-hangzhou.aliyuncs.com/ahas/sentinel-php-demo demo/bin/demo.sh --local
sh
docker run --name=demo --net=host -d registry.cn-hangzhou.aliyuncs.com/ahas/sentinel-php-demo demo/bin/demo.sh --app=demo --license=${license}