|
| 1 | +package polaris |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/TarsCloud/TarsGo/tars/protocol/res/endpointf" |
| 8 | + "github.com/TarsCloud/TarsGo/tars/registry" |
| 9 | + "github.com/TarsCloud/TarsGo/tars/util/endpoint" |
| 10 | + "github.com/polarismesh/polaris-go" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + endpointMeta = "endpoint" |
| 15 | +) |
| 16 | + |
| 17 | +type polarisRegistry struct { |
| 18 | + namespace string |
| 19 | + provider polaris.ProviderAPI |
| 20 | + consumer polaris.ConsumerAPI |
| 21 | +} |
| 22 | + |
| 23 | +type RegistryOption func(pr *polarisRegistry) |
| 24 | + |
| 25 | +func WithNamespace(namespace string) RegistryOption { |
| 26 | + return func(pr *polarisRegistry) { |
| 27 | + pr.namespace = namespace |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func New(provider polaris.ProviderAPI, opts ...RegistryOption) registry.Registry { |
| 32 | + consumer := polaris.NewConsumerAPIByContext(provider.SDKContext()) |
| 33 | + pr := &polarisRegistry{namespace: "tars", provider: provider, consumer: consumer} |
| 34 | + for _, opt := range opts { |
| 35 | + opt(pr) |
| 36 | + } |
| 37 | + //pr.addMiddleware() |
| 38 | + return pr |
| 39 | +} |
| 40 | + |
| 41 | +/*func (pr *polarisRegistry) addMiddleware() { |
| 42 | + tars.UseClientFilterMiddleware(func(next tars.ClientFilter) tars.ClientFilter { |
| 43 | + return func(ctx context.Context, msg *tars.Message, invoke tars.Invoke, timeout time.Duration) (err error) { |
| 44 | + start := time.Now() |
| 45 | + defer func() { |
| 46 | + delay := time.Since(start) |
| 47 | + retStatus := model.RetSuccess |
| 48 | + if msg.Resp.IRet != 0 { |
| 49 | + retStatus = model.RetFail |
| 50 | + } |
| 51 | + ret := &polaris.ServiceCallResult{ |
| 52 | + ServiceCallResult: model.ServiceCallResult{ |
| 53 | + EmptyInstanceGauge: model.EmptyInstanceGauge{}, |
| 54 | + CalledInstance: nil, // todo: 怎么获取到或构造 Instance |
| 55 | + Method: msg.Req.SServantName + "." + msg.Req.SFuncName, |
| 56 | + RetStatus: retStatus, |
| 57 | + }, |
| 58 | + } |
| 59 | + ret.SetDelay(delay) |
| 60 | + ret.SetRetCode(msg.Resp.IRet) |
| 61 | + if er := pr.consumer.UpdateServiceCallResult(ret); er != nil { |
| 62 | + TLOG.Errorf("do report service call result : %+v", er) |
| 63 | + } |
| 64 | + }() |
| 65 | + return next(ctx, msg, invoke, timeout) |
| 66 | + } |
| 67 | + }) |
| 68 | +}*/ |
| 69 | + |
| 70 | +func (pr *polarisRegistry) Registry(_ context.Context, servant *registry.ServantInstance) error { |
| 71 | + instance := &polaris.InstanceRegisterRequest{} |
| 72 | + instance.Host = servant.Endpoint.Host |
| 73 | + instance.Port = int(servant.Endpoint.Port) |
| 74 | + instance.Protocol = &servant.Protocol |
| 75 | + instance.Namespace = pr.namespace |
| 76 | + instance.Service = servant.Servant |
| 77 | + if servant.Endpoint.Weight > 0 { |
| 78 | + weight := int(servant.Endpoint.Weight) |
| 79 | + instance.Weight = &weight |
| 80 | + } |
| 81 | + if servant.Endpoint.Timeout > 0 { |
| 82 | + timeout := time.Duration(servant.Endpoint.Timeout) * time.Millisecond |
| 83 | + instance.Timeout = &timeout |
| 84 | + } |
| 85 | + instance.Metadata = createMetadata(servant) |
| 86 | + _, err := pr.provider.RegisterInstance(instance) |
| 87 | + return err |
| 88 | +} |
| 89 | + |
| 90 | +func (pr *polarisRegistry) Deregister(_ context.Context, servant *registry.ServantInstance) error { |
| 91 | + instance := &polaris.InstanceDeRegisterRequest{} |
| 92 | + instance.Namespace = pr.namespace |
| 93 | + instance.Service = servant.Servant |
| 94 | + instance.Host = servant.Endpoint.Host |
| 95 | + instance.Port = int(servant.Endpoint.Port) |
| 96 | + if servant.Endpoint.Timeout > 0 { |
| 97 | + timeout := time.Duration(servant.Endpoint.Timeout) * time.Millisecond |
| 98 | + instance.Timeout = &timeout |
| 99 | + } |
| 100 | + err := pr.provider.Deregister(instance) |
| 101 | + return err |
| 102 | +} |
| 103 | + |
| 104 | +func (pr *polarisRegistry) QueryServant(_ context.Context, id string) (activeEp []endpointf.EndpointF, inactiveEp []endpointf.EndpointF, err error) { |
| 105 | + req := &polaris.GetAllInstancesRequest{} |
| 106 | + req.Namespace = pr.namespace |
| 107 | + req.Service = id |
| 108 | + resp, err := pr.consumer.GetAllInstances(req) |
| 109 | + if err != nil { |
| 110 | + return nil, nil, err |
| 111 | + } |
| 112 | + instances := resp.GetInstances() |
| 113 | + for _, ins := range instances { |
| 114 | + ep := endpoint.Parse(ins.GetMetadata()[endpointMeta]) |
| 115 | + ep.Host = ins.GetHost() |
| 116 | + ep.Port = int32(ins.GetPort()) |
| 117 | + epf := endpoint.Endpoint2tars(ep) |
| 118 | + if ins.IsHealthy() { |
| 119 | + activeEp = append(activeEp, epf) |
| 120 | + } else { |
| 121 | + inactiveEp = append(inactiveEp, epf) |
| 122 | + } |
| 123 | + } |
| 124 | + return activeEp, inactiveEp, err |
| 125 | +} |
| 126 | + |
| 127 | +func (pr *polarisRegistry) QueryServantBySet(_ context.Context, id, setId string) (activeEp []endpointf.EndpointF, inactiveEp []endpointf.EndpointF, err error) { |
| 128 | + req := &polaris.GetInstancesRequest{} |
| 129 | + req.Namespace = pr.namespace |
| 130 | + req.Service = id |
| 131 | + req.Metadata = map[string]string{ |
| 132 | + "internal-enable-set": "Y", |
| 133 | + "internal-set-name": setId, |
| 134 | + } |
| 135 | + resp, err := pr.consumer.GetInstances(req) |
| 136 | + if err != nil { |
| 137 | + return nil, nil, err |
| 138 | + } |
| 139 | + instances := resp.GetInstances() |
| 140 | + for _, ins := range instances { |
| 141 | + ep := endpoint.Parse(ins.GetMetadata()[endpointMeta]) |
| 142 | + ep.Host = ins.GetHost() |
| 143 | + ep.Port = int32(ins.GetPort()) |
| 144 | + epf := endpoint.Endpoint2tars(ep) |
| 145 | + if ins.IsHealthy() { |
| 146 | + activeEp = append(activeEp, epf) |
| 147 | + } else { |
| 148 | + inactiveEp = append(inactiveEp, epf) |
| 149 | + } |
| 150 | + } |
| 151 | + return activeEp, inactiveEp, err |
| 152 | +} |
| 153 | + |
| 154 | +func createMetadata(servant *registry.ServantInstance) map[string]string { |
| 155 | + metadata := make(map[string]string) |
| 156 | + metadata["tarsVersion"] = servant.TarsVersion |
| 157 | + metadata["app"] = servant.App |
| 158 | + metadata["server"] = servant.Server |
| 159 | + metadata[endpointMeta] = servant.Endpoint.String() |
| 160 | + // polaris plugin |
| 161 | + metadata["internal-enable-set"] = "N" |
| 162 | + if servant.EnableSet { |
| 163 | + metadata["internal-enable-set"] = "Y" |
| 164 | + metadata["internal-set-name"] = servant.SetDivision |
| 165 | + } |
| 166 | + return metadata |
| 167 | +} |
0 commit comments