Explain PHP generate unique RequestID class related content

Source: Internet
Author: User

This article introduces PHP to generate a unique RequestID class, using the session_create_id () and uniqid () methods, to ensure uniqueness, provide complete code and demonstration, easy to learn to use.

Today's system design generally uses a distributed system, a request may have to invoke several microservices processing, and finally return the results. When there is a problem with the request, it is difficult to track which microservices are having problems.

Each time a request is made to the server, we can add a unique identifier (RequestID)to the access, write the critical data of the request process to the log (for example, parameters at the time of access, MicroServices, data returned at the end of the service, etc.), used for reference when there is a problem with the access, easy to trace to the problem.

For example, a request needs to go through several microservices and return the output

Request->a->b->c-a-> Output

If the access process has no output, or an output error, we can find the a,b,c corresponding log according to RequestID, and check which service is having problems.

The code is as follows:

RequestID.class.php

<?php/** * PHP generated unique RequestID class * date:2018-04-10 * author:fdipzone * version:1.0 * * Description: * PHP implementation generated unique Reque The Stid class, implemented using the session_create_id () and Uniqid () methods, guarantees uniqueness.     * Func: * Public generate generate unique request ID * Private format Request ID */class requestid{//class start/** * Generate unique Request ID * @return String */public static function generate () {//Use SESSION_CREATE_ID () method to create prefix $prefix = SE        ssion_create_id (Date (' ymdhis '));        Use the Uniqid () method to create a unique ID $request _id = strtoupper (MD5 (Uniqid ($prefix, True)));    Format Request ID return Self::format ($request _id);     }/** * Format Request ID * @param string $request _id Request ID * @param Array $format format * @return string        */private static function format ($request _id, $format = ' 8,4,4,4,12 ') {$tmp = array ();        $offset = 0;        $cut = Explode (', ', $format);  Format if ($cut) {foreach ($cut as $v) {$tmp [] = substr ($request _id, $offset, $v) according to the settings;              $offset + = $v;        }}//Add the remainder if ($offset <strlen ($request _id)) {$tmp [] = substr ($request _id, $offset);    } return implode ('-', $tmp); }}//Class end?>

Demo

<?phprequire ' RequestID.class.php ';//Generate 10 Requests idfor ($i =0; $i <10; $i + +) {    echo requestid::generate (). Php_eol;}? >

Output:

16532925-4502- cdad-23a2-463fc7b5803a500b77ad-cd24-0dda-9e6e-2fdf2dd7ca94813143d0-958f-9f56-e04f-679598594452e5ee1b0b-e0d6-3e60-d831-462 C5a262fce79e714b5-a37f-4b5e-4ede-83e18391ebf9e1c440ab-fc2c-ac74-e79a-016fd59d9651ae483861-1040-be8d-e523-d7638d0f0d35bbd7 A03a-36c9-24b7-c453-fb1ddd6e201ebf62c3e6-9c5f-22cb-668d-381863b35268e97e1f44-f048-962a-5bf7-1113727551b1


Note the session_create_id method requires more than php7.1 versions to be used.

This article explains PHP generation unique RequestID class related content, more relevant knowledge, please pay attention to the PHP Chinese web.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.