日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

PHP 服務實現(xiàn)性能剖析、跟蹤和可觀察性14444444444444】=102102102102102102102102102102102102102102102102實踐

來源: 責編: 時間:2024-06-11 17:52:20 215觀看
導讀簡介鏈路追蹤Tracing Analysis為分布式應用的開發(fā)者提供了完整的調用鏈路還原、調用請求量統(tǒng)計、鏈路拓撲、應用依賴分析等工具,可以幫助開發(fā)者快速分析和診斷分布式應用架構下的性能瓶頸,提高微服務時代下的開發(fā)診斷效

簡介

鏈路追蹤Tracing Analysis為分布式應用的開發(fā)者提供了完整的調用鏈路還原、調用請求量統(tǒng)計、鏈路拓撲、應用依賴分析等工具,可以幫助開發(fā)者快速分析和診斷分布式應用架構下的性能瓶頸,提高微服務時代下的開發(fā)診斷效率。bp428資訊網——每日最新資訊28at.com

官方地址:https://github.com/openzipkin/zipkinbp428資訊網——每日最新資訊28at.com

Zipkin運行架構

圖片圖片bp428資訊網——每日最新資訊28at.com

產品架構(鏈路追蹤)

圖片圖片bp428資訊網——每日最新資訊28at.com

鏈路追蹤的主要工作流程如下

  1. 客戶側的應用程序通過集成鏈路追蹤的多語言客戶端SDK上報服務調用數(shù)據(jù)。鏈路追蹤支持多種開源社區(qū)的SDK,且支持OpenTracing標準。
  2. 數(shù)據(jù)上報至鏈路追蹤控制臺后,鏈路追蹤組件進行實時聚合計算和持久化,形成鏈路明細、性能總覽、實時拓撲等監(jiān)控數(shù)據(jù)。您可以據(jù)此進行問題排查與診斷。
  3. 調用鏈數(shù)據(jù)可對接下游阿里云產品,例如LogSearch、CloudMonitor、MaxCompute等,用于離線分析、報警等場景。

業(yè)務場景

隨著業(yè)務越來越復雜,系統(tǒng)也隨之進行各種拆分,特別是隨著微服務架構和容器技術的興起,看似簡單的一個應用,后臺可能有幾十個甚至幾百個服務在支撐;一個前端的請求可能需要多次的服務調用最后才能完成;當請求變慢或者不可用時,我們無法得知是哪個后臺服務引起的,這時就需要解決如何快速定位服務故障點,zipkin分布式跟蹤系統(tǒng)就能很好的解決這樣的問題。bp428資訊網——每日最新資訊28at.com

圖片圖片bp428資訊網——每日最新資訊28at.com

請求&響應

微服務架構下,一次請求后端會經歷多個服務調用(所有請求鏈有相同的traceId和不同的spanId),都會沿著traceText帶到每一個服務中。bp428資訊網——每日最新資訊28at.com

數(shù)據(jù)是如何上報的?

直接上報數(shù)據(jù)

圖片圖片bp428資訊網——每日最新資訊28at.com

bp428資訊網——每日最新資訊28at.com

不通過Agent而直接上報數(shù)據(jù)的原理(傳統(tǒng)框架。PHP-FPM + Nginx模式)bp428資訊網——每日最新資訊28at.com

  • ThinkPHP6.0
  • Laravel
  • Yii2.0

通過Agent上報數(shù)據(jù)

圖片圖片bp428資訊網——每日最新資訊28at.com

通過Agent上報數(shù)據(jù)的原理(現(xiàn)代化框架。命令行模式)。bp428資訊網——每日最新資訊28at.com

  • webman
  • Swoole

安裝

通過composer安裝:bp428資訊網——每日最新資訊28at.com

composer require openzipkin/zipkin

使用

創(chuàng)建Tracer

Tracer對象可以用來創(chuàng)建Span對象(記錄分布式操作時間)。Tracer對象還配置了上報數(shù)據(jù)的網關地址、本機IP、采樣頻率等數(shù)據(jù),您可以通過調整采樣率來減少因上報數(shù)據(jù)產生的開銷。bp428資訊網——每日最新資訊28at.com

function create_tracing($endpointName, $ipv4){    $endpoint = Endpoint::create($endpointName, $ipv4, null, 2555);    /* Do not copy this logger into production.     * Read https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#log-levels     */    $logger = new /Monolog/Logger('log');    $logger->pushHandler(new /Monolog/Handler/ErrorLogHandler());    $reporter = new Zipkin/Reporters/Http(/Zipkin/Reporters/Http/CurlFactory::create());    $sampler = BinarySampler::createAsAlwaysSample();    $tracing = TracingBuilder::create()        ->havingLocalEndpoint($endpoint)        ->havingSampler($sampler)        ->havingReporter($reporter)        ->build();    return $tracing;}

記錄請求數(shù)據(jù)

$rootSpan = $tracer->newTrace();$rootSpan->setName('encode');$rootSpan->start();try {  doSomethingExpensive();} finally {  $rootSpan->finish();}

以上代碼用于記錄請求的根操作,如果需要記錄請求的上一步和下一步操作,則需要傳入上下文。示例:bp428資訊網——每日最新資訊28at.com

$span = $tracer->newChild($parentSpan->getContext());$span->setName('encode');$span->start();try {  doSomethingExpensive();} finally {  $span->finish();}

總體流程如下

