• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

hyperf框架nacos服务注册、配置心和json_rpc调用

武飞扬头像
守护在原地
帮助11

  • nacos服务注册

    • 安装统一接入层
      composer require hyperf/service-governance

    • 安装nacos
      composer require hyperf/service-governance-nacos

    • 配置文件(组件由 config/autoload/services.php 配置文件来驱动)

      		return [
      				    'enable' => [
      				        // 开启服务发现
      				        'discovery' => true,
      				        // 开启服务注册
      				        'register' => true,
      				    ],
      				    // 服务消费者相关配置
      				    'consumers' => [],
      				    // 服务提供者相关配置
      				    'providers' => [],
      				    // 服务驱动相关配置
      				    'drivers' => [
      				        'consul' => [
      				            'uri' => 'http://127.0.0.1:8500',
      				            'token' => '',
      				            'check' => [
      				                'deregister_critical_service_after' => '90m',
      				                'interval' => '1s',
      				            ],
      				        ],
      				        'nacos' => [
      				            // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
      				            // 'url' => '',
      				            // The nacos host info
      				            'host' => '127.0.0.1',
      				            'port' => 8848,
      				            // The nacos account info
      				            'username' => null,
      				            'password' => null,
      				            'guzzle' => [
      				                'config' => null,
      				            ],
      				            'group_name' => 'api',
      				            'namespace_id' => 'namespace_id',
      				            'heartbeat' => 5,
      				            'ephemeral' => false, // 是否注册临时实例
      				        ],
      				    ],
      				];
      
      学新通
  • nacos配置中心

    • 配置中心统一接入层
      composer require hyperf/config-center
    • Nacos 安装
      composer require hyperf/config-nacos
    • 配置中心 (config目录下config_center.php)
      	'drivers' => [
      			'nacos' => [
                  'driver' => Hyperf\ConfigNacos\NacosDriver::class,
                  // 配置合并方式,支持覆盖和合并
                  'merge_mode' => Hyperf\ConfigNacos\Constants::CONFIG_MERGE_OVERWRITE,
                  'interval' => 3,
                  // 如果对应的映射 key 没有设置,则使用默认的 key
                  'default_key' => 'nacos_config',
                  'listener_config' => [
                      // dataId, group, tenant, type, content
                      // 映射后的配置 KEY => Nacos 中实际的配置
                      'nacos_config' => [
                          'tenant' => 'tenant', // corresponding with service.namespaceId
                          'data_id' => 'hyperf-service-config',
                          'group' => 'DEFAULT_GROUP',
                      ],
                      'nacos_config.data' => [
                          'data_id' => 'hyperf-service-config-yml',
                          'group' => 'DEFAULT_GROUP',
                          'type' => 'yml',
                      ],
                  ],
      		]
      
      学新通
    • nacos配置和hyperf调用
      • 学新通
        学新通
        学新通
        学新通
  • json_rpc调用

    • 安装
      composer require hyperf/json-rpc
      composer require hyperf/rpc-server(JSON RPC 服务端)
      composer require hyperf/rpc-client(JSON RPC 客户端)

    • 服务提供者

      		<?php
      		
      		namespace App\JsonRpc;
      		
      		use Hyperf\RpcServer\Annotation\RpcService;
      		
      		/**
      		 * 注意,如希望通过服务中心来管理服务,需在注解内增加 publishTo 属性
      		 */
      		#[RpcService(name: "CalculatorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
      		class CalculatorService implements CalculatorServiceInterface
      		{
      		    // 实现一个加法方法,这里简单的认为参数都是 int 类型
      		    public function add(int $a, int $b): int
      		    {
      		        // 这里是服务方法的具体实现
      		        return $a   $b;
      		    }
      		}
      		
      		<?php
      		
      		use Hyperf\Server\Server;
      		use Hyperf\Server\Event;
      		
      		return [
      		    // 这里省略了该文件的其它配置
      		    'servers' => [
      		        [
      		            'name' => 'jsonrpc-http',
      		            'type' => Server::SERVER_HTTP,
      		            'host' => '0.0.0.0',
      		            'port' => 9504,
      		            'sock_type' => SWOOLE_SOCK_TCP,
      		            'callbacks' => [
      		                Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
      		            ],
      		        ],
      		    ],
      		];
      
      学新通
    • 发布到服务中心

      		<?php
      		return [
      		    'enable' => [
      		        'discovery' => true,
      		        'register' => true,
      		    ],
      		    'consumers' => [],
      		    'providers' => [],
      		    'drivers' => [
      		        'consul' => [
      		            'uri' => 'http://127.0.0.1:8500',
      		            'token' => '',
      		        ],
      		        'nacos' => [
      		            // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
      		            // 'url' => '',
      		            // The nacos host info
      		            'host' => '127.0.0.1',
      		            'port' => 8848,
      		            // The nacos account info
      		            'username' => null,
      		            'password' => null,
      		            'guzzle' => [
      		                'config' => null,
      		            ],
      		            'group_name' => 'api',
      		            'namespace_id' => 'namespace_id',
      		            'heartbeat' => 5,
      		        ],
      		    ],
      		];
      
      学新通
    • 定义消费者

      		<?php
      		
      		namespace App\JsonRpc;
      		
      		use Hyperf\RpcClient\AbstractServiceClient;
      		
      		class CalculatorServiceConsumer extends AbstractServiceClient implements CalculatorServiceInterface
      		{
      		    /**
      		     * 定义对应服务提供者的服务名称
      		     */
      		    protected string $serviceName = 'CalculatorService';
      		    
      		    /**
      		     * 定义对应服务提供者的服务协议
      		     */
      		    protected string $protocol = 'jsonrpc-http';
      		
      		    public function add(int $a, int $b): int
      		    {
      		        return $this->__request(__FUNCTION__, compact('a', 'b'));
      		    }
      		}
      		services.php
      		<?php
      		return [
      		    // 此处省略了其它同层级的配置
      		    'consumers' => [
      		        [
      		            // 对应消费者类的 $serviceName
      		            'name' => 'CalculatorService',
      		            // 这个消费者要从哪个服务中心获取节点信息,如不配置则不会从服务中心获取节点信息
      		            'registry' => [
      		                'protocol' => 'nacos',
      		                'address' => 'http://192.168.61.111:8848',
      		            ],
      		            // 如果没有指定上面的 registry 配置,即为直接对指定的节点进行消费,通过下面的 nodes 参数来配置服务提供者的节点信息
      		//            'nodes' => [
      		//                ['host' => '172.19.60.77', 'port' => 9504],
      		//            ],
      		        ]
      		    ],
      		    'drivers' => [
      		        'nacos' => [
      		            // nacos server url like https://nacos.hyperf.io, Priority is higher than host:port
      		            // 'url' => '',
      		            // The nacos host info
      		            'host' => '192.168.61.111',
      		            'port' => 8848,
      		            // The nacos account info
      		            'username' => 'nacos',
      		            'password' => 'nacos',
      		            'guzzle' => [
      		                'config' => null,
      		            ],
      		            'group_name' => 'DEFAULT_GROUP',
      		            'namespace_id' => '8ab0c40e-3e16-4809-836d-a2947589276f',
      		            'heartbeat' => 5,
      		            'ephemeral' => false, // 是否注册临时实例
      		        ],
      		    ],
      		];
      
      学新通

      这样我们便可以通过 CalculatorService 类来实现对服务的消费了,为了让这里的关系逻辑更加的合理,还应该在 config/autoload/dependencies.php 内定义 CalculatorServiceInterface 和 CalculatorServiceConsumer 的关系,示例如下:

      return [
          App\JsonRpc\CalculatorServiceInterface::class => App\JsonRpc\CalculatorServiceConsumer::class,
      ];
      

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiaffe
系列文章
更多 icon
同类精品
更多 icon
继续加载