gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/runtime/service/service.go (about) 1 package service 2 3 import ( 4 "context" 5 "sync" 6 7 "gitee.com/liuxuezhan/go-micro-v1.18.0/client" 8 "gitee.com/liuxuezhan/go-micro-v1.18.0/runtime" 9 pb "gitee.com/liuxuezhan/go-micro-v1.18.0/runtime/service/proto" 10 ) 11 12 type svc struct { 13 sync.RWMutex 14 options runtime.Options 15 runtime pb.RuntimeService 16 } 17 18 // NewRuntime creates new service runtime and returns it 19 func NewRuntime(opts ...runtime.Option) runtime.Runtime { 20 // get default options 21 options := runtime.Options{} 22 23 // apply requested options 24 for _, o := range opts { 25 o(&options) 26 } 27 28 // create default client 29 cli := client.DefaultClient 30 31 return &svc{ 32 options: options, 33 runtime: pb.NewRuntimeService(runtime.DefaultName, cli), 34 } 35 } 36 37 // Init initializes runtime with given options 38 func (s *svc) Init(opts ...runtime.Option) error { 39 s.Lock() 40 defer s.Unlock() 41 42 for _, o := range opts { 43 o(&s.options) 44 } 45 46 return nil 47 } 48 49 // Create registers a service in the runtime 50 func (s *svc) Create(svc *runtime.Service, opts ...runtime.CreateOption) error { 51 options := runtime.CreateOptions{} 52 // apply requested options 53 for _, o := range opts { 54 o(&options) 55 } 56 57 // runtime service create request 58 req := &pb.CreateRequest{ 59 Service: &pb.Service{ 60 Name: svc.Name, 61 Version: svc.Version, 62 Source: svc.Source, 63 Metadata: svc.Metadata, 64 }, 65 Options: &pb.CreateOptions{ 66 Command: options.Command, 67 Env: options.Env, 68 }, 69 } 70 71 if _, err := s.runtime.Create(context.Background(), req); err != nil { 72 return err 73 } 74 75 return nil 76 } 77 78 // Read returns the service with the given name from the runtime 79 func (s *svc) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error) { 80 options := runtime.ReadOptions{} 81 // apply requested options 82 for _, o := range opts { 83 o(&options) 84 } 85 86 // runtime service create request 87 req := &pb.ReadRequest{ 88 Options: &pb.ReadOptions{ 89 Service: options.Service, 90 Version: options.Version, 91 Type: options.Type, 92 }, 93 } 94 95 resp, err := s.runtime.Read(context.Background(), req) 96 if err != nil { 97 return nil, err 98 } 99 100 services := make([]*runtime.Service, 0, len(resp.Services)) 101 for _, service := range resp.Services { 102 svc := &runtime.Service{ 103 Name: service.Name, 104 Version: service.Version, 105 Source: service.Source, 106 Metadata: service.Metadata, 107 } 108 services = append(services, svc) 109 } 110 111 return services, nil 112 } 113 114 // Update updates the running service 115 func (s *svc) Update(svc *runtime.Service) error { 116 // runtime service create request 117 req := &pb.UpdateRequest{ 118 Service: &pb.Service{ 119 Name: svc.Name, 120 Version: svc.Version, 121 }, 122 } 123 124 if _, err := s.runtime.Update(context.Background(), req); err != nil { 125 return err 126 } 127 128 return nil 129 } 130 131 // Delete stops and removes the service from the runtime 132 func (s *svc) Delete(svc *runtime.Service) error { 133 // runtime service create request 134 req := &pb.DeleteRequest{ 135 Service: &pb.Service{ 136 Name: svc.Name, 137 Version: svc.Version, 138 }, 139 } 140 141 if _, err := s.runtime.Delete(context.Background(), req); err != nil { 142 return err 143 } 144 145 return nil 146 } 147 148 // List lists all services managed by the runtime 149 func (s *svc) List() ([]*runtime.Service, error) { 150 // list all services managed by the runtime 151 resp, err := s.runtime.List(context.Background(), &pb.ListRequest{}) 152 if err != nil { 153 return nil, err 154 } 155 156 services := make([]*runtime.Service, 0, len(resp.Services)) 157 for _, service := range resp.Services { 158 svc := &runtime.Service{ 159 Name: service.Name, 160 Version: service.Version, 161 Source: service.Source, 162 Metadata: service.Metadata, 163 } 164 services = append(services, svc) 165 } 166 167 return services, nil 168 } 169 170 // Start starts the runtime 171 func (s *svc) Start() error { 172 // NOTE: nothing to be done here 173 return nil 174 } 175 176 // Stop stops the runtime 177 func (s *svc) Stop() error { 178 // NOTE: nothing to be done here 179 return nil 180 } 181 182 // Returns the runtime service implementation 183 func (s *svc) String() string { 184 return "service" 185 }