改进
This commit is contained in:
parent
f843a11ea7
commit
68020d67a5
97
app/Console/Commands/Cluster/Send.php
Normal file
97
app/Console/Commands/Cluster/Send.php
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands\Cluster;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Redis\RedisManager;
|
||||||
|
|
||||||
|
class Send extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'cluster:send';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = '发送节点心跳';
|
||||||
|
|
||||||
|
|
||||||
|
protected RedisManager $redis;
|
||||||
|
protected String $instance_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->redis = app('redis');
|
||||||
|
$this->instance_id = config('app.instance_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
|
||||||
|
$this->info('将上报节点信息到集群中。');
|
||||||
|
|
||||||
|
// 非堵塞
|
||||||
|
// $this->redis->subscribe(['cluster:sync'], function ($message, $channel) {
|
||||||
|
// $this->info('收到同步请求');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// echo '开始循环发送心跳';
|
||||||
|
while (true) {
|
||||||
|
|
||||||
|
// get cpu usage
|
||||||
|
$cpu = round($this->getCpuUsage(), 2);
|
||||||
|
|
||||||
|
echo "CPU: {$cpu}%\n";
|
||||||
|
$this->redis->publish('cluster_ready', json_encode([
|
||||||
|
'instance_id' => $this->instance_id,
|
||||||
|
'cpu' => $cpu,
|
||||||
|
]));
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCpuUsage()
|
||||||
|
{
|
||||||
|
$load = sys_getloadavg();
|
||||||
|
return $load[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function subscribe()
|
||||||
|
{
|
||||||
|
|
||||||
|
// 非堵塞模式
|
||||||
|
$this->redis->subscribe(['cluster_ready'], function ($message, $channel) {
|
||||||
|
echo "Received {$message} from {$channel}\n";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function publish()
|
||||||
|
{
|
||||||
|
$this->redis->publish('cluster_ready', json_encode([
|
||||||
|
'instance_id' => $this->instance_id,
|
||||||
|
'cpu' => 1
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
76
app/Console/Commands/Cluster/Work.php
Normal file
76
app/Console/Commands/Cluster/Work.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands\Cluster;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Redis\RedisManager;
|
||||||
|
|
||||||
|
class Work extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'cluster:work';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = '开始节点协调工作';
|
||||||
|
|
||||||
|
|
||||||
|
protected RedisManager $redis;
|
||||||
|
protected String $instance_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->redis = app('redis');
|
||||||
|
$this->instance_id = config('app.instance_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->info('节点协调工作开始');
|
||||||
|
|
||||||
|
$this->redis->subscribe(['cluster_ready'], function ($message, $channel) {
|
||||||
|
|
||||||
|
$message = json_decode($message, true);
|
||||||
|
|
||||||
|
var_dump($message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function subscribe()
|
||||||
|
{
|
||||||
|
|
||||||
|
// 非堵塞模式
|
||||||
|
$this->redis->subscribe(['cluster_ready'], function ($message, $channel) {
|
||||||
|
echo "Received {$message} from {$channel}\n";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function publish()
|
||||||
|
{
|
||||||
|
$this->redis->publish('cluster_ready', json_encode([
|
||||||
|
'instance_id' => $this->instance_id,
|
||||||
|
'cpu' => 1
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
@ -1,66 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Console\Commands\Cluster;
|
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
|
||||||
|
|
||||||
class Worker extends Command
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The name and signature of the console command.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $signature = 'cluster:work';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The console command description.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $description = '开始节点协调工作';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new command instance.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the console command.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function handle()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
|
|
||||||
$redis = app('redis');
|
|
||||||
|
|
||||||
$instance_id = config('app.instance_id');
|
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
|
|
||||||
// get cpu usage
|
|
||||||
$cpu = round($this->getCpuUsage(), 2);
|
|
||||||
|
|
||||||
$redis->publish('cluster_ready', json_encode([
|
|
||||||
'instance_id' => $instance_id,
|
|
||||||
'cpu' => $cpu,
|
|
||||||
]));
|
|
||||||
|
|
||||||
sleep(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCpuUsage()
|
|
||||||
{
|
|
||||||
$load = sys_getloadavg();
|
|
||||||
return $load[0];
|
|
||||||
}
|
|
||||||
}
|
|
@ -41,7 +41,8 @@ class Kernel extends ConsoleKernel
|
|||||||
Count::class,
|
Count::class,
|
||||||
Status::class,
|
Status::class,
|
||||||
Cluster\Init::class,
|
Cluster\Init::class,
|
||||||
Cluster\Worker::class,
|
Cluster\Send::class,
|
||||||
|
Cluster\Work::class,
|
||||||
Cluster\Sync::class,
|
Cluster\Sync::class,
|
||||||
Commands\System\Down::class,
|
Commands\System\Down::class,
|
||||||
Commands\System\Up::class,
|
Commands\System\Up::class,
|
||||||
|
@ -6,12 +6,12 @@
|
|||||||
use App\Models\Transaction;
|
use App\Models\Transaction;
|
||||||
use App\Models\User\Balance;
|
use App\Models\User\Balance;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use App\Exceptions\ChargeException;
|
use App\Exceptions\ChargeException;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
|
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
|
||||||
|
use Alipay\EasySDK\Kernel\Config as AlipayConfig;
|
||||||
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
|
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
|
||||||
|
|
||||||
class BalanceController extends Controller
|
class BalanceController extends Controller
|
||||||
@ -73,8 +73,6 @@ public function store(Request $request)
|
|||||||
|
|
||||||
public function show(Request $request, Balance $balance)
|
public function show(Request $request, Balance $balance)
|
||||||
{
|
{
|
||||||
// dd(AlipayFactory::payment()->common()->query('20220901070430102316'));
|
|
||||||
// dd(route(''));
|
|
||||||
if ($balance->paid_at !== null) {
|
if ($balance->paid_at !== null) {
|
||||||
return $this->error('订单已支付');
|
return $this->error('订单已支付');
|
||||||
}
|
}
|
||||||
@ -85,6 +83,8 @@ public function show(Request $request, Balance $balance)
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
AlipayFactory::setOptions($this->alipayOptions());
|
||||||
|
|
||||||
$result = AlipayFactory::payment()->page()->pay("支付", $balance->order_id, $balance->amount, route('balances.return'));
|
$result = AlipayFactory::payment()->page()->pay("支付", $balance->order_id, $balance->amount, route('balances.return'));
|
||||||
|
|
||||||
$responseChecker = new ResponseChecker();
|
$responseChecker = new ResponseChecker();
|
||||||
@ -154,6 +154,8 @@ public function notify(Request $request)
|
|||||||
|
|
||||||
public function checkAndCharge(Balance $balance)
|
public function checkAndCharge(Balance $balance)
|
||||||
{
|
{
|
||||||
|
AlipayFactory::setOptions($this->alipayOptions());
|
||||||
|
|
||||||
$trade = AlipayFactory::payment()->common()->query($balance->order_id);
|
$trade = AlipayFactory::payment()->common()->query($balance->order_id);
|
||||||
|
|
||||||
if ($trade->code == "10000" && $trade->tradeStatus == "TRADE_SUCCESS") {
|
if ($trade->code == "10000" && $trade->tradeStatus == "TRADE_SUCCESS") {
|
||||||
@ -222,4 +224,34 @@ public function drops()
|
|||||||
|
|
||||||
return $this->success($resp);
|
return $this->success($resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function alipayOptions()
|
||||||
|
{
|
||||||
|
$options = new AlipayConfig();
|
||||||
|
$options->protocol = 'https';
|
||||||
|
|
||||||
|
// if local
|
||||||
|
if (app()->environment() == 'local') {
|
||||||
|
$options->gatewayHost = 'openapi.alipaydev.com';
|
||||||
|
} else {
|
||||||
|
$options->gatewayHost = 'openapi.alipay.com';
|
||||||
|
}
|
||||||
|
|
||||||
|
$options->signType = 'RSA2';
|
||||||
|
|
||||||
|
$options->appId = config('payment.alipay.app_id');
|
||||||
|
|
||||||
|
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
||||||
|
$options->merchantPrivateKey = trim(Storage::get('alipayAppPriv.key'));
|
||||||
|
|
||||||
|
$options->alipayCertPath = storage_path('app/alipayCertPublicKey_RSA2.crt');
|
||||||
|
$options->alipayRootCertPath = storage_path('app/alipayRootCert.crt');
|
||||||
|
$options->merchantCertPath = storage_path('app/appCertPublicKey.crt');
|
||||||
|
|
||||||
|
$options->notifyUrl = route('balances.notify');
|
||||||
|
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Illuminate\Support\Str;
|
// use Illuminate\Support\Str;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@
|
|||||||
'host' => env('REDIS_HOST', '127.0.0.1'),
|
'host' => env('REDIS_HOST', '127.0.0.1'),
|
||||||
'password' => env('REDIS_PASSWORD'),
|
'password' => env('REDIS_PASSWORD'),
|
||||||
'port' => env('REDIS_PORT', '6379'),
|
'port' => env('REDIS_PORT', '6379'),
|
||||||
'database' => env('REDIS_DB', '0'),
|
'database' => env('REDIS_DB', 0),
|
||||||
'persistent' => true,
|
'persistent' => true,
|
||||||
],
|
],
|
||||||
|
|
||||||
@ -130,7 +130,15 @@
|
|||||||
'host' => env('REDIS_HOST', '127.0.0.1'),
|
'host' => env('REDIS_HOST', '127.0.0.1'),
|
||||||
'password' => env('REDIS_PASSWORD'),
|
'password' => env('REDIS_PASSWORD'),
|
||||||
'port' => env('REDIS_PORT', '6379'),
|
'port' => env('REDIS_PORT', '6379'),
|
||||||
'database' => env('REDIS_CACHE_DB', '1'),
|
'database' => env('REDIS_CACHE_DB', 1),
|
||||||
|
],
|
||||||
|
|
||||||
|
'cluster_ready' => [
|
||||||
|
'url' => env('REDIS_URL'),
|
||||||
|
'host' => env('REDIS_HOST', '127.0.0.1'),
|
||||||
|
'password' => env('REDIS_PASSWORD'),
|
||||||
|
'port' => env('REDIS_PORT', '6379'),
|
||||||
|
'database' => env('REDIS_CACHE_DB', 2),
|
||||||
],
|
],
|
||||||
|
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user