php install protobuf, install gRPC extension, enable GMP (math function)

When I first came into contact with the database, I have been thinking about a question: If the amount of data is huge, is there any magical tool for data storage and interpretation that can help me. When I grew up, I learned that there was an open source artifact called protobuf in Google. When I first knew it, I was still in a daze. what is this? what is the benefit? How to install it? How to use it? , This article mainly answers these questions.

Based on the fact that I have checked a lot of information, most of the information is copied and copied. I have no choice but to summarize it myself.

What is protobuf and how did it come from?

Simply put, it is doing the same thing as xml, storing the information of a certain data structure in a certain format. Mainly used for data storage, transmission protocol format and other occasions.

Professional answer: 
Protocol Buffers is a lightweight and efficient structured data storage format, which can be used for the serialization of structured data and is very suitable for data storage or RPC data exchange format. It can be used for language-independent, platform-independent, and extensible serialized structured data formats in communication protocols, data storage and other fields.

Some classmates asked, why don’t you build wheels and build a protobuf if you already have xml? .

The root cause is still: XML performance is not good.

1. Time dimension: The overhead of xml formatting (serialization) is good; but the overhead of XML parsing (deserialization) is just fine. 
2. Spatial dimension: In order to have better readability, the XML format introduces some redundant text information, so the space overhead is not very good.

This is unbearable for the large number of googles. Must be a performance that is better than xml.

What are the benefits of protobuf?

1. The performance of serialization and deserialization of data structure is better than that of xml 
2. Code generation mechanism, which is very convenient to use (mentioned later) 
3. Star projects are used: It is understood that Google, Sina, Meipai, etc. Protobuf is used, so it is certainly not wrong to move to the project.

How to install protobuf?

Install protobuf

protobuf
wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz

 

 

 

 

You can see the following installation process:

su root

wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
 
tar zxvf protobuf-2.6.1.tar.gz
 
cd protobuf-2.6.1/
 
./configure --prefix=/usr/local/protobuf
 
make && make install
 
export PATH=/usr/local/protobuf/bin:$PATH
 
protoc --version

 

Next install the protobuf extension of php:

 

 

 

sudo vim /etc/php/7.2/mods-available/protobuf.ini添加extension=protobuf.so

Restart php

You can see a successful installation!

Then enter the installation dependency:

 

Create the cbstest.proto file:

 

message Person {
    required string name = 1;
    required int32 id = 2;
    optional string email = 3;
    optional double money = 4;
}

Execute the php ./protoc-gen-php.php cbstest.proto command:

You can see that the Person.php file is generated under the directory

<?php
/**
 * Auto generated from cbstest.proto at 2021-03-05 04:02:43
 */

namespace {
/**
 * Person message
 */
class Person extends \ProtobufMessage
{
    /* Field index constants */
    const NAME = 1;
    const ID = 2;
    const EMAIL = 3;
    const MONEY = 4;

    /* @var array Field descriptors */
    protected static $fields = array(
        self::NAME => array(
            'name' => 'name',
            'required' => true,
            'type' => \ProtobufMessage::PB_TYPE_STRING,
        ),
        self::ID => array(
            'name' => 'id',
            'required' => true,
            'type' => \ProtobufMessage::PB_TYPE_INT,
        ),
        self::EMAIL => array(
            'name' => 'email',
            'required' => false,
            'type' => \ProtobufMessage::PB_TYPE_STRING,
        ),
        self::MONEY => array(
            'name' => 'money',
            'required' => false,
            'type' => \ProtobufMessage::PB_TYPE_DOUBLE,
        ),
    );

    /**
     * Constructs new message container and clears its internal state
     */
    public function __construct()
    {
        $this->reset();
    }

    /**
     * Clears message values and sets default ones
     *
     * @return null
     */
    public function reset()
    {
        $this->values[self::NAME] = null;
        $this->values[self::ID] = null;
        $this->values[self::EMAIL] = null;
        $this->values[self::MONEY] = null;
    }

    /**
     * Returns field descriptors
     *
     * @return array
     */
    public function fields()
    {
        return self::$fields;
    }

    /**
     * Sets value of 'name' property
     *
     * @param string $value Property value
     *
     * @return null
     */
    public function setName($value)
    {
        return $this->set(self::NAME, $value);
    }

    /**
     * Returns value of 'name' property
     *
     * @return string
     */
    public function getName()
    {
        $value = $this->get(self::NAME);
        return $value === null ? (string)$value : $value;
    }

    /**
     * Returns true if 'name' property is set, false otherwise
     *
     * @return boolean
     */
    public function hasName()
    {
        return $this->get(self::NAME) !== null;
    }

    /**
     * Sets value of 'id' property
     *
     * @param integer $value Property value
     *
     * @return null
     */
    public function setId($value)
    {
        return $this->set(self::ID, $value);
    }

    /**
     * Returns value of 'id' property
     *
     * @return integer
     */
    public function getId()
    {
        $value = $this->get(self::ID);
        return $value === null ? (integer)$value : $value;
    }

    /**
     * Returns true if 'id' property is set, false otherwise
     *
     * @return boolean
     */
    public function hasId()
    {
        return $this->get(self::ID) !== null;
    }

    /**
     * Sets value of 'email' property
     *
     * @param string $value Property value
     *
     * @return null
     */
    public function setEmail($value)
    {
        return $this->set(self::EMAIL, $value);
    }

    /**
     * Returns value of 'email' property
     *
     * @return string
     */
    public function getEmail()
    {
        $value = $this->get(self::EMAIL);
        return $value === null ? (string)$value : $value;
    }

    /**
     * Returns true if 'email' property is set, false otherwise
     *
     * @return boolean
     */
    public function hasEmail()
    {
        return $this->get(self::EMAIL) !== null;
    }

    /**
     * Sets value of 'money' property
     *
     * @param double $value Property value
     *
     * @return null
     */
    public function setMoney($value)
    {
        return $this->set(self::MONEY, $value);
    }

    /**
     * Returns value of 'money' property
     *
     * @return double
     */
    public function getMoney()
    {
        $value = $this->get(self::MONEY);
        return $value === null ? (double)$value : $value;
    }

    /**
     * Returns true if 'money' property is set, false otherwise
     *
     * @return boolean
     */
    public function hasMoney()
    {
        return $this->get(self::MONEY) !== null;
    }
}
}

 

Then we write a test file: test.php:

 

 After saving, we run it:

 

 

You can see each value obtained after serialization. This can be convenient to use in rpc!

Install grpc by the way

sudo pecl install grpc

Compile all the time, and wait until the end, you will see:

At this time, add grpc.so to php.ini and restart php

You can see that the extension is generated!

Later we will talk about the use of grpc

gmp math function, gmp extension must be installed when using it:

 

sudo apt-get install php7.2-gmp

Restart after installation

 

The gmp mathematical function will be used when using grpc to do the blockchain later.

Guess you like

Origin blog.csdn.net/lchmyhua88/article/details/114393471