i was writing microservices in go using go kit.my first server has a microservice create docfile.
CreateHandler := khttp.NewServer(
CreateEndpoint,
DecodeCreateRequest,
common.EncodeResponse,
opts...,
)
r := mux.NewRouter()
r.Handle("localhost:8082/create/docfile", CreateHandler).Methods("POST")
and inside my create endpoint i call create docfile service of my first server. actually it is a microservice for creating a docfile. and my file struct is
type DocFile struct {
ID uint `gorm:"primary_key;->"`
DocID uint `validate:"required"`
Name string `validate:"required,max=250"`
Notes string
FileUUID *uuid.UUID
IsUploaded bool
}
the field fileuuid is coming from another microservice from another server.lets call it my second server
url := "http://localhost:8085/file/create"
it is run as a seperate microservice for all files. a file is created in second server and the uuid of created file is returned in this microservice and that uuid is updated in my files table. and the request object is
type File struct {
ID uint `gorm:"primary_key;->"`
UUID *uuid.UUID
FileName string
DirectoryID uint
}
and it generates response of type
type CreateFile struct {
ID uint
UUID uuid.UUID
}
so in summary, my create doc file method in my first server is calling another microservice from second server. and i communicate with these services over restful api calls like below
var response CreateFile
//construct file request object
file := storage.File{FileName: fileName, DirectoryID: 101}
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(file)
// Fetch Request
httpResponse, err := http.Post(url, common.Json, &buf)
if err != nil {
return response, err
}
defer httpResponse.Body.Close()
// Read Response Body
responseBody, err := httputil.DecodeResponseBody(httpResponse)
if err != nil {
return response, err
}
// Decode the response body into the response variable
if err := json.Unmarshal(responseBody, &response); err != nil {
return response, err
}
and use this response body to update my fileuuid . if i am communicating between microservices in this way , i find difficult to manage transactions . because if an error is occured along the way , i cant roll back the transactions. please help me in this regard