Skip to content

Commit

Permalink
fix: allow run without consul
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Feb 15, 2024
1 parent c05d45c commit caf6f3a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
24 changes: 18 additions & 6 deletions cmd/librarian/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
searcher "github.com/tuihub/protos/pkg/librarian/searcher/v1"

"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/google/wire"
Expand Down Expand Up @@ -42,15 +41,28 @@ var ProviderSet = wire.NewSet(
minerClientSelector,
)

func newApp(gs *grpc.Server, hs *http.Server, mq *libmq.MQ, cron *libcron.Cron, r registry.Registrar) *kratos.App {
return kratos.New(
kratos.ID(id+name),
func newApp(
gs *grpc.Server,
hs *http.Server,
mq *libmq.MQ,
cron *libcron.Cron,
consul *conf.Consul,
) (*kratos.App, error) {
options := []kratos.Option{
kratos.ID(id + name),
kratos.Name(name),
kratos.Version(version),
kratos.Metadata(map[string]string{}),
kratos.Server(gs, hs, mq, cron),
kratos.Registrar(r),
)
}
if consul != nil {
r, err := libapp.NewRegistrar(consul)
if err != nil {
return nil, err
}
options = append(options, kratos.Registrar(r))
}
return kratos.New(options...), nil
}

func main() {
Expand Down
1 change: 0 additions & 1 deletion cmd/librarian/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func wireApp(
libmq.ProviderSet,
libcron.ProviderSet,
libcache.ProviderSet,
libapp.ProviderSet,
ProviderSet,
),
)
Expand Down
3 changes: 1 addition & 2 deletions cmd/librarian/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions internal/lib/libapp/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func NewDiscovery(c *conf.Consul) (registry.Discovery, error) {
if err != nil {
return nil, err
}
// check if the consul agent is available
_, err = client.Status().Leader()
if err != nil {
if c == nil { // if consul is not configured, return an empty discovery
return emptyDiscovery{}, nil
}
return nil, err
}
return consul.New(client), nil
}

Expand Down Expand Up @@ -59,3 +67,23 @@ func NewNodeFilter() selector.NodeFilter {
return newNodes
}
}

type emptyDiscovery struct{}

func (e emptyDiscovery) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) {
return []*registry.ServiceInstance{}, nil
}

func (e emptyDiscovery) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) {
return &emptyWatcher{}, nil
}

type emptyWatcher struct{}

func (e emptyWatcher) Next() ([]*registry.ServiceInstance, error) {
return []*registry.ServiceInstance{}, nil
}

func (e emptyWatcher) Stop() error {
return nil
}
14 changes: 14 additions & 0 deletions internal/lib/libapp/service_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ func NewHealthChecker(serviceName string, c *conf.Consul) (*HealthChecker, error
if err != nil {
return nil, err
}
// check if the consul agent is available
_, err = client.Status().Leader()
if err != nil {
if c == nil {
return &HealthChecker{
healthClient: nil,
serviceName: serviceName,
}, nil
}
return nil, err
}

return &HealthChecker{
healthClient: client.Health(),
Expand All @@ -29,6 +40,9 @@ func NewHealthChecker(serviceName string, c *conf.Consul) (*HealthChecker, error
}

func (hc *HealthChecker) GetAliveInstances() ([]*api.ServiceEntry, error) {
if hc.healthClient == nil {
return []*api.ServiceEntry{}, nil
}
instances, _, err := hc.healthClient.Service(hc.serviceName, "", true, nil)
return instances, err
}

0 comments on commit caf6f3a

Please sign in to comment.