Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持nebula #6

Open
wants to merge 1 commit into
base: di
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions config/nebula_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

import (
"fmt"

"github.com/glory-go/glory/tools"
)

type NebulaConfig struct {
ConfigSource string `yaml:"config_source"`
Host string `yaml:"host"`
Port string `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}

func (s *NebulaConfig) checkAndFix() {
if err := tools.ReadFromEnvIfNeed(s); err != nil {
fmt.Println("warn: NebulaConfig checkAndFix failed with err = ", err)
}
}
4 changes: 3 additions & 1 deletion config/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type ServerConfig struct {
RegistryConfig map[string]*RegistryConfig `yaml:"registry"`
// MysqlConfigs Mysql 数据库配置
MysqlConfigs map[string]*MysqlConfig `yaml:"mysql"`
// NebulaConfigs Nebula Graph 数据库配置
NebulaConfigs map[string]*NebulaConfig `yaml:"nebula"`
// RedisConfig redis 配置
RedisConfig map[string]*RedisConfig `yaml:"redis"`
// K8SConfig K8s配置
Expand Down Expand Up @@ -101,7 +103,7 @@ func (s *ServerConfig) checkAndFix() {
}
}
if s.OrgName == "" {
panic("please add your service org_name in config file from: classroom|ide|children|goonline")
panic("please add your service org_name in config file like: classroom|ide|children|goonline")
}

if s.ServerName == "" {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/uber/jaeger-lib v2.4.0+incompatible // indirect
github.com/urfave/negroni v1.0.0
github.com/valyala/fasthttp v1.24.0 // indirect
github.com/vesoft-inc/nebula-go/v2 v2.6.0
go.mongodb.org/mongo-driver v1.4.4
go.uber.org/atomic v1.9.0
go.uber.org/multierr v1.7.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/facebook/fbthrift v0.31.1-0.20210223140454-614a73a42488 h1:A4KCT0mvTBkvb93gGN+efLPkrgTqmqMeaLDG51KVhMM=
github.com/facebook/fbthrift v0.31.1-0.20210223140454-614a73a42488/go.mod h1:2tncLx5rmw69e5kMBv/yJneERbzrr1yr5fdlnTbu8lU=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 h1:Ghm4eQYC0nEPnSJdVkTrXpu9KtoVCSo1hg7mtI7G9KU=
Expand Down Expand Up @@ -671,6 +673,8 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasthttp v1.24.0 h1:AAiG4oLDUArTb7rYf9oO2bkGooOqCaUF6a2u8asBP3I=
github.com/valyala/fasthttp v1.24.0/go.mod h1:0mw2RjXGOzxf4NL2jni3gUQ7LfjjUSiG5sskOUUSEpU=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vesoft-inc/nebula-go/v2 v2.6.0 h1:yS5JH8eNjXtHSZXDRcNihdYaW9FyjOsrpG5iIMkIFBs=
github.com/vesoft-inc/nebula-go/v2 v2.6.0/go.mod h1:fehDUs97/mpmxXi9WezhznX0Dg7hmQRUoOWgDZv9zG0=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc h1:n+nNi93yXLkJvKwXNP9d55HC7lGK4H/SRcwB5IaUZLo=
Expand Down
11 changes: 0 additions & 11 deletions mysql/mysql.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mysql

import (
"errors"

"github.com/glory-go/glory/config"
"github.com/glory-go/glory/log"
)
Expand Down Expand Up @@ -35,15 +33,6 @@ func newMysqlHandler() *MysqlHandler {
}
}

func RegisterModel(mysqlServiceName string, model UserDefinedModel) (*MysqlTable, error) {
service, ok := defaultMysqlHandler.mysqlServices[mysqlServiceName]
if !ok {
log.Error("mysql service name = ", mysqlServiceName, " not setup successful")
return nil, errors.New("mysql service name = " + mysqlServiceName + " not setup successful")
}
return service.registerModel(model)
}

func GetService(mysqlServiceName string) (*MysqlService, bool) {
s, ok := defaultMysqlHandler.mysqlServices[mysqlServiceName]
return s, ok
Expand Down
30 changes: 3 additions & 27 deletions mysql/mysql_service.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package mysql

import (
"errors"

"github.com/glory-go/glory/log"
"github.com/glory-go/glory/service/middleware/jaeger"
"gorm.io/gorm"
Expand All @@ -13,15 +11,12 @@ import (

// MysqlService 保存单个mysql链接
type MysqlService struct {
DB *gorm.DB
tables map[string]*MysqlTable
conf config.MysqlConfig
DB *gorm.DB
conf config.MysqlConfig
}

func newMysqlService() *MysqlService {
return &MysqlService{
tables: make(map[string]*MysqlTable),
}
return &MysqlService{}
}

func getMysqlLinkStr(conf config.MysqlConfig) string {
Expand Down Expand Up @@ -53,22 +48,3 @@ func (ms *MysqlService) openDB(conf config.MysqlConfig) error {
}
return nil
}

func (ms *MysqlService) registerModel(model UserDefinedModel) (*MysqlTable, error) {
table := newMysqlTable(ms.DB)
if err := table.registerModel(model); err != nil {
log.Error("mysql service register model err")
return nil, err
}

return table, nil
}

func (ms *MysqlService) GetTable(tableName string) (*MysqlTable, error) {
table, ok := ms.tables[tableName]
if !ok {
log.Error("table name = ", tableName, " not registered")
return nil, errors.New("table name = " + tableName + " not registered")
}
return table, nil
}
89 changes: 0 additions & 89 deletions mysql/mysql_table.go

This file was deleted.

29 changes: 29 additions & 0 deletions nebula/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nebula

import (
"github.com/glory-go/glory/log"
nebula "github.com/vesoft-inc/nebula-go/v2"
)

var (
logger nebula.Logger = &NebulaLogger{}
)

type NebulaLogger struct {
}

func (l *NebulaLogger) Info(msg string) {
log.Info(msg)
}

func (l *NebulaLogger) Warn(msg string) {
log.Warn(msg)
}

func (l *NebulaLogger) Error(msg string) {
log.Error(msg)
}

func (l *NebulaLogger) Fatal(msg string) {
log.Error(msg)
}
39 changes: 39 additions & 0 deletions nebula/nebula.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package nebula

import (
"github.com/glory-go/glory/config"
"github.com/glory-go/glory/log"
)

func init() {
defaultMysqlHandler = newNebulaHandler()
defaultMysqlHandler.setup(config.GlobalServerConf.NebulaConfigs)
}

type NebulaHandler struct {
services map[string]*NebulaService
}

var defaultMysqlHandler *NebulaHandler

func (mh *NebulaHandler) setup(conf map[string]*config.NebulaConfig) {
for k, v := range conf {
tempService := newNebulaService()
if err := tempService.openDB(*v); err != nil {
log.Errorf("opendb with key = %s, err = %s", k, err)
continue
}
mh.services[k] = tempService
}
}

func newNebulaHandler() *NebulaHandler {
return &NebulaHandler{
services: make(map[string]*NebulaService),
}
}

func GetService(serviceName string) (*NebulaService, bool) {
s, ok := defaultMysqlHandler.services[serviceName]
return s, ok
}
57 changes: 57 additions & 0 deletions nebula/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package nebula

import (
"strconv"

"github.com/glory-go/glory/config"
"github.com/glory-go/glory/log"
nebula "github.com/vesoft-inc/nebula-go/v2"
)

// MysqlService 保存单个mysql链接
type NebulaService struct {
pool *nebula.ConnectionPool
conf config.NebulaConfig
}

func newNebulaService() *NebulaService {
return &NebulaService{}
}

func (ms *NebulaService) loadConfig(conf config.NebulaConfig) error {
ms.conf = conf
return nil
}

func (ms *NebulaService) openDB(conf config.NebulaConfig) error {
var err error
if err := ms.loadConfig(conf); err != nil {
log.Error("opendb error with err = ", err)
return err
}
if conf.Port == "" {
conf.Port = "9669"
}
port, err := strconv.Atoi(conf.Port)
if err != nil {
log.Errorf("nebula fail to open db when parse port in conf, err: %v", err)
return err
}
ms.pool, err = nebula.NewConnectionPool([]nebula.HostAddress{
{
Host: conf.Host,
Port: port,
},
}, nebula.GetDefaultConf(), logger)
if err != nil {
log.Error("open db error ", err, "with db config = ", ms.conf)
return err
}
return nil
}

func (ms *NebulaService) GetSession() (*nebula.Session, error) {
session, err := ms.pool.GetSession(ms.conf.Username, ms.conf.Password)
// TODO: 使用jager打点
return session, err
}