Client Span                                                Server Span┌──────────────────┐                                       ┌──────────────────┐│                  │                                       │                  ││   TraceContext   │           Http Request Headers        │   TraceContext   ││ ┌──────────────┐ │          ┌───────────────────┐        │ ┌──────────────┐ ││ │ TraceId      │ │          │ X-B3-TraceId      │        │ │ TraceId      │ ││ │              │ │          │                   │        │ │              │ ││ │ ParentSpanId │ │ Inject   │ X-B3-ParentSpanId │Extract │ │ ParentSpanId │ ││ │              ├─┼─────────>│                   ├────────┼>│              │ ││ │ SpanId       │ │          │ X-B3-SpanId       │        │ │ SpanId       │ ││ │              │ │          │                   │        │ │              │ ││ │ Sampled      │ │          │ X-B3-Sampled      │        │ │ Sampled      │ ││ └──────────────┘ │          └───────────────────┘        │ └──────────────┘ ││                  │                                       │                  │└──────────────────┘                                       └──────────────────┘

webman應用

1. 開通ARMS

開通ARMS地址 https://arms.console.aliyun.com/ (一般有15天試用)bp428資訊網——每日最新資訊28at.com

2. 獲得數(shù)據(jù)上報接入點url

進入 https://tracing.console.aliyun.com/#/globalSetting/cn-hangzhou/process 按照圖示獲得接入點url地址。bp428資訊網——每日最新資訊28at.com

圖片圖片bp428資訊網——每日最新資訊28at.com

如果你的服務器在阿里云上可以用阿里云vpc網絡接入點,本示例用的是阿里云公網接入點。bp428資訊網——每日最新資訊28at.com

安裝

通過composer安裝:bp428資訊網——每日最新資訊28at.com

composer require openzipkin/zipkin

使用

1. 編寫中間件

鏈路監(jiān)控中間件 app/middleware/ArmsMiddleware.phpbp428資訊網——每日最新資訊28at.com

<?php/** * @desc 全鏈路監(jiān)控中間件 * @author Tinywan(ShaoBo Wan) * @date 2021/12/6 14:06 */declare(strict_types=1);namespace app/middleware;use Monolog/Handler/ErrorLogHandler;use Monolog/Logger;use support/Log;use think/facade/Db;use Webman/MiddlewareInterface;use Webman/Http/Response;use Webman/Http/Request;use Zipkin/Reporters/Http;use Zipkin/TracingBuilder;use Zipkin/Samplers/BinarySampler;use Zipkin/Endpoint;use Workerman/Timer;use const Zipkin/Tags/SQL_QUERY;class ArmsMiddleware implements MiddlewareInterface{    /**     * @desc: 方法描述     * @param Request $request     * @param callable $next     * @return Response     * @author Tinywan(ShaoBo Wan)     */    public function process(Request $request, callable $next) : Response    {        static $tracing = null, $tracer = null;        if (!$tracing) {            $endpoint = Endpoint::create('開源技術小棧', $request->getRealIp(), null, 2555);            $logger = new Logger('log');            $logger->pushHandler(new ErrorLogHandler());            $reporter = new Http(['endpoint_url' => config('security')['endpoint_url']]);            $sampler = BinarySampler::createAsAlwaysSample();            $tracing = TracingBuilder::create()                ->havingLocalEndpoint($endpoint)                ->havingSampler($sampler)                ->havingReporter($reporter)                ->build();            $tracer = $tracing->getTracer();            // 55秒上報一次,盡量將上報對業(yè)務的影響減少到最低            Timer::add(55, function () use ($tracer) {                $tracer->flush();            });            register_shutdown_function(function () use ($tracer) {                $tracer->flush();            });        }        $rootSpan = $tracer->newTrace();        $rootSpan->setName($request->controller."::".$request->action);        $rootSpan->start();        $request->rootSpan = $rootSpan;        $request->tracer = $tracer;        $result = $next($request);        // 統(tǒng)計sql(日志在內存)        if (class_exists(Db::class)) {            $logs = Db::getDbLog(true);            if (!empty($logs['sql'])) {                foreach ($logs['sql'] as $sql) {                    $sqlSpan = $tracer->newChild($rootSpan->getContext());                    $sqlSpan->setName(SQL_QUERY);                    $sqlSpan->start();                    $sqlSpan->tag('db.statement', $sql);                    $sqlSpan->finish();                }            }        }        $rootSpan->finish();        return $result;    }}

2. 配置中間件

在 config/middleware.php 中添加全局中間件如下:bp428資訊網——每日最新資訊28at.com

return [    '' => [        /app/middleware/ArmsMiddleware::class,    ],    ...];

3. 查看監(jiān)控

訪問地址 https://tracing.console.aliyun.com/ ,效果類似如下:bp428資訊網——每日最新資訊28at.com

圖片圖片bp428資訊網——每日最新資訊28at.com

接口監(jiān)控

圖片圖片bp428資訊網——每日最新資訊28at.com

數(shù)據(jù)庫監(jiān)控

圖片 圖片 bp428資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-93091-0.htmlPHP 服務實現(xiàn)性能剖析、跟蹤和可觀察性14444444444444】=102102102102102102102102102102102102102102102102實踐

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 震驚!用 Suspense 解決請求依賴的復雜場景居然這么簡單!

下一篇: 嚴重!Spring AOP Bug導致切面重復執(zhí)行

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 闽侯县| 威远县| 博乐市| 武陟县| 丰宁| 麦盖提县| 托里县| 裕民县| 宣威市| 大理市| 开封市| 密山市| 潼关县| 天柱县| 台州市| 正镶白旗| 苍梧县| 阳曲县| 新郑市| 宁都县| 彰化市| 岚皋县| 双江| 斗六市| 谢通门县| 监利县| 阿坝县| 驻马店市| 龙南县| 苗栗县| 团风县| 肃南| 措勤县| 常德市| 车险| 扶沟县| 肥东县| 涿州市| 思南县| 宣恩县| 潮州市|