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

  • 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,
      ];
      
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

守护在原地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值