使用golang的http模块构建redis读写查api

前端之家收集整理的这篇文章主要介绍了使用golang的http模块构建redis读写查api前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文出处:http://www.jb51.cc/article/p-wqmxalhu-ng.html

支持该文原创作者 rfyiamcool 的博客 峰云,就她了。


前沿:

这两天试着用golang做一些高性能的api,不想把压力到聚合在平台的接口上。平台因为要做很多耗时间的操作,uwsgi下会出现少许错误,找了一圈不知道如何解决该问题。 暂时先绕道而行,先拿简单的接口来做测试,慢慢的把复杂的操作也迁移到golang上。

话说以前高性能的接口,我用的最多的方案还是Nginx lua的组合,超强,大家可以看看我以前写的Nginx lua的文章,各方面没得说。只是这段时间正在看golang 的,就试着用golang实现redis的api,先来个简单的试试手。


里面引用的是golang自带的http模块,redis是自己down的。

golang redis的配置方法

1
2
3
4
cd $GOPATH/src@H_403_48@
git clone git:@H_403_48@ //github.com/alphazero/Go-Redis.git redis@H_403_48@
cd redis@H_403_48@
go install@H_403_48@

先简单说下,golang对于redis的操作方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//xiaorui.cc@H_403_48@
package main@H_403_48@
import@H_403_48@ (@H_403_48@
@H_403_48@ "os"@H_403_48@ ;@H_403_48@
@H_403_48@ "bufio"@H_403_48@ ;@H_403_48@
@H_403_48@ "log"@H_403_48@ ;@H_403_48@
@H_403_48@ "fmt"@H_403_48@ ;@H_403_48@
@H_403_48@ "redis"@H_403_48@ ;@H_403_48@
)@H_403_48@
/*@H_403_48@
@H_403_48@ hello world,redis style.@H_403_48@
*/@H_403_48@
func main () {@H_403_48@
@H_403_48@ // create the client. @H_403_48@ Here@H_403_48@ we are using a synchronous client.@H_403_48@
@H_403_48@ // @H_403_48@ Using@H_403_48@ the default @H_403_48@ ConnectionSpec@H_403_48@ ,we are specifying the client to connect@H_403_48@
@H_403_48@ // to db 13 (e.g. @H_403_48@ SELECT@H_403_48@ 13),@H_403_48@ and@H_403_48@ a password @H_403_48@ of@H_403_48@ go-redis (e.g. @H_403_48@ AUTH@H_403_48@ go-redis)@H_403_48@
@H_403_48@ spec := redis.@H_403_48@ DefaultSpec@H_403_48@ ().@H_403_48@ Db@H_403_48@ (13).@H_403_48@ Password@H_403_48@ (@H_403_48@ "go-redis"@H_403_48@ );@H_403_48@
@H_403_48@ client,e := redis.@H_403_48@ NewSynchClientWithSpec@H_403_48@ (spec);@H_403_48@
@H_403_48@ if@H_403_48@ e != nil { log.@H_403_48@ Println@H_403_48@ (@H_403_48@ "Failed to create the client"@H_403_48@ ,e); return }@H_403_48@
@H_403_48@ key := @H_403_48@ "examples/hello/user.name"@H_403_48@ ;@H_403_48@
@H_403_48@ value,e := client.@H_403_48@ Get@H_403_48@ (key);@H_403_48@
@H_403_48@ if@H_403_48@ e!= nil { log.@H_403_48@ Println@H_403_48@ (@H_403_48@ "error on Get"@H_403_48@ ,e); return }@H_403_48@
@H_403_48@ if@H_403_48@ value == nil {@H_403_48@
@H_403_48@ fmt.@H_403_48@ Printf@H_403_48@ (@H_403_48@ "\nHello,don't believe we've met before!\nYour name? "@H_403_48@ );@H_403_48@
@H_403_48@ reader:= bufio.@H_403_48@ NewReader@H_403_48@ (os.@H_403_48@ Stdin@H_403_48@ );@H_403_48@
@H_403_48@ user,_ := reader.@H_403_48@ ReadString@H_403_48@ (byte(@H_403_48@ '\n'@H_403_48@ ));@H_403_48@
@H_403_48@ if@H_403_48@ len(user) > 1 {@H_403_48@
@H_403_48@ user = user[@H_403_48@ 0:len@H_403_48@ (user)-1];@H_403_48@
@H_403_48@ value = []byte(user);@H_403_48@
@H_403_48@ client.@H_403_48@ Set@H_403_48@ (key,value);@H_403_48@
@H_403_48@ } else {@H_403_48@
@H_403_48@ fmt.@H_403_48@ Printf@H_403_48@ (@H_403_48@ "vafanculo!\n"@H_403_48@ );@H_403_48@
@H_403_48@ return;@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ fmt.@H_403_48@ Printf@H_403_48@ (@H_403_48@ "Hey,ciao %s!\n"@H_403_48@ ,fmt.@H_403_48@ Sprintf@H_403_48@ (@H_403_48@ "%s"@H_403_48@ ,value));@H_403_48@
}@H_403_48@

我写的实例,大家看懂了后,完全可以做更多的扩展。


其实golang自带的http很有mvc的感觉,三者做了一些分离,很像python里面的web.py tornado。。。

测试结果:

服务端的启动

wKioL1Mrmi3Qj3nVAAHGNMEWAPA108.jpg

客户端的测试

wKiom1MrmkSg8_TGAATqDvHKzoA311.jpg

原文:http://www.jb51.cc/article/p-wqmxalhu-ng.html


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//xiaorui.cc@H_403_48@
package@H_403_48@ main@H_403_48@
import@H_403_48@ (@H_403_48@
@H_403_48@ "fmt"@H_403_48@
@H_403_48@ "net/http"@H_403_48@
@H_403_48@ "io/IoUtil"@H_403_48@
@H_403_48@ "log"@H_403_48@
@H_403_48@ "time"@H_403_48@
@H_403_48@ "redis"@H_403_48@
)@H_403_48@
//xiaorui.cc@H_403_48@
const@H_403_48@ AddForm = `@H_403_48@
<html><body>@H_403_48@
<form method=@H_403_48@ "POST"@H_403_48@ action=@H_403_48@ "/add"@H_403_48@ >@H_403_48@
Name: <input type=@H_403_48@ "text"@H_403_48@ name=@H_403_48@ "name"@H_403_48@ >@H_403_48@
Age: <input type=@H_403_48@ "text"@H_403_48@ name=@H_403_48@ "age"@H_403_48@ >@H_403_48@
<input type=@H_403_48@ "submit"@H_403_48@ value=@H_403_48@ "Add"@H_403_48@ >@H_403_48@
</form>@H_403_48@
</body></html>@H_403_48@
`@H_403_48@
const@H_403_48@ setform = `@H_403_48@
<html><body>@H_403_48@
<form method=@H_403_48@ "POST"@H_403_48@ action=@H_403_48@ "/set"@H_403_48@ >@H_403_48@
key: <input type=@H_403_48@ "text"@H_403_48@ name=@H_403_48@ "key"@H_403_48@ >@H_403_48@
value: <input type=@H_403_48@ "text"@H_403_48@ name=@H_403_48@ "value"@H_403_48@ >@H_403_48@
<input type=@H_403_48@ "submit"@H_403_48@ value=@H_403_48@ "set"@H_403_48@ >@H_403_48@
</form>@H_403_48@
</body></html>@H_403_48@
`@H_403_48@
func Handler( w http.ResponseWriter,r *http.Request ){@H_403_48@
@H_403_48@ path := r.URL.Path[@H_403_48@ 1@H_403_48@ :]@H_403_48@
@H_403_48@ if@H_403_48@ path == @H_403_48@ "favicon.ico"@H_403_48@ {@H_403_48@
@H_403_48@ http.NotFound(w,r)@H_403_48@
@H_403_48@ return@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ if@H_403_48@ path == @H_403_48@ ""@H_403_48@ {@H_403_48@
@H_403_48@ path = @H_403_48@ "index.html"@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ contents,err:= IoUtil.ReadFile( path )@H_403_48@
@H_403_48@ if@H_403_48@ err !=nil{@H_403_48@
@H_403_48@ fmt.Fprintf( w,@H_403_48@ "404"@H_403_48@ )@H_403_48@
@H_403_48@ return@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ fmt.Fprintf( w,@H_403_48@ "%s\n"@H_403_48@ ,contents )@H_403_48@
}@H_403_48@
@H_403_48@
func Add( w http.ResponseWriter,r *http.Request ){@H_403_48@
@H_403_48@ name := r.FormValue(@H_403_48@ "name"@H_403_48@ )@H_403_48@
@H_403_48@ age := r.FormValue(@H_403_48@ "age"@H_403_48@ )@H_403_48@
@H_403_48@ if@H_403_48@ name == @H_403_48@ ""@H_403_48@ || age == @H_403_48@ ""@H_403_48@ {@H_403_48@
@H_403_48@ fmt.Fprint(w,AddForm)@H_403_48@
@H_403_48@ return@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ fmt.Fprintf(w,@H_403_48@ "Save : Your name is %s,You age is %s"@H_403_48@ ,name,age)@H_403_48@
}@H_403_48@
func redisset( w http.ResponseWriter,r *http.Request ){@H_403_48@
@H_403_48@ key := r.FormValue(@H_403_48@ "key"@H_403_48@ )@H_403_48@
@H_403_48@ value := r.FormValue(@H_403_48@ "value"@H_403_48@ )@H_403_48@
@H_403_48@ if@H_403_48@ key == @H_403_48@ ""@H_403_48@ || value == @H_403_48@ ""@H_403_48@ {@H_403_48@
@H_403_48@ fmt.Fprint(w,setform)@H_403_48@
@H_403_48@ return@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ spec := redis.DefaultSpec().Db(@H_403_48@ 0@H_403_48@ ).Password(@H_403_48@ ""@H_403_48@ );@H_403_48@
@H_403_48@ client,e := redis.NewSynchClientWithSpec (spec);@H_403_48@
@H_403_48@ if@H_403_48@ e != nil { log.Println (@H_403_48@ "服务器连接有异常"@H_403_48@ ,e); @H_403_48@ return@H_403_48@ }@H_403_48@
@H_403_48@ inva := []byte(value)@H_403_48@
@H_403_48@ client.Set(key,inva);@H_403_48@
@H_403_48@ fmt.Fprintf(w,@H_403_48@ "哥们,你输入的key %s 和value %s 已经插入到redis里面了"@H_403_48@ ,key,key)@H_403_48@
}@H_403_48@
func redisget( w http.ResponseWriter,r *http.Request ){@H_403_48@
@H_403_48@ key := r.FormValue(@H_403_48@ "key"@H_403_48@ )@H_403_48@
@H_403_48@ if@H_403_48@ key == @H_403_48@ ""@H_403_48@ {@H_403_48@
@H_403_48@ fmt.Fprint(w,setform)@H_403_48@
@H_403_48@ return@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ spec := redis.DefaultSpec().Db(@H_403_48@ 0@H_403_48@ ).Password(@H_403_48@ ""@H_403_48@ );@H_403_48@
@H_403_48@ client,e := redis.NewSynchClientWithSpec (spec);@H_403_48@
@H_403_48@ if@H_403_48@ e != nil { log.Println (@H_403_48@ "服务器连接有异常"@H_403_48@ ,e); @H_403_48@ return@H_403_48@ }@H_403_48@
@H_403_48@
@H_403_48@ value,e := client.Get(key);@H_403_48@
@H_403_48@ fmt.Fprintf(w,@H_403_48@ "哥们,你要查询的key %s 和value %s "@H_403_48@ ,value)@H_403_48@
}@H_403_48@
func valueget(w http.ResponseWriter,r *http.Request) {@H_403_48@
@H_403_48@ params := r.URL.Query()@H_403_48@
@H_403_48@ user := params.Get(@H_403_48@ "user"@H_403_48@ )@H_403_48@
@H_403_48@ fmt.Fprintf(w,@H_403_48@ "you are get user %s"@H_403_48@ ,user)@H_403_48@
}@H_403_48@
@H_403_48@
func main(){@H_403_48@
@H_403_48@ http.HandleFunc( @H_403_48@ "/"@H_403_48@ ,Handler)@H_403_48@
@H_403_48@ http.HandleFunc( @H_403_48@ "/add"@H_403_48@ ,Add)@H_403_48@
@H_403_48@ http.HandleFunc( @H_403_48@ "/redisset"@H_403_48@ ,redisset)@H_403_48@
@H_403_48@ http.HandleFunc( @H_403_48@ "/redisget"@H_403_48@ ,redisget)@H_403_48@
@H_403_48@ http.HandleFunc( @H_403_48@ "/valueget"@H_403_48@ ,valueget)@H_403_48@
@H_403_48@ s := &http.Server{@H_403_48@
@H_403_48@ Addr: @H_403_48@ ":80"@H_403_48@ ,@H_403_48@
@H_403_48@ ReadTimeout: @H_403_48@ 30@H_403_48@ * time.Second,@H_403_48@
@H_403_48@ WriteTimeout: @H_403_48@ 30@H_403_48@ * time.Second,@H_403_48@
@H_403_48@ MaxHeaderBytes: @H_403_48@ 1@H_403_48@ << @H_403_48@ 20@H_403_48@ ,@H_403_48@
@H_403_48@ }@H_403_48@
@H_403_48@ log.Fatal(s.ListenAndServe())@H_403_48@
}@H_403_48@

对于go的基础教程,我也写过其他的文章,大家可以参考下。


关于Golang语言的web编程的实例及常见问题

http://www.jb51.cc/article/p-cvbhvbgk-ng.html

关于Go语言在服务端做Restful接口和socket通信

http://www.jb51.cc/article/p-usbcgzmu-ng.html


原文出处:http://www.jb51.cc/article/p-wqmxalhu-ng.html

支持该文原创作者 rfyiamcool 的博客 峰云,就她了。

猜你在找的Go相关文章