e754b4ce33dbe7ca0851f1459efd20124ee37623.svn-base 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package endpoint
  2. import (
  3. "context"
  4. "github.com/go-kit/kit/endpoint"
  5. "github.com/longjoy/micro-go-course/section13/register/discovery"
  6. "github.com/longjoy/micro-go-course/section13/register/service"
  7. )
  8. type RegisterEndpoints struct {
  9. DiscoveryEndpoint endpoint.Endpoint
  10. HealthCheckEndpoint endpoint.Endpoint
  11. }
  12. // 服务发现请求结构体
  13. type DiscoveryRequest struct {
  14. ServiceName string
  15. }
  16. // 服务发现响应结构体
  17. type DiscoveryResponse struct {
  18. Instances []*discovery.InstanceInfo `json:"instances"`
  19. Error string `json:"error"`
  20. }
  21. // 创建服务发现的 Endpoint
  22. func MakeDiscoveryEndpoint(svc service.Service) endpoint.Endpoint {
  23. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  24. req := request.(DiscoveryRequest)
  25. instances, err := svc.DiscoveryService(ctx, req.ServiceName)
  26. var errString = ""
  27. if err != nil{
  28. errString = err.Error()
  29. }
  30. return &DiscoveryResponse{
  31. Instances:instances,
  32. Error:errString,
  33. }, nil
  34. }
  35. }
  36. // HealthRequest 健康检查请求结构
  37. type HealthRequest struct{}
  38. // HealthResponse 健康检查响应结构
  39. type HealthResponse struct {
  40. Status string `json:"status"`
  41. }
  42. // MakeHealthCheckEndpoint 创建健康检查Endpoint
  43. func MakeHealthCheckEndpoint(svc service.Service) endpoint.Endpoint {
  44. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  45. status := svc.HealthCheck()
  46. return HealthResponse{
  47. Status:status,
  48. }, nil
  49. }
  50. }