根据 umermansoor github的 Python
版本的微服务改形成 Golang
版本
一共有4个微服务git
源码 Githubgithub
各个服务之间相互独立,单独的路由和单独的数据库,各个服务之间的通讯是经过 HTTP JSON
,每一个服务的API的返回结果也是JSON类型,能够参考 使用Golang和MongoDB构建 RESTful API,提取出各个服务之间共同的东西,独立于服务以外,供服务调用golang
helper/utils.go
mongodb
func ResponseWithJson(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
复制代码
models/models.go
数据库
type User struct {
Id string `bson:"_id" json:"id"`
Name string `bson:"name" json:"name"`
}
type Movie struct {
Id string `bson:"_id" json:"id"`
Title string `bson:"title" json:"title"`
Rating float32 `bson:"rating" json:"rating"`
Director string `bson:"director" json:"director"`
}
type ShowTimes struct {
Id string `bson:"_id" json:"id"`
Date string `bson:"date" json:"date"`
Movies []string `bson:"movies" json:"movies"`
}
type Booking struct {
Id string `bson:"_id" json:"id"`
Name string `bson:"name" json:"name"`
Books []BookInfo `bson:"books" json:"books"`
}
type BookInfo struct {
Date string `bson:"date" json:"date"`
Movies []string `bson:"movies" json:"movies"`
}
type Result struct {
Name string `json:"name"`
Books []ResultInfo `json:"books"`
}
type ResultInfo struct {
Date string `json:"date"`
Movies []Movie `json:"movies"`
}
复制代码
dao/db.go
,具体的请参考 对 mgo关于MongoDB的基础操做的封装json
func Insert(db, collection string, docs ...interface{}) error {
ms, c := connect(db, collection)
defer ms.Close()
return c.Insert(docs...)
}
func FindOne(db, collection string, query, selector, result interface{}) error {
ms, c := connect(db, collection)
defer ms.Close()
return c.Find(query).Select(selector).One(result)
}
...
复制代码
各个服务具体的逻辑具体的参考 使用Golang和MongoDB构建 RESTful APIbash
查询某个用户的订阅的电影信息时,须要先经过 User Service
服务查询这个用户,根据用户名经过 Booking Service
查询用户的订阅信息,而后经过 Movie Service
服务查询对应的电影的信息,都是经过 HTTP
通讯app
params := mux.Vars(r)
name := params["name"]
var user models.User
if err := dao.FindOne(db, collection, bson.M{"_id": name}, nil, &user); err != nil {
helper.ResponseWithJson(w, http.StatusBadRequest, "invalid request")
return
}
res, err := http.Get("http://127.0.0.1:8003/booking/" + name)
if err != nil {
helper.ResponseWithJson(w, http.StatusBadRequest, "invalid request by name "+name)
return
}
defer res.Body.Close()
result, err := ioutil.ReadAll(res.Body)
if err != nil {
helper.ResponseWithJson(w, http.StatusBadRequest, "invalid request of booking by name "+name)
return
}
var booking models.Booking
var resResult models.Result
resResult.Name = name
var resInfo models.ResultInfo
if err := json.Unmarshal(result, &booking); err == nil {
for _, book := range booking.Books {
resInfo.Date = book.Date
for _, movie := range book.Movies {
res, err := http.Get("http://127.0.0.1:8001/movies/" + movie)
if err == nil {
result, err := ioutil.ReadAll(res.Body)
if err == nil {
var movie models.Movie
if err := json.Unmarshal(result, &movie); err == nil {
resInfo.Movies = append(resInfo.Movies, movie)
}
}
}
}
resResult.Books = append(resResult.Books, resInfo)
}
helper.ResponseWithJson(w, http.StatusOK, resResult)
} else {
helper.ResponseWithJson(w, http.StatusBadRequest, "invalid request")
}
复制代码