Disclosure of Invention
The invention provides a short link generation method, a short link generation device and a short link generation server, which enable a converted short link to have uniqueness when a long link is converted into a short link, thereby improving the accuracy of access.
The invention provides a short link generation method, which is applied to a server and comprises the following steps:
determining a first character string corresponding to the long chain connection to be converted according to the identification of the server;
distributing a corresponding second character string for the long chain connection to be converted; the second character string corresponding to the long chain connection to be converted is different from the second character string corresponding to other long chains in the server;
and generating a short chain connection corresponding to the long chain connection to be converted according to the domain name corresponding to the long chain connection to be converted, the first character string and the second character string.
Optionally, the determining, according to the identifier of the server, a first character string corresponding to the long link to be converted includes:
acquiring the identifier of the server from a database cluster;
and shortening the identifier of the server to obtain a first character string corresponding to the long chain connection to be converted.
Optionally, the allocating a corresponding second character string for the long link to be converted includes:
acquiring a preset character; the preset character is a sequence value of the server for processing the long link to be converted, or the preset character is a random value distributed by the server for the long link to be converted;
and shortening the preset character to obtain a second character string corresponding to the long chain to be converted.
Optionally, the generating a short link corresponding to the long link to be converted according to the domain name corresponding to the long link to be converted, the first character string and the second character string includes:
sequentially splicing the domain name, the first character string and the second character string to obtain a first splicing result;
and determining the first splicing result as a short link corresponding to the long link to be converted.
Optionally, the generating a short link corresponding to the long link to be converted according to the domain name corresponding to the long link to be converted, the first character string and the second character string includes:
sequentially splicing the domain name, the second character string and the first character string to obtain a second splicing result;
and determining the second splicing result as a short link corresponding to the long link to be converted.
Optionally, the generating a short link corresponding to the long link to be converted according to the domain name corresponding to the long link to be converted, the first character string and the second character string includes:
calculating a third character string corresponding to the long chain connection to be converted by adopting a message digest algorithm;
sequentially splicing the domain name, the first character string, the second character string and the third character string to obtain a third splicing result;
and determining the third splicing result as a short link corresponding to the long link to be converted.
Optionally, the method further includes:
and establishing and storing a mapping relation between the long chain connection to be converted and the corresponding short chain connection.
Optionally, the database cluster is a Redis cluster.
The invention provides a short link generation device, which comprises:
the determining module is used for determining a first character string corresponding to the long chain connection to be converted according to the identifier of the device;
the distribution module is used for distributing a corresponding second character string for the long chain connection to be converted; the second character string corresponding to the long chain connection to be converted is different from the second character string corresponding to other long chain connections in the device;
and the generating module is used for generating a short link corresponding to the long link to be converted according to the domain name corresponding to the long link to be converted, the first character string and the second character string.
Optionally, the determining module is specifically configured to:
obtaining an identification of the device from a database cluster; and the mark of the device is shortened to obtain a first character string corresponding to the long chain connection to be converted. Optionally, the database cluster is a Redis cluster.
Optionally, the allocation module is specifically configured to:
acquiring a preset character; the preset character is a sequence value of the device for processing the long link to be converted, or the preset character is a random value distributed by the device for the long link to be converted; and shortening the preset character to obtain a second character string corresponding to the long chain to be converted.
Optionally, the generating module is specifically configured to:
sequentially splicing the domain name, the first character string and the second character string to obtain a first splicing result; and determining the first splicing result as a short link corresponding to the long link to be converted.
Optionally, the generating module is specifically configured to:
sequentially splicing the domain name, the second character string and the first character string to obtain a second splicing result; and determining the second splicing result as a short link corresponding to the long link to be converted.
Optionally, the generating module is specifically configured to:
calculating a third character string corresponding to the long chain connection to be converted by adopting a message digest algorithm; sequentially splicing the domain name, the first character string, the second character string and the third character string to obtain a third splicing result; and determining the third splicing result as a short link corresponding to the long link to be converted.
Optionally, the apparatus further comprises:
and the storage module is used for establishing and storing the mapping relation between the long link to be converted and the corresponding short link.
The invention provides a server, which is characterized by comprising a memory and a processor; the memory is connected with the processor;
the memory is used for storing a computer program;
the processor is configured to implement the short link generation method as described in any one of the above when the computer program is executed.
The invention provides a storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements a short link generation method as defined in any one of the above.
The invention provides a short link generation method, a short link generation device and a server.A first character string corresponding to a long link to be converted is determined according to an identifier of the server; and distributing a corresponding second character string for the long chain connection to be converted; the second character string corresponding to the long chain connection to be converted is different from the second character string corresponding to other long chain connections in the server; and generating a short chain connection corresponding to the long chain connection to be converted according to the domain name, the first character string and the second character string corresponding to the long chain connection to be converted. Therefore, when the short link corresponding to the long link to be converted is generated, the short link corresponding to the long link to be converted can be determined according to the first character string and the second character string, and the uniqueness of the short link corresponding to the long link to be converted can be ensured due to the unique combination of the first character string and the second character string, so that the accuracy of access is improved.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
FIG. 1 is a schematic flow diagram of a short chain joint generation method provided by the present invention. The execution subject of the method is a server, and the server can be realized by software and/or hardware. As shown in fig. 1, the method of this embodiment may include:
s101, determining a first character string corresponding to the long chain connection to be converted according to the identification of the server.
It will be appreciated that the identity of the server is unique. For example, the identifier of the server may be an identifier set before the server leaves a factory, or may be an identifier allocated to the server when the server is used, as long as the server can be uniquely identified.
In this embodiment, the server has an external interface to provide a service for converting a long link into a short link, and the user can send a request to the server by calling the interface, where the request includes the long link to be converted. After receiving the request, the server firstly determines a first character string corresponding to the long link to be converted according to the identifier of the server, wherein the identifier of the server is the unique identifier of the server.
At present, systems such as internet application and the like mostly adopt a distributed architecture, a plurality of servers can provide services in one application system, for example, a service for converting a long link into a short link can be provided by a plurality of servers, the number of the servers can be dynamically changed, namely, dynamic expansion or contraction, and each server has different server identifications. When the user calls the interface to use the service of converting the long link into the short link, the request sent by the user is distributed to one of the servers, and the first character string generated by the server according to the identification of the server can be used for distinguishing the server from other servers in the plurality of servers.
When a server processes a plurality of long links, if the first character strings corresponding to the long links are determined only according to the identification of the server, the first character strings corresponding to the long links are the same, and at this time, the uniqueness of the short links corresponding to the long links cannot be guaranteed, so that the short links corresponding to the long links need to be determined together with the second character strings, that is, the following steps S102 to S103 are executed:
and S102, distributing a corresponding second character string for the long chain connection to be converted.
And the second character string corresponding to the long link to be converted is different from the second character string corresponding to other long links in the server.
In the above S101, the server may be distinguished from other servers in the plurality of servers by the first character string, on this basis, in order to ensure uniqueness of the short link corresponding to the long link to be converted, a second character string needs to be further determined, and since the second character string corresponding to the long link to be converted is different from the second character strings corresponding to other long links in the server, the short link corresponding to the long link to be converted may be distinguished from the short links corresponding to other long links in the server by the second character string, thereby ensuring uniqueness of the short link corresponding to the long link to be converted.
S103, generating a short chain connection corresponding to the long chain connection to be converted according to the domain name, the first character string and the second character string corresponding to the long chain connection to be converted.
The long link to be converted consists of a domain name and a long string of characters, the domain name is taken out, and a short link corresponding to the long link to be converted is generated according to the domain name, the first character string and the first character string. The server can be distinguished from other servers in the servers by the first character string, and the second character string corresponding to the long chain connection to be converted is different from the second character strings corresponding to other long chain connections in the server, so that the short chain connection corresponding to the long chain connection to be converted is distinguished from the short chain connection corresponding to other long chain connections in the server by the second character string, and the uniqueness of the short chain connection corresponding to the long chain connection to be converted is ensured.
In the short link generation method provided by this embodiment, a first character string corresponding to a long link to be converted is determined according to an identifier of a server; and distributing a corresponding second character string for the long chain connection to be converted; the second character string corresponding to the long chain connection to be converted is different from the second character string corresponding to other long chain connections in the server; and generating a short chain connection corresponding to the long chain connection to be converted according to the domain name, the first character string and the second character string corresponding to the long chain connection to be converted. Therefore, when the short link corresponding to the long link to be converted is generated, the short link corresponding to the long link to be converted can be determined according to the first character string and the second character string, and the uniqueness of the short link corresponding to the long link to be converted can be ensured due to the unique combination of the first character string and the second character string, so that the accuracy of access is improved.
Based on the embodiment shown in fig. 1, when determining the first character string corresponding to the long link to be converted according to the identifier of the server in S101, the determining may include:
acquiring an identifier of a server from a database cluster; and shortening the identifier of the server to obtain a first character string corresponding to the long chain connection to be converted.
In this embodiment, the identifier of the server may be stored in a database cluster connected to the servers, that is, the database cluster allocates a corresponding server identifier to each server, so as to ensure that each server has a unique identifier, and when the number of servers increases, the database may automatically allocate an identifier to the newly added server. Alternatively, the database may be a Key-Value (Key-Value) type database, such as Redis, or may be a database in other forms, which is not specifically limited in the present invention. The server can perform data preloading when being started, and acquire a corresponding server identifier from the database cluster. In this way, when the server performs conversion of the long link, the first character string can be generated according to the server identifier acquired from the database in advance.
Further, a character set may be preset in the server, for example, the character set may include 62 characters from a to Z, a to z and from 0 to 9, and the character set is used to shorten the identifier of the server. Taking the character set of 62 characters as an example, arranging the 62 characters in sequence, dividing the identifier of the server by 62, replacing the quotient and the remainder in the obtained result with the characters in the corresponding sequence in the character set respectively, if the quotient is greater than 62, continuing dividing by 62, replacing the quotient and the remainder with the characters in the corresponding sequence in the character set respectively until the quotient is less than 62, and obtaining the first character string. And the identifier of the server is shortened by adopting the character set, so that the obtained first character string is shorter, and the short chain length corresponding to the long chain connection to be converted is shorter.
After the identifier of the server is shortened to obtain the first character string corresponding to the long link to be converted, in order to ensure the uniqueness of the short link, a corresponding second character string needs to be allocated to the long link to be converted, and S102 allocates a corresponding second character string to the long link to be converted, which may include:
acquiring a preset character string; the preset character string is a sequence value of the server for processing the long link to be converted, or the preset character string is a random value distributed by the server for the long link to be converted; and shortening the preset character string to obtain a second character string corresponding to the long chain to be converted.
In this embodiment, the preset character strings obtained by the server when performing long link conversion each time are different, and the preset character strings may be numerical values that sequentially increase according to the order of processing the long links to be converted, for example, a value of the preset character string increases by one or two or the preset character string may be a preset memory value in the server when processing the long links to be converted each time, and the server performs a self-increment calculation on the preset memory value each time performing long link conversion, thereby ensuring that the values obtained each time are different; the preset character string can also be an assigned random value, and the random value assigned by the server every time is different. And then, shortening the preset character string to obtain a second character string corresponding to the long chain to be converted. The method for shortening the preset character string is the same as the method for using the character set in the above embodiment, and details are not repeated here. And the preset character string is shortened by adopting the character set, so that the obtained second character string is shorter, and the short chain length corresponding to the long chain connection to be converted is shorter. The initial value of the preset character string can be set according to actual needs, so that the length of the second character string obtained when long link conversion is carried out each time is the same.
After the first character string and the second character string corresponding to the long chain connection to be converted are obtained, S103 may be executed to generate the short chain connection corresponding to the long chain connection to be converted according to the domain name, the first character string and the second character string corresponding to the long chain connection to be converted. For example, when the short link corresponding to the long link to be converted is generated according to the domain name corresponding to the long link to be converted, the first character string and the second character string, at least three possible implementation manners may be included, and the three possible implementation manners will be described in detail below.
In one possible implementation mode, sequentially splicing the domain name, the first character string and the second character string to obtain a first splicing result; and determining the first splicing result as a short link corresponding to the long link to be converted.
In another possible implementation manner, the domain name, the second character string and the first character string are spliced in sequence to obtain a second splicing result; and determining the second splicing result as a short link corresponding to the long link to be converted.
In the two possible implementation manners, the first character string is obtained according to the identifier of the server, so that the first character string can distinguish one server from other servers, meanwhile, the second character strings determined on one server are different, so that the first character string and the second character string are spliced to determine the only one character string in the multiple servers, the domain name, the first character string and the second character string are spliced to obtain the short link corresponding to the long link to be converted, and the uniqueness of the short link can be ensured.
In another possible implementation manner, a Message Digest (MD) algorithm is used to calculate a third string corresponding to the long chain to be converted; sequentially splicing the domain name, the first character string, the second character string and the third character string to obtain a third splicing result; and determining the third splicing result as a short link corresponding to the long link to be converted.
In practical application, a fifth version of the MD algorithm, that is, the MD5 algorithm, may be used to encrypt the long link to be converted to obtain 32-bit numbers, and the 32-bit numbers are segmented according to the length 8 to obtain 4 sets of data. The 16-ary number of each set of data performs a bitwise AND operation on 0x3FFFFFFF, with low 30-bit digital fidelity. The 30-bit data is cycled for 6 times, each cycle is shifted to the right by 5 bits, the 5 bits shifted to the right are scaled down by 62 bits through a 0x0000003D bitwise AND operation, and the 6-bit scaled-down number is obtained after 6 cycles. Each set of data is processed separately to obtain 4 sets of 6-bit reduced numbers, and any one of the 4 sets of 6-bit reduced numbers can be selected as the third string, and preferably, the first set of reduced numbers can be selected as the third string.
And sequentially splicing the domain name, the first character string, the second character string and the third character string to obtain a splicing result, namely the short link corresponding to the long link to be converted. Optionally, when the domain name, the first character string, the second character string, and the third character string are spliced, different splicing orders may be adopted for the first character string, the second character string, and the third character string, which is not limited in this invention.
Illustratively, if the long link to be converted is: https:// cc.xxxxx.com/kc/base/html/app _ open/detail-sendmsg. htmlklid. 1812140017540050748& isPublished. 1& isBackStage. 0& reginId. 97& ismissing. splitlld. 1812140017180050719& channelCode. CHNL 00000001. the domain name in the long link to be converted is cc.xxxxx.com. the first string determined using the method of this embodiment is q8, the second string is gLA9q, and the third string is bQrAzm. And taking the result https:// cc. xxxxx. com/s/q8gLA9qbQrAzm of the sequential splicing of the domain name, the first character string, the second character string and the third character string as the short link corresponding to the long link to be converted, wherein the sequential result of the first character string, the second character string and the third character string can be called as a short link code, namely q8gLA9qbQrAzm is a short link code.
In this embodiment, a third character string corresponding to the long chain connection to be converted is calculated by using an MD5 algorithm, and the domain name, the first character string, the second character string, and the third character string are spliced into a short chain connection corresponding to the long chain connection to be converted. In the method, if the first character string obtained by the server is abnormal, for example, when the server obtains the identifier of the server from the database cluster, the identifier obtained by the server is abnormal due to the database abnormality, for example, the identifier is empty, only the first character string and the second character string cannot uniquely identify a short link corresponding to a long link to be converted, and therefore, the short link corresponding to the long link to be converted is distinguished by adding the third character string, and high availability of the service is ensured.
In another embodiment, the short link generation method provided by the present invention further comprises: and establishing and storing a mapping relation between the long chain connection to be converted and the corresponding short chain connection. Therefore, when the user accesses through the short link, the long link corresponding to the short link can be directly obtained from the mapping relation, and then the content corresponding to the long link is accessed, so that the accessing efficiency is improved.
The following exemplifies a usage scenario of the short link generation method provided by the present invention in conjunction with practical applications.
In practical applications, after the long link is converted into the short link, the user can access the short link. Two services can thus be designed: the system comprises a front-end service and a back-end service, wherein the front-end service receives a request for converting a long link into a long link and skips the long link, and the back-end service provides a service for converting the long link into a short link and a service for converting the short link into the long link.
For example, when the connection needs to be released to the user by sending the short message, the long-link switching short-link service is firstly called through the interface, the short link corresponding to the long link is obtained, and the obtained short link is added into the short message template and sent to the user.
When a user accesses corresponding content through a short link in a received short message, in order to ensure that the same response is required when the user accesses different short links, a reverse proxy is used for intercepting related paths and reversely acting the related paths to the same inlet, a short link switching long link service is called when an inlet page is loaded, a long link address is obtained, and the long link address is requested in a page redirection mode.
Fig. 2 is a schematic structural diagram of a short link generation device provided by the present invention. As shown in fig. 2, the short link generation device 20 includes:
the determining module 201 is configured to determine, according to the identifier of the short link generating device 20, a first character string corresponding to the long link to be converted.
The distribution module 202 is configured to distribute a corresponding second character string to the long link to be converted; the second character string corresponding to the long chain link to be converted is different from the second character string corresponding to the other long chain links in the short chain link generation device 20.
The generating module 203 is configured to generate a short link corresponding to the long link to be converted according to the domain name corresponding to the long link to be converted, the first character string, and the second character string.
Optionally, the determining module 201 is specifically configured to:
obtaining an identification of the short link generator 20 from the database cluster; and shortening the mark of the short link generation device 20 to obtain a first character string corresponding to the long link to be converted. Optionally, the database cluster is a Redis cluster.
Optionally, the allocating module 202 is specifically configured to:
acquiring a preset character; the preset character is a sequence value of the short link generation device 20 for processing the long link to be converted, or the preset character is a random value allocated by the short link generation device 20 for the long link to be converted; and shortening the preset character to obtain a second character string corresponding to the long chain to be converted.
Optionally, the generating module 203 is specifically configured to:
sequentially splicing the domain name, the first character string and the second character string to obtain a first splicing result; and determining the first splicing result as a short link corresponding to the long link to be converted.
Optionally, the generating module 203 is specifically configured to:
sequentially splicing the domain name, the second character string and the first character string to obtain a second splicing result; and determining the second splicing result as a short link corresponding to the long link to be converted.
Optionally, the generating module 203 may be further specifically configured to:
calculating a third character string corresponding to the long chain connection to be converted by adopting a message digest algorithm; sequentially splicing the domain name, the first character string, the second character string and the third character string to obtain a third splicing result; and determining the third splicing result as a short link corresponding to the long link to be converted.
Optionally, the short link generation device 20 further includes:
the storage module 204 is configured to establish and store a mapping relationship between the long link to be converted and the corresponding short link.
The short link generation device provided by the embodiment is used for realizing the short link generation method in any embodiment, the realization principle and the technical effect are the same, and the details are not repeated here.
Fig. 3 is a schematic structural diagram of a server according to the present invention. As shown in fig. 3, the server 30 includes a memory 301 and a processor 302; the memory 301 is connected to the processor 302.
The memory 301 is used to store computer programs.
The processor 302 is configured to implement the short link generation method in any of the above embodiments when the computer program is executed.
The server provided by this embodiment is used to implement the short link generation method in any of the above embodiments, and the implementation principle and technical effect are the same, which are not described herein again.
The present invention provides a storage medium having stored thereon a computer program which, when executed by a processor, implements the short link generation method of any of the above embodiments.
Those of ordinary skill in the art will understand that: all or a portion of the steps of implementing the above-described method embodiments may be performed by hardware associated with program instructions. The program may be stored in a computer-readable storage medium. When executed, the program performs steps comprising the method embodiments described above; and the aforementioned storage medium includes: various media that can store program codes, such as ROM, RAM, magnetic or optical disks.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present invention.