栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

hyperf框架nacos服务注册、配置中心和json

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

hyperf框架nacos服务注册、配置中心和json

  • 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' => HyperfConfigNacosNacosDriver::class,
                  // 配置合并方式,支持覆盖和合并
                  'merge_mode' => HyperfConfigNacosConstants::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 客户端)

    • 服务提供者

      		
      		    // 实现一个加法方法,这里简单的认为参数都是 int 类型
      		    public function add(int $a, int $b): int
      		    {
      		        // 这里是服务方法的具体实现
      		        return $a + $b;
      		    }
      		}
      		
      		 [
      		        [
      		            'name' => 'jsonrpc-http',
      		            'type' => Server::SERVER_HTTP,
      		            'host' => '0.0.0.0',
      		            'port' => 9504,
      		            'sock_type' => SWOOLE_SOCK_TCP,
      		            'callbacks' => [
      		                Event::ON_REQUEST => [HyperfJsonRpcHttpServer::class, 'onRequest'],
      		            ],
      		        ],
      		    ],
      		];
      
    • 发布到服务中心

      		 [
      		        '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,
      		        ],
      		    ],
      		];
      
    • 定义消费者

      		
      		    
      		    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
      		 [
      		        [
      		            // 对应消费者类的 $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 [
          AppJsonRpcCalculatorServiceInterface::class => AppJsonRpcCalculatorServiceConsumer::class,
      ];
      
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号