• Hyperf微服务搭建


    一、安装 consul 服务

    1. docker pull consul
    2. docker run --name=consul -p 8500:8500 -e CONSUL_BIND_INTERFACE=eth0 -d consul

     

     安装并启动成功后访问:http://本机IP:8500/

    二、服务提供者

    2.1、启动服务提供者容器

    1. // 本地 D:\phpstudy_pro\WWW 目录创建一个 hyperf 项目作为服务提供者
    2. composer create-project hyperf/hyperf-skeleton hyperf-db
    3. // 基于hyperf镜像生成容器
    4. docker run --name hyperf -v D:\phpstudy_pro\WWW\hyperf-db:/data/project/hyperf-db -p 9501:9501 -it --privileged -u root --entrypoint /bin/sh hyperf/hyperf:7.4-alpine-v3.11-swoole
    5. cd data/project/hyperf-db
    6. // 安装统一接入层
    7. composer require hyperf/service-governance
    8. // 选择安装对应的适配器(这里用consul)
    9. composer require hyperf/service-governance-consul
    10. composer require hyperf/service-governance-nacos
    11. // 安装 JSON RPC 服务
    12. composer require hyperf/json-rpc
    13. // 安装 JSON RPC 服务端
    14. composer require hyperf/rpc-server
    15. // 安装 JSON RPC 客户端
    16. composer require hyperf/rpc-client
    17. // 安装 consul 组件
    18. composer require hyperf/consul
    19. // 发布 consul 组件配置到 app
    20. php bin/hyperf.php vendor:publish hyperf/consul

    2.2、定义 JSON RPC Server 

    1. 'servers' => [
    2. [
    3. 'name' => 'http',
    4. 'type' => Server::SERVER_HTTP,
    5. 'host' => '0.0.0.0',
    6. 'port' => 9501,
    7. 'sock_type' => SWOOLE_SOCK_TCP,
    8. 'callbacks' => [
    9. Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
    10. ],
    11. ],
    12. [
    13. 'name' => 'jsonrpc-http',
    14. 'type' => Server::SERVER_HTTP,
    15. 'host' => '0.0.0.0',
    16. 'port' => 9504,
    17. 'sock_type' => SWOOLE_SOCK_TCP,
    18. 'callbacks' => [
    19. Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
    20. ],
    21. ],
    22. [
    23. 'name' => 'jsonrpc',
    24. 'type' => Server::SERVER_BASE,
    25. 'host' => '0.0.0.0',
    26. 'port' => 9503,
    27. 'sock_type' => SWOOLE_SOCK_TCP,
    28. 'callbacks' => [
    29. Event::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
    30. ],
    31. 'settings' => [
    32. 'open_length_check' => true,
    33. 'package_length_type' => 'N',
    34. 'package_length_offset' => 0,
    35. 'package_body_offset' => 4,
    36. 'package_max_length' => 1024 * 1024 * 2,
    37. ],
    38. ],
    39. ],

    2.3、定义接口

    1. declare(strict_types=1);
    2. namespace App\JsonRpc;
    3. interface CalculatorServiceInterface
    4. {
    5. public function add(int $a, int $b): int;
    6. }

    2.4、定义服务提供者

    1. namespace App\JsonRpc;
    2. use Hyperf\RpcServer\Annotation\RpcService;
    3. /**
    4. * @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="consul")
    5. */
    6. class CalculatorService implements CalculatorServiceInterface
    7. {
    8. // 实现一个加法方法,这里简单的认为参数都是 int 类型
    9. public function add(int $a, int $b): int
    10. {
    11. // 这里是服务方法的具体实现
    12. return $a + $b;
    13. }
    14. }

     2.5、发布到服务中心

    在 config/autoload/services.php 配置文件内配置 drivers.consul 配置即可,注意 127.0.0.1 需要更换本机 ip 地址,否则会报错。

    1. return [
    2. 'enable' => [
    3. 'discovery' => true,
    4. 'register' => true,
    5. ],
    6. 'providers' => [],
    7. 'drivers' => [
    8. 'consul' => [
    9. 'uri' => 'http://127.0.0.1:8500',
    10. 'token' => '',
    11. 'check' => [
    12. 'deregister_critical_service_after' => '90m',
    13. 'interval' => '1s',
    14. ],
    15. ],
    16. 'nacos' => [
    17. // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
    18. // 'url' => '',
    19. // The nacos host info
    20. 'host' => '127.0.0.1',
    21. 'port' => 8848,
    22. // The nacos account info
    23. 'username' => null,
    24. 'password' => null,
    25. 'guzzle' => [
    26. 'config' => null,
    27. ],
    28. 'group_name' => 'api',
    29. 'namespace_id' => 'namespace_id',
    30. 'heartbeat' => 5,
    31. 'ephemeral' => false,
    32. ],
    33. ],
    34. ];

     三、服务消费者

    3.1、启动服务消费者容器

    1. // 本地 D:\phpstudy_pro\WWW 目录创建一个 hyperf 项目作为服务消费者
    2. composer create-project hyperf/hyperf-skeleton hyperf-api
    3. // 基于hyperf镜像生成容器
    4. docker run --name hyperf-api -v D:\phpstudy_pro\WWW\hyperf-api:/data/project/hyperf-api -p 9502:9502 -it --privileged -u root --entrypoint /bin/sh hyperf/hyperf:7.4-alpine-v3.11-swoole
    5. cd data/project/hyperf-api

    服务消费者也可以作为服务提供者提供服务,所以按照之前的步骤把相关组件安装一下

    3.2、修改 server 配置

    1. // config/autoload/server.php
    2. 'servers' => [
    3. [
    4. 'name' => 'http',
    5. 'type' => Server::SERVER_HTTP,
    6. 'host' => '0.0.0.0',
    7. 'port' => 9502,
    8. 'sock_type' => SWOOLE_SOCK_TCP,
    9. 'callbacks' => [
    10. Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
    11. ],
    12. ],
    13. ],
    14. // config/autoload/services.php
    15. 'consumers' => [
    16. [
    17. // name 需与服务提供者的 name 属性相同
    18. 'name' => 'CalculatorService',
    19. // 服务接口名,可选,默认值等于 name 配置的值,如果 name 直接定义为接口类则可忽略此行配置,如 name 为字符串则需要配置 service 对应到接口类
    20. 'service' => \App\JsonRpc\CalculatorServiceInterface::class,
    21. // 对应容器对象 ID,可选,默认值等于 service 配置的值,用来定义依赖注入的 key
    22. 'id' => \App\JsonRpc\CalculatorServiceInterface::class,
    23. // 服务提供者的服务协议,可选,默认值为 jsonrpc-http
    24. // 可选 jsonrpc-http jsonrpc jsonrpc-tcp-length-check
    25. 'protocol' => 'jsonrpc-http',
    26. // 负载均衡算法,可选,默认值为 random
    27. 'load_balancer' => 'random',
    28. // 这个消费者要从哪个服务中心获取节点信息,如不配置则不会从服务中心获取节点信息
    29. 'registry' => [
    30. 'protocol' => 'consul',
    31. 'address' => 'http://127.0.0.1:8500',
    32. ],
    33. // 如果没有指定上面的 registry 配置,即为直接对指定的节点进行消费,通过下面的 nodes 参数来配置服务提供者的节点信息
    34. 'nodes' => [
    35. ['host' => '127.0.0.1', 'port' => 9504],
    36. ],
    37. // 配置项,会影响到 Packer 和 Transporter
    38. 'options' => [
    39. 'connect_timeout' => 5.0,
    40. 'recv_timeout' => 5.0,
    41. 'settings' => [
    42. // 根据协议不同,区分配置
    43. 'open_eof_split' => true,
    44. 'package_eof' => "\r\n",
    45. // 'open_length_check' => true,
    46. // 'package_length_type' => 'N',
    47. // 'package_length_offset' => 0,
    48. // 'package_body_offset' => 4,
    49. ],
    50. // 重试次数,默认值为 2,收包超时不进行重试。暂只支持 JsonRpcPoolTransporter
    51. 'retry_count' => 2,
    52. // 重试间隔,毫秒
    53. 'retry_interval' => 100,
    54. // 使用多路复用 RPC 时的心跳间隔,null 为不触发心跳
    55. 'heartbeat' => 30,
    56. // 当使用 JsonRpcPoolTransporter 时会用到以下配置
    57. 'pool' => [
    58. 'min_connections' => 1,
    59. 'max_connections' => 32,
    60. 'connect_timeout' => 10.0,
    61. 'wait_timeout' => 3.0,
    62. 'heartbeat' => -1,
    63. 'max_idle_time' => 60.0,
    64. ],
    65. ],
    66. ]
    67. ],
    68. 'drivers' => [
    69. 'consul' => [
    70. 'uri' => 'http://127.0.0.1:8500',
    71. 'token' => '',
    72. 'check' => [
    73. 'deregister_critical_service_after' => '90m',
    74. 'interval' => '1s',
    75. ],
    76. ],
    77. 'nacos' => [
    78. // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
    79. // 'url' => '',
    80. // The nacos host info
    81. 'host' => '127.0.0.1',
    82. 'port' => 8848,
    83. // The nacos account info
    84. 'username' => null,
    85. 'password' => null,
    86. 'guzzle' => [
    87. 'config' => null,
    88. ],
    89. 'group_name' => 'api',
    90. 'namespace_id' => 'namespace_id',
    91. 'heartbeat' => 5,
    92. 'ephemeral' => false,
    93. ],
    94. ],

    3.3、定义接口,同服务端保持一致

    1. declare(strict_types=1);
    2. namespace App\JsonRpc;
    3. interface CalculatorServiceInterface
    4. {
    5. public function add(int $a, int $b): int;
    6. }

     3.4、定义路由

    Router::get('/testRpc', 'App\Controller\IndexController::testRpc');

    3.5、控制器中调用

    1. public function testRpc()
    2. {
    3. $client = ApplicationContext::getContainer()->get(CalculatorServiceInterface::class);
    4. $res = $client->add(1,2);
    5. return ['result'=>$res];
    6. }

  • 相关阅读:
    禁止安装新软件怎么设置(超详细图文介绍)
    【yolov6系列一】深度解析网络架构
    .net 使用Docker开发
    Go语言中操作Redis
    目标检测介绍以及自动驾驶场景应用
    python+selenium进行cnblog的自动化登录测试
    SQL注入学习--GTFHub(布尔盲注+时间盲注+MySQL结构)
    KMP算法的实现详解
    tomcat9 zip包 安装
    7种典型的钢结构BIM应用
  • 原文地址:https://blog.csdn.net/phpBin/article/details/125996616