service.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/afex/hystrix-go/hystrix"
  8. "github.com/longjoy/micro-go-course/section25/goods/common"
  9. "go.etcd.io/etcd/clientv3"
  10. "io/ioutil"
  11. "log"
  12. "net/http"
  13. "net/url"
  14. "strconv"
  15. "time"
  16. )
  17. type GoodsDetailVO struct {
  18. Id string
  19. Name string
  20. Comments common.CommentListVO
  21. }
  22. type Service interface {
  23. GetGoodsDetail(ctx context.Context, id string) (GoodsDetailVO, error)
  24. InitConfig(ctx context.Context)
  25. }
  26. func NewGoodsServiceImpl() Service {
  27. return &GoodsDetailServiceImpl{}
  28. }
  29. type GoodsDetailServiceImpl struct {
  30. callCommentService int
  31. }
  32. func (service *GoodsDetailServiceImpl) GetGoodsDetail(ctx context.Context, id string) (GoodsDetailVO, error) {
  33. detail := GoodsDetailVO{Id: id, Name: "Name"}
  34. if service.callCommentService != 0 {
  35. commentResult, _ := GetGoodsComments(id)
  36. detail.Comments = commentResult.Detail
  37. }
  38. var err error
  39. if err != nil {
  40. return detail, err
  41. }
  42. return detail, nil
  43. }
  44. func GetGoodsComments(id string) (common.CommentResult, error) {
  45. var result common.CommentResult
  46. serviceName := "Comments"
  47. err := hystrix.Do(serviceName, func() error {
  48. requestUrl := url.URL{
  49. Scheme: "http",
  50. Host: "127.0.0.1" + ":" + "8081",
  51. Path: "/comments/detail",
  52. RawQuery: "id=" + id,
  53. }
  54. resp, err := http.Get(requestUrl.String())
  55. if err != nil {
  56. return err
  57. }
  58. body, _ := ioutil.ReadAll(resp.Body)
  59. jsonErr := json.Unmarshal(body, &result)
  60. if jsonErr != nil {
  61. return jsonErr
  62. }
  63. return nil
  64. }, func(e error) error {
  65. // 断路器打开时的处理逻辑,本示例是直接返回错误提示
  66. return errors.New("Http errors!")
  67. })
  68. if err == nil {
  69. return result, nil
  70. } else {
  71. return result, err
  72. }
  73. }
  74. func (service *GoodsDetailServiceImpl) InitConfig(ctx context.Context) {
  75. log.Printf("InitConfig")
  76. cli, _ := clientv3.New(clientv3.Config{
  77. Endpoints: []string{"127.0.0.1:2379"},
  78. DialTimeout: 5 * time.Second,
  79. })
  80. // get
  81. resp, _ := cli.Get(ctx, "call_service_d")
  82. for _, ev := range resp.Kvs {
  83. fmt.Printf("%s:%s\n", ev.Key, ev.Value)
  84. if string(ev.Key) == "call_service_d" {
  85. service.callCommentService, _ = strconv.Atoi(string(ev.Value))
  86. }
  87. }
  88. rch := cli.Watch(context.Background(), "call_service_d") // <-chan WatchResponse
  89. for wresp := range rch {
  90. for _, ev := range wresp.Events {
  91. fmt.Printf("Type: %s Key:%s Value:%s\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  92. if string(ev.Kv.Key) == "call_service_d" {
  93. service.callCommentService, _ = strconv.Atoi(string(ev.Kv.Value))
  94. }
  95. }
  96. }
  97. }