In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Based on Knative Serverless technology how to achieve a short URL service, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
A short URL, as its name implies, is to use a shorter URL instead of a very long one. The explanation above in Wikipedia goes like this:
Short URL, also known as URL shortening, URL shortening, etc., refers to a technology and service on the Internet, which can provide a very short URL to replace the original possibly longer URL and shorten the long URL address. Users will usually be redirected to the original long URL when they access the shortened URL
Origin
Although the Internet is now very developed, there are still many scenarios that limit the length of what users enter. For example:
The length of Twitter should not exceed 140characters.
Scenarios such as some early BBS articles whose single line length cannot exceed 78 characters
The length of SMS messages of operators cannot exceed 70 words.
At present, most of the contents of many media and e-commerce platforms are generated by multi-person cooperation through more complex systems and frameworks, and it is very common for links to be dozens or even hundreds of characters long. therefore, if you spread the link in the above scenarios to use short URL service is an inevitable result. For example, the following text message screenshots should be familiar to you:
Application scenario
The original intention of the short URL service is to shorten the long url to facilitate dissemination. But in fact, the short URL service can do a lot of other things. Such as the following:
Restrictions on the number of visits, such as only one visit and a denial of service on the second visit
Time limit, for example, access service can only be provided within one week, and service can be denied more than one week.
According to the geographical restrictions of visitors
Access through password
Visit statistics
Peak visit time statistics and so on.
Count some information about visitors, such as:
Source city
Access time
Terminal devices, browsers used
Access source IP
In fact, in marketing activities, we can also generate impassable short web addresses for different channels, so that we can judge the number of visits to different channels and other information through statistics of these short web addresses.
Implementation of a short URL Service based on Knative Serverless Technology
Distribution on demand can be achieved in Knative mode. When there is no traffic, the instance capacity is reduced to zero, and when there is traffic coming in, the instance capacity is automatically expanded to provide services. Now we will implement a short URL service in serverless mode based on the Knative of Ali Cloud Container Service. This example shows a complete demo. You can create a Knative cluster on top of the Ali Cloud container service and use this example to provide the service. Implement the simplest function in this example
Realize the mapping service from long URL to short URL through interface
When a user accesses a short URL through a browser, he jumps to a long URL through 301.
Let's implement this function step by step.
Database
Now that you want to achieve the mapping from a short URL to a long URL, you need to save the information of the long URL to the database and generate a short ID as part of the short URL. So we first need to choose which database to use. In this example, we choose to use Aliyun's table storage. The biggest advantage of table storage is the postpaid service. You only need to pay for the quantity you use, and the price is very affordable. The postpaid price list shown below. The cost of saving 1G data for one year is 3.65292 yuan / year (0.000417 _ 24 _ 365mm 3.65292), which is not very cost-effective.
Generate API for short URL
We need to have an API generated short URL
/ new?origin-url=$ {long URL}
Origin-url access address
Return the result
VEzm6v
Assuming that the domain name we serve is short-url.default.serverless.kuberun.com, we can now jump to a long URL when we visit http://short-url.default.serverless.kuberun.com/vEzm6v.
The code implements package mainimport ("crypto/md5"encoding/hex"fmt"log"net/http"os"strconv"time"strings"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore") var (alphabet = [] byte ("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") l = & Log {}) func ShortUrl (url string) string {md5Str: = getMd5Str (url) var tempVal int64 var result [4] string for i: = 0 I
< 4; i++ { tempSubStr := md5Str[i*8 : (i+1)*8] hexVal, _ := strconv.ParseInt(tempSubStr, 16, 64) tempVal = 0x3FFFFFFF & hexVal var index int64 tempUri := []byte{} for i := 0; i < 6; i++ { index = 0x0000003D & tempVal tempUri = append(tempUri, alphabet[index]) tempVal = tempVal >> 5} result [I] = string (tempUri)} return result [0]} func getMd5Str (str string) string {m: = md5.New () m.Write ([] byte (str)) c: = m.Sum (nil) return hex.EncodeToString (c)} type Log struct {} func (log * Log) Infof (format string A... interface {}) {log.log ("INFO", format, a...)} func (log * Log) Info (msg string) {log.log ("INFO", "% s", msg)} func (log * Log) Errorf (format string, a... interface {}) {log.log ("ERROR", format, a...)} func (log * Log) Error (msg string) {log.log ("ERROR") "% s", msg)} func (log * Log) Fatalf (format string, a... interface {}) {log.log ("FATAL", format, a...)} func (log * Log) Fatal (msg string) {log.log ("FATAL", "% s", msg)} func (log * Log) log (level, format string, a. Interface {}) {var cstSh _ = time.LoadLocation ("Asia/Shanghai") ft: = fmt.Sprintf ("% s% s% s\ n", time.Now () .In (cstSh) .Format ("2006-01-02 15:04:05"), level, format) fmt.Printf (ft, a...)} func handler (w http.ResponseWriter, r * http.Request) {l: = & Log {} l.Infof ("Hello world received a request, url:% s") R.URL.Path) l.Infof ("url:%s", r.URL) / / if r.URL.Path = = "/ favicon.ico" {/ / http.NotFound (w, r) / / return / /} urls: = strings.Split (r.URL.Path, "/") originUrl: = getOriginUrl (urls [urls)-1]) http.Redirect (w R, originUrl, http.StatusMovedPermanently)} func new (w http.ResponseWriter, r * http.Request) {l.Infof ("Hello world received a request, url:%s", r.URL) l.Infof ("url:%s", r.URL) originUrl Ok: = r.URL.Query () ["origin-url"] if! ok {l.Errorf ("no origin-url params found") w.WriteHeader (http.StatusBadRequest) w.Write ([] byte ("Bad request!")) Return} surl: = ShortUrl (originUrl [0]) save (surl, originUrl [0]) fmt.Fprint (w Surl)} func getOriginUrl (surl string) string {endpoint: = os.Getenv ("OTS_TEST_ENDPOINT") tableName: = os.Getenv ("TABLE_NAME") instanceName: = os.Getenv ("OTS_TEST_INSTANCENAME") accessKeyId: = os.Getenv ("OTS_TEST_KEYID") accessKeySecret: = os.Getenv ("OTS_TEST_SECRET") client: = tablestore.NewClient (endpoint, instanceName, accessKeyId) AccessKeySecret) getRowRequest: = & tablestore.GetRowRequest {} criteria: = & tablestore.SingleRowQueryCriteria {} putPk: = & tablestore.PrimaryKey {} putPk.AddPrimaryKeyColumn ("id", surl) criteria.PrimaryKey = putPk getRowRequest.SingleRowQueryCriteria = criteria getRowRequest.SingleRowQueryCriteria.TableName = tableName getRowRequest.SingleRowQueryCriteria.MaxVersion = 1 getResp _: = client.GetRow (getRowRequest) colmap: = getResp.GetColumnMap () return fmt.Sprintf ("% s", colmap.Columns ["originUrl"] [0] .value)} func save (surl OriginUrl string) {endpoint: = os.Getenv ("OTS_TEST_ENDPOINT") tableName: = os.Getenv ("TABLE_NAME") instanceName: = os.Getenv ("OTS_TEST_INSTANCENAME") accessKeyId: = os.Getenv ("OTS_TEST_KEYID") accessKeySecret: = os.Getenv ("OTS_TEST_SECRET") client: = tablestore.NewClient (endpoint, instanceName, accessKeyId AccessKeySecret) putRowRequest: = & tablestore.PutRowRequest {} putRowChange: = & tablestore.PutRowChange {} putRowChange.TableName = tableName putPk: = & tablestore.PrimaryKey {} putPk.AddPrimaryKeyColumn ("id", surl) putRowChange.PrimaryKey = putPk putRowChange.AddColumn ("originUrl", originUrl) putRowChange.SetCondition (tablestore.RowExistenceExpectation_IGNORE) putRowRequest.PutRowChange = putRowChange if _, err: = client.PutRow (putRowRequest) Err! = nil {l.Errorf ("putrow failed with error:% s", err)}} func main () {http.HandleFunc ("/", handler) http.HandleFunc ("/ new") New) port: = os.Getenv ("PORT") if port = = "" {port = "9090"} if err: = http.ListenAndServe (fmt.Sprintf (":% s", port), nil) Err! = nil {log.Fatalf ("ListenAndServe error:%s", err.Error ())}}
I have compiled the code into an image, and you can directly use registry.cn-hangzhou.aliyuncs.com/knative-sample/shorturl:v1 to deploy the service.
Three steps to go! The first step to prepare the database
First, activate the table storage service in Aliyun, and then create an instance and table. The structure we need is relatively simple. We only need the mapping from short URL ID to long URL. The storage table structure is designed as follows:
Name description id short URL IDoriginUrl long URL
The second step is to obtain access key
After logging in to Aliyun, the mouse floats in the upper right corner of the page, then click accesskeys to jump to the accesskeys management page and click to show Access Key Secret.
Step 3 deploy the service
The configuration of Knative Service is as follows, populating the environment variables of Knative Service with the configuration information of the first two steps. Then deploy to the Knative cluster.
ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: short-url namespace: defaultspec: template: metadata: labels: app: short-url annotations: autoscaling.knative.dev/maxScale: "20" autoscaling.knative.dev/minScale: "0" autoscaling.knative.dev/target: "100" spec: containers:-image: registry.cn-hangzhou.aliyuncs.com / knative-sample/shorturl:v1 ports:-name: http1 containerPort: 8080 env:-name: OTS_TEST_ENDPOINT value: http://t.cn-hangzhou.ots.aliyuncs.com-name: TABLE_NAME value: ${TABLE_NAME}-name: OTS_TEST_INSTANCENAME value: ${ OTS_TEST_INSTANCENAME}-name: OTS_TEST_KEYID value: ${OTS_TEST_KEYID}-name: OTS_TEST_SECRET value: ${OTS_TEST_SECRET}
Using the knative service deployment service above, the deployment may be as follows:
└─ # kubectl get ksvcshort-url http://short-url.default.serverless.kuberun.com short-url-456q9 short-url-456q9 True
You can start testing now.
Generate a short URL
└─ # curl 'http://short-url.default.serverless.kuberun.com/new?origin-url=https://help.aliyun.com/document_detail/121534.html?spm=a2c4g.11186623.6.786.41e074d9oHpbO2'vEzm6v
The output VR7baa of the curl command is the ID of the short URL.
Visit the short URL and open the http://short-url.default.serverless.kuberun.com/vEzm6v in the browser to jump to the long url URL.
In this practice, we only need three steps to implement a Serverless short URL service based on Knative. This short URL service can be scaled down to zero to save computing resources when there is no request, and can be automatically expanded when there are many requests. And use Aliyun table storage, so that the database is also paid on demand. The Serverless of short URL service is realized based on Knative + TableStore.
After reading the above, have you mastered how to implement a short URL service based on Knative Serverless technology? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.