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

news/2024/11/15 1:58:58/

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/news/1501944.html

相关文章

PHP超级全局变量

PHP 中的超级全局变量&#xff08;Superglobals&#xff09;是 PHP 4.1.0 引入的一个特性&#xff0c;它们在脚本的全部作用域中都是可用的。这意味着一旦一个脚本被载入&#xff0c;这些变量就可以在脚本的任何地方被访问&#xff0c;无论是在函数内部还是外部。PHP 提供了几个…

日常踩坑---ljmp在BIOS中的使用

在 x86 架构&#xff0c;ljmp 指令用于执行远跳转&#xff0c;该指令的操作数是一个远指针&#xff0c;由一个16位的段选择器和一个16位的偏移量组成&#xff0c;因此实模式下ljmp 指令的字节码长度是5个字节。 1个字节&#xff1a;操作码&#xff08;opcode&#xff09;&…

ARM系列运行异常排查

一、断点指令BKPT BKPT指令产生软件断点中断&#xff0c;可用于程序的调试。它使处理器停止执行正常指令&#xff08;使处理器中止预取指&#xff09;而进入相应的调试程序。 BKPT指令的格式为&#xff1a;BKPT 16位的立即数 二、使用BKPT进行软件异常定位 假设异常发生后…

CentOS磁盘分区和挂载

记一次Linux上磁盘分区和挂载的日记。 #查看磁盘 [rootlocalhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 50G 0 disk ├─sda1 8:1 0 600M 0 part /boot/efi ├─sda2 8:2 0 1G 0 part /b…

MySQL --- 表的操作

在对表进行操作时&#xff0c;需要先选定操作的表所在的数据库&#xff0c;即先执行 use 数据库名; 一、创建表 create table 表名( field1 datatype, field2 datatype, field3 datatype ) character set 字符集 collate 校验规则 engine 存储引擎 ; 说明&#xff1a…

Spark实时(一):StructuredStreaming 介绍

文章目录 Structured Streaming 介绍 一、SparkStreaming实时数据处理痛点 1、复杂的编程模式 2、SparkStreaming处理实时数据只支持Processing Time 3、微批处理,延迟高 4、精准消费一次问题 二、StructuredStreaming架构与场景应用 三、​​​​​​​​​​​​​​…

SSCI 二区正刊 绿色金融、财政、经济、债务、成本、创新题目:

1金融科技能提升企业的双元创新能力吗&#xff1f;组织韧性xxxxx 2从财政分权到经济高质量发展&#xff1a;税收征管强度xxxxxxx 3企业智能化转型、债务融资成本与绿色xxxx 绿色金融改革能否促进地方经济高质量发展&#xff1a;基于绿色金融改革创新试验区的准xxxx 4绿色金融改…

笑谈“八股文”,人生不成文

一、“八股文”在实际工作中是助力、阻力还是空谈&#xff1f; 作为现在各类大中小企业面试程序员时的必问内容&#xff0c;“八股文”似乎是很重要的存在。但“八股文”是否能在实际工作中发挥它“敲门砖”应有的作用呢&#xff1f;有IT人士不禁发出疑问&#xff1a;程序员面试…