PHP接入consul,注册服务和发现服务【学习笔记】

embedded/2024/9/22 17:27:27/

consul_0">PHP接入consul,注册服务和发现服务

consul_1">consul安装

链接: consul安装

consul_3">启动consul

C:\Users\14684>consul agent -dev

在这里插入图片描述

安装TP5

composer create-project topthink/think=5.0.* tp5_pro --prefer-dist

consul_13">配置consul

在这里插入图片描述

php_15">创建tp5_pro/application/service/Consul.php

<?php
/************************************************************************** File Name: Consul.php* Author: shijie.zheng* Created Time: 2024/7/21 18:11* Desc:************************************************************************/namespace app\service;use think\Config;class Consul
{private $httpUrl;public function __construct(){$consulPort = \think\Env::get('consul.port');$consulHost = \think\Env::get('consul.host');$this->httpUrl = 'http://' . $consulHost . ':' . $consulPort . '/';}//服务注册//agent/service/registerpublic function registerService($data){$url = $this->httpUrl . 'v1/agent/service/register';return $this->curlPUT($url, $data);}//服务信息public function serviceInfo($serviceId){$url = $this->httpUrl . 'v1/health/service/' . $serviceId;
//        echo $url;
//        return $this->makeRequest($url);return file_get_contents($url);}public function curlPUT($httpUrl, $data){$ch = curl_init();curl_setopt($ch,CURLOPT_URL,$httpUrl);curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"PUT");curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_HTTPHEADER, ["Content-type:application/json"]);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($data));$res = curl_exec($ch);if($res === false){var_dump(curl_error($ch));}curl_close($ch);return $res;}public function makeRequest($url, $argument = array(), $ttl = 5, $method = "GET", $cookie = '', $follow = 0, $referer = ''){if (!$url) {throw new LogicException('$url不能为空');}if (substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://') {return array('result' => NULL, 'code' => '400');}if ($method == 'GET' && count($argument) > 0) {$url .= "?" . (http_build_query($argument));//echo $url;}$header = array('Accept-Language: zh-cn,zh;q=0.8','Connection: Keep-alive','Cache-Control: max-age=0');$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);if ($method == 'POST') {curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $argument);}if (file_exists($cookie)) {curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);}if (!empty($referer)) {curl_setopt($ch, CURLOPT_REFERER, $referer);}curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_TIMEOUT, $ttl);curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1707.0 Safari/537.36');curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//curl_setopt($ch, CURLOPT_REFERER, 'https://analytics.talkingdata.net/webpage/UserRetainInfo.jsp');if ($follow == 1) {curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);}$return = array();$return['result'] = curl_exec($ch);$return['code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);curl_close($ch);unset($ch);return $return;}}

php_125">创建tp5_pro/application/index/controller/ConsulDemo.php

<?php
/************************************************************************** File Name: ConsulDemo.php* Author: shijie.zheng* Created Time: 2024/7/21 22:30* Desc:************************************************************************/namespace app\index\controller;use app\service\Consul;class ConsulDemo
{//注册服务public function regDemo(){
//        echo 1;$data = ['ID' => 'demoService','Name' => 'demoService','Tags' => ['core.demo'],'Address' => '127.0.0.1','Port' => 8300,'Check' => ['HTTP' => 'http://127.0.0.1:80','Interval' => '5s']];$consul = new Consul();$rs = $consul->registerService($data);var_dump($rs);}//服务发现public function serviceInfo(){$serviceId = 'demoService';$consul = new Consul();$rs = $consul->serviceInfo($serviceId);echo $rs;}
}

php_170">创建路由tp5_pro/application/route.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use  \think\Route;Route::get('consul/reg/demo', 'ConsulDemo/regDemo');
Route::get('consul/service/info', 'ConsulDemo/serviceInfo');
Route::get('/test',function(){return 'Hello,world!';
});

浏览器进行服务注册

在这里插入图片描述

consul_192">打开consul

http://127.0.0.1:8500/ui/dc1/services
在这里插入图片描述

获取信息

http://www.tp5.com/index.php/consul/service/info
在这里插入图片描述


http://www.ppmy.cn/embedded/87135.html

相关文章

C++ - char*、const char*、char[]、string

const char* const char* 用来定义字符串常量。 char[ ] char型的字符数组是一种定长的数组&#xff0c;存储指定长度的字符序列&#xff0c;数组中的每个元素都是一个char类型的变量&#xff0c;如&#xff1a; char arr[] {h, a, l, l, o, \0}; char c arr[0]; // 访问…

MySQL数据库(基础篇)

&#x1f30f;个人博客主页&#xff1a;心.c 前言&#xff1a;今天讲解的是MySQL的详细知识点的&#xff0c;希望大家可以收货满满&#xff0c;话不多说&#xff0c;直接开始搞&#xff01; &#x1f525;&#x1f525;&#x1f525;文章专题&#xff1a;MySQL &#x1f63d;感…

如何让python列表的读取

列表是什么 列表是一种数据结构&#xff0c;用于存储多个元素。每个元素可以是任意类型的数据&#xff0c;如整数、字符串、布尔值等。列表中的元素可以根据需要进行添加、删除、修改和访问。列表的特点是可以存储多个元素&#xff0c;并且元素之间可以有顺序关系。通过使用索…

我去,怎么http全变https了

项目场景&#xff1a; 在公司做的一个某地可视化项目。 部署采用的是前后端分离部署&#xff0c;图片等静态资源请求一台minio服务器。 项目平台用的是http 图片资源的服务器用的是https 问题描述 在以https请求图片资源时&#xff0c;图片请求成功报200。 【现象1】: 继图…

51单片机和STM32区别

51单片机和 STM32 区别 51单片机和 STM32 是两种常见的微控制器&#xff0c;它们在架构、性能、外设接口、功耗和开发环境等方面有所不同。 1. 架构差异 51单片机基于传统的哈佛总线结构&#xff0c;采用 CISC 架构&#xff0c;而 STM32 基于 ARM Cortex-M 系列的32位处理器核…

Unity横版动作游戏 -瓦片地形和动画瓦片

(规则瓦片)瓦片地形和动画瓦片 准备阶段 在Tilemap中创建一个新的文件夹起名叫做Rule Tile&#xff0c;创建一个Rule Tile&#xff0c;用来设置瓦片地形&#xff0c;我们将用他来绘制地形图&#xff0c;类似于Godot中的瓦片地形。 这里给他取名为了Ground 1&#xff0c;用于创…

windows10 安装CUDA教程

如何在windows10系统上安装CUDA? 1、查看电脑的NVIDIA版本 nvidia-smi 2、官网下载所需CUDA版本 官网地址:https://developer.nvidia.com/cuda-toolkit-archive 我们所安装的CUDA版本需要小于等于本机电脑的NVIDIA版本。推荐使用迅雷下载,速度会更快哦。 3、安装步骤

React 学习——组件内通信(父 <=> 子)

1、父组件参数传递到子组件&#xff08;父>子&#xff09; function Son(props){console.log(props);// props.name hello;//props传递过来的属性是只读的&#xff0c;想修改的话去父组件修改&#xff1b;&#xff08;简单理解&#xff1a;谁的东西谁修改&#xff09;retur…