【发布时间】:2019-02-18 03:38:51
【问题描述】:
我正在 Windows 上使用 Redis(第一次尝试 redis)编写一个简单的 Go Web 应用程序。我正在使用 go-redis 包连接到 Redis。
package main
import (
"fmt"
"net/http"
"text/template"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
)
var client *redis.Client
var tmpl *template.Template
func init() {
client = redis.NewClient(&redis.Options{
Addr: "localhost:6397",
Password: "",
DB: 0,
})
tmpl = template.Must(template.ParseGlob("./templates/*.gohtml"))
pong, err := client.Ping().Result()
fmt.Println(pong, err)
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", indexHandler).Methods("GET")
http.Handle("/", router)
http.ListenAndServe(":1234", nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
comments, err := client.LRange("comments", 0, 10).Result()
check(err)
tmpl.ExecuteTemplate(w, "index.gohtml", comments)
}
func check(err error) {
if err != nil {
fmt.Println(err)
return
}
}
但是当我运行这段代码时,我得到了
dial tcp [::1]:6397: connectex: 由于目标机器主动拒绝,无法建立连接。
我能找到的唯一答案是“启动 redis 服务器”。我的 redis 服务器已启动并正在运行(使用 redis 客户端中的“PING”命令检查它)。我也试过以管理员身份运行它,但没有运气。
截图:
【问题讨论】: