1. 前言:
获取了健康服务列表,如果存在多个service,在调用的时候,必然要选取一个.可以由网关或者自己写程序获取服务.常见的算法有:
- 随机算法
- 轮训
2. 随机算法:
(1). 算法公共类:
app\Consul\ServiceSelector.php:
namespace App\Consul;
class ServiceSelector {
// 静态方法:随机算法
public static function selectByRandom(array $serviceList): array
{
$index = array_rand($serviceList);
return $serviceList[$index];
}
}
(2). 调用方法:
app\Http\MyClient.php:
use App\Consul\ServiceSelector;
...
/**
* @RequestMapping(router="getOneService", method={RequestMethod::GET})
*/
public function getOneService(): array
{
// 静态方法调用
return ServiceSelector::selectByRandom($this->getHealthService());
}
(3). 问题点:
第一次访问是A服务器提供的csrt toekn,但是表单提交时,随机算法到B服务器了.如果两者之间需要保持session,就会出现不同步.