PHP code example of overlu / laravel-reget

1. Go to this page and download the library: Download overlu/laravel-reget 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/ */

    

overlu / laravel-reget example snippets


# publish config 
php artisan vendor:publish --provider="Overlu\Reget\RegetServiceProvider"

# 打开 config/app.php,注册如下服务提供者到 $providers 数组
Overlu\Reget\RegetServiceProvider::class

# 然后添加如下门面到 $aliaes 数组
'Reget' => \Overlu\Reget\Facades\Reget::class

[
'driver' => 'nacos',
  'nacos' => [
    'register_host' => env('NACOS_REGISTER_HOST', 'http://127.0.0.1:8848'),
    'ip' => env('NACOS_SERVICE_HOST', ''),   // 服务实例IP
    'port' => env('NACOS_SERVICE_PORT', ''), // 服务实例port
    'namespaceId' => env('NACOS_SERVICE_NAMESPACE_ID', ''), // 命名空间ID
    'weight' => env('NACOS_SERVICE_WEIGHT', '0.8'),  // 权重
    'enabled' => env('NACOS_SERVICE_ENABLE', 'true'),  // 是否上线
    'healthy' => env('NACOS_SERVICE_HEALTHY', 'true'),  // 是否健康
    'metadata' => env('NACOS_SERVICE_METADATA', '{}'),  // 扩展信息 json
    'clusterName' => env('NACOS_SERVICE_CLUSTER_NAME', 'DEFAULT'),  // 集群名
    'serviceName' => env('NACOS_SERVICE_NAME', 'server_name'), // 服务名
    'groupName' => env('NACOS_SERVICE_GROUP_NAME', 'DEFAULT_GROUP'),  // 分组名
    'ephemeral' => env('NACOS_SERVICE_EPHEMERAL', 'true'), // 是否临时实例
    'scheduled' => env('NACOS_SERVICE_SCHEDULED', 'true')
  ]
];

# 2. 在 App\Console\Kernel 类的 schedule 方法中定义所有的调度任务
protected function schedule(Schedule $schedule)
{
    $schedule->command('reget:heartbeat')->everyMinute();
}

$service = \Overlu\Reget\Reget::getInstance()->service('service_name');
# or 
$service = Reget::service('service_name');
return $service;

$services_list = \Overlu\Reget\Reget::getInstance()->services();
# or 
$services_list = Reget::services();

$config = \Overlu\Reget\Reget::getInstance()->config('key');  // 走缓存处理
$config = \Overlu\Reget\Reget::getInstance()->config('key', false); // 直接获取远程配置数据
# or 
$config = Reget::config('key'); // 走缓存处理
$config = Reget::config('key', false); // 直接获取远程配置数据

$response = \Overlu\Reget\Reget::getInstance()->publish('key', 'string value');
# or
$response = Reget::publish('key', 'string value'); // return true

$response = \Overlu\Reget\Reget::getInstance()->remove('key'); # return true
# or 
$response = Reget::remove('key');

Reget::listen('key'); # 可以curl请求路由

php artisan reget:listen key --handle="Namespace\ClassName"



namespace Namespace;

Class ClassName
{
    /**
     * @param $key
     * @param $data
     * @param $originData
     */
    public static function handle($key, $data, $originData)
    {
        dd($key, $data, $originData);
    }

    /**
     * @param \Exception $exception
     */
    public static function error(\Exception $exception)
    {
        dd($exception->getCode());
    }
}

# 读取配置缓存
\Overlu\Reget\Utils\ConfigCache::get('key', 'group_name');
# 写入配置缓存
\Overlu\Reget\Utils\ConfigCache::set('key', 'group_name', 'content');

$env = new \Overlu\Reget\Utils\Env();
# 读取env
$value = env('key', 'default_value');
# or 
$value = $env->getEnv('key', 'default_value');

# 写入env
$result = $env->setEnv('key', 'value');
# 批量写入env数据,会自动插入空行分组
$env->setEnvs([
    'key1' => 'value1',
    'key2' => 'value2'
]);

php composer.phar 
shell
php artisan reget:register  // 根据配置文件注册
shell
php artisan reget:register --init  // 初始化配置并注册
shell
php artisan reget:heartbeat   // 发送一次
shell
php artisan reget:heartbeat --cron  // 定时发送
shell
php artisan reget:list
shell
php artisan reget:instance
shell
php artisan reget:remove
shell
php artisan reget:listen key
# 加入观察者
php artisan reget:listen key --handle="Namespace\ClassName"
shell
php artisan reget:heartbeat --cron # 联调测试用
# or
php artisan reget:heartbeat --cron>>/dev/null 2>&1 &
shell
php artisan reget:listen key