composer require wjy/rpc-helper
自动配置服务消费者,省去配置services.consumers步骤
#[RpcClient(registerCenter: 'nacos')]
class CalculatorServiceClient extends AbstractServiceClient implements CalculatorServiceInterface
{
protected string $serviceName = 'CalculatorService';
// ...
}指定nacos地址, 默认使用services.php配置的nacos
#[RpcClient(registerCenter: 'nacos', address: 'http://localhost:8848')]如果不使用配置中心
#[RpcClient(nodes: ['localhost:9502'])]#[RpcController(prefix: '/calculator')] // prefix默认为类名首字母小写
class GoodsService implements GoodsServiceInterface
{
#[RpcMapping(method: 'get')] // path默认是方法名 接口: /calculator/add
public function add($a, $b)
{
return $a + $b;
}
#[RpcMapping(path: 'minus', method: 'get')] // 指定path地址 接口: /calculator/add
public function minus($a, $b)
{
return $a + $b;
}
}