Skip to content

Commit

Permalink
feat add resolver_option for servicecomb
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-Morgan committed Sep 13, 2024
1 parent bb83542 commit 23c28df
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 147 deletions.
41 changes: 41 additions & 0 deletions registry/servicecomb/options/resolver_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package options

type ResolverOptions struct {
AppId string
VersionRule string
ConsumerId string
}

// ResolverOption is service-comb resolver option.
type ResolverOption func(o *ResolverOptions)

// WithResolverAppId with appId option.
func WithResolverAppId(appId string) ResolverOption {
return func(o *ResolverOptions) { o.AppId = appId }
}

// WithResolverVersionRule with versionRule option.
func WithResolverVersionRule(versionRule string) ResolverOption {
return func(o *ResolverOptions) { o.VersionRule = versionRule }
}

// WithResolverConsumerId with consumerId option.
func WithResolverConsumerId(consumerId string) ResolverOption {
return func(o *ResolverOptions) { o.ConsumerId = consumerId }
}
43 changes: 0 additions & 43 deletions registry/servicecomb/servicecombhertz/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,6 @@ type scHeartbeat struct {
instanceKey string
}

/*
type registryOptions struct {
appId string
versionRule string
hostName string
heartbeatInterval int32
}
// RegistryOption is ServiceComb option.
type RegistryOption func(o *registryOptions)
// WithAppId with app id option
func WithAppId(appId string) RegistryOption {
return func(o *registryOptions) {
o.appId = appId
}
}
// WithRegistryVersionRule with version rule option
func WithRegistryVersionRule(versionRule string) RegistryOption {
return func(o *registryOptions) {
o.versionRule = versionRule
}
}
// WithRegistryHostName with host name option
func WithRegistryHostName(hostName string) RegistryOption {
return func(o *registryOptions) {
o.hostName = hostName
}
}
// WithRegistryHeartbeatInterval with heart beat second
func WithRegistryHeartbeatInterval(second int32) RegistryOption {
return func(o *registryOptions) {
o.heartbeatInterval = second
}
}
*/
type serviceCombRegistry struct {
cli *sc.Client
opts options.Options
Expand Down
44 changes: 11 additions & 33 deletions registry/servicecomb/servicecombhertz/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,20 @@ package servicecombhertz
import (
"context"

"github.com/cloudwego-contrib/cwgo-pkg/registry/servicecomb/options"

"github.com/cloudwego/hertz/pkg/app/client/discovery"
"github.com/go-chassis/sc-client"
)

var _ discovery.Resolver = (*serviceCombResolver)(nil)

type resolverOptions struct {
appId string
versionRule string
consumerId string
}

// ResolverOption is service-comb resolver option.
type ResolverOption func(o *resolverOptions)

// WithResolverAppId with appId option.
func WithResolverAppId(appId string) ResolverOption {
return func(o *resolverOptions) { o.appId = appId }
}

// WithResolverVersionRule with versionRule option.
func WithResolverVersionRule(versionRule string) ResolverOption {
return func(o *resolverOptions) { o.versionRule = versionRule }
}

// WithResolverConsumerId with consumerId option.
func WithResolverConsumerId(consumerId string) ResolverOption {
return func(o *resolverOptions) { o.consumerId = consumerId }
}

type serviceCombResolver struct {
cli *sc.Client
opts resolverOptions
opts options.ResolverOptions
}

func NewDefaultSCResolver(endPoints []string, opts ...ResolverOption) (discovery.Resolver, error) {
func NewDefaultSCResolver(endPoints []string, opts ...options.ResolverOption) (discovery.Resolver, error) {
client, err := sc.NewClient(sc.Options{
Endpoints: endPoints,
})
Expand All @@ -63,11 +41,11 @@ func NewDefaultSCResolver(endPoints []string, opts ...ResolverOption) (discovery
return NewSCResolver(client, opts...), nil
}

func NewSCResolver(cli *sc.Client, opts ...ResolverOption) discovery.Resolver {
op := resolverOptions{
appId: "DEFAULT",
versionRule: "latest",
consumerId: "",
func NewSCResolver(cli *sc.Client, opts ...options.ResolverOption) discovery.Resolver {
op := options.ResolverOptions{
AppId: "DEFAULT",
VersionRule: "latest",
ConsumerId: "",
}
for _, option := range opts {
option(&op)
Expand All @@ -85,7 +63,7 @@ func (*serviceCombResolver) Target(_ context.Context, target *discovery.TargetIn

// Resolve a service info by desc.
func (scr *serviceCombResolver) Resolve(_ context.Context, desc string) (discovery.Result, error) {
res, err := scr.cli.FindMicroServiceInstances(scr.opts.consumerId, scr.opts.appId, desc, scr.opts.versionRule, sc.WithoutRevision())
res, err := scr.cli.FindMicroServiceInstances(scr.opts.ConsumerId, scr.opts.AppId, desc, scr.opts.VersionRule, sc.WithoutRevision())
if err != nil {
return discovery.Result{}, err
}
Expand All @@ -111,5 +89,5 @@ func (scr *serviceCombResolver) Resolve(_ context.Context, desc string) (discove

// Name returns the name of the resolver.
func (scr *serviceCombResolver) Name() string {
return "sc-resolver" + ":" + scr.opts.appId + ":" + scr.opts.versionRule
return "sc-resolver" + ":" + scr.opts.AppId + ":" + scr.opts.VersionRule
}
37 changes: 0 additions & 37 deletions registry/servicecomb/servicecombkitex/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,6 @@ type scHeartbeat struct {
instanceKey string
}

/*type options struct {
appId string
versionRule string
hostName string
heartbeatInterval int32
}
// Option is ServiceComb option.
type Option func(o *options)
// WithAppId with app id option
func WithAppId(appId string) Option {
return func(o *options) {
o.appId = appId
}
}
// WithVersionRule with version rule option
func WithVersionRule(versionRule string) Option {
return func(o *options) {
o.versionRule = versionRule
}
}
// WithHostName with host name option
func WithHostName(hostName string) Option {
return func(o *options) {
o.hostName = hostName
}
}
func WithHeartbeatInterval(second int32) Option {
return func(o *options) {
o.heartbeatInterval = second
}
}*/

type serviceCombRegistry struct {
cli *sc.Client
opts options.Options
Expand Down
43 changes: 10 additions & 33 deletions registry/servicecomb/servicecombkitex/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package resolver
import (
"context"
"fmt"
"github.com/cloudwego-contrib/cwgo-pkg/registry/servicecomb/options"

"github.com/cloudwego-contrib/cwgo-pkg/registry/servicecomb/servicecombkitex/servicecomb"

Expand All @@ -25,48 +26,24 @@ import (
"github.com/go-chassis/sc-client"
)

type options struct {
appId string
versionRule string
consumerId string
}

// Option is service-comb resolver option.
type Option func(o *options)

// WithAppId with appId option.
func WithAppId(appId string) Option {
return func(o *options) { o.appId = appId }
}

// WithVersionRule with versionRule option.
func WithVersionRule(versionRule string) Option {
return func(o *options) { o.versionRule = versionRule }
}

// WithConsumerId with consumerId option.
func WithConsumerId(consumerId string) Option {
return func(o *options) { o.consumerId = consumerId }
}

type serviceCombResolver struct {
cli *sc.Client
opts options
opts options.ResolverOptions
}

func NewDefaultSCResolver(opts ...Option) (discovery.Resolver, error) {
func NewDefaultSCResolver(opts ...options.ResolverOption) (discovery.Resolver, error) {
client, err := servicecomb.NewDefaultSCClient()
if err != nil {
return nil, err
}
return NewSCResolver(client, opts...), nil
}

func NewSCResolver(cli *sc.Client, opts ...Option) discovery.Resolver {
op := options{
appId: "DEFAULT",
versionRule: "latest",
consumerId: "",
func NewSCResolver(cli *sc.Client, opts ...options.ResolverOption) discovery.Resolver {
op := options.ResolverOptions{
AppId: "DEFAULT",
VersionRule: "latest",
ConsumerId: "",
}
for _, option := range opts {
option(&op)
Expand All @@ -84,7 +61,7 @@ func (scr *serviceCombResolver) Target(_ context.Context, target rpcinfo.Endpoin

// Resolve a service info by desc.
func (scr *serviceCombResolver) Resolve(_ context.Context, desc string) (discovery.Result, error) {
res, err := scr.cli.FindMicroServiceInstances(scr.opts.consumerId, scr.opts.appId, desc, scr.opts.versionRule, sc.WithoutRevision())
res, err := scr.cli.FindMicroServiceInstances(scr.opts.ConsumerId, scr.opts.AppId, desc, scr.opts.VersionRule, sc.WithoutRevision())
if err != nil {
return discovery.Result{}, err
}
Expand Down Expand Up @@ -118,5 +95,5 @@ func (scr *serviceCombResolver) Diff(cacheKey string, prev, next discovery.Resul

// Name returns the name of the resolver.
func (scr *serviceCombResolver) Name() string {
return "sc-resolver" + ":" + scr.opts.appId + ":" + scr.opts.versionRule
return "sc-resolver" + ":" + scr.opts.AppId + ":" + scr.opts.VersionRule
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/cloudwego-contrib/cwgo-pkg/registry/servicecomb/options"
scregistry "github.com/cloudwego-contrib/cwgo-pkg/registry/servicecomb/servicecombkitex/registry"
"github.com/go-chassis/sc-client"

Expand Down Expand Up @@ -53,7 +54,7 @@ func init() {
return
}
time.Sleep(time.Second)
err = scregistry.NewSCRegistry(cli, scregistry.WithAppId(AppId), scregistry.WithVersionRule(Version), scregistry.WithHostName(HostName)).Register(svcInfo)
err = scregistry.NewSCRegistry(cli, options.WithAppId(AppId), options.WithVersionRule(Version), options.WithHostName(HostName)).Register(svcInfo)
if err != nil {
return
}
Expand Down

0 comments on commit 23c28df

Please sign in to comment.