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

'get nacos client' #2

Merged
merged 2 commits into from
Sep 17, 2021
Merged
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
2 changes: 1 addition & 1 deletion configs/api_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ resources:
- name: requestBody._all
mapTo: 0
mapType: "object"
applicationName: "UserProvider"
applicationName: "service-gateway-provider"
interface: "com.dubbogo.pixiu.UserService"
method: "CreateUser"
group: "test"
Expand Down
7 changes: 6 additions & 1 deletion configs/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ static_resources:
strip-prefix: true # 代理请求时移除前缀 rm /scp
otherConfig: "pixiu"
registries:
"Nacos":
protocol: "Nacos"
timeout: "3s"
address: "127.0.0.1:8848"
username: ""
password: ""
"eureka":
protocol: "eureka"
enable: true
Expand All @@ -109,4 +115,3 @@ static_resources:
address: "127.0.0.1:2181"
username: ""
password: ""

144 changes: 110 additions & 34 deletions pkg/adapter/springcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@
package springcloud

import (
"fmt"
"github.com/apache/dubbo-go-pixiu/pkg/common/constant"
"github.com/apache/dubbo-go-pixiu/pkg/common/extension/adapter"
"github.com/apache/dubbo-go-pixiu/pkg/logger"
"github.com/apache/dubbo-go-pixiu/pkg/model"
"github.com/apache/dubbo-go-pixiu/pkg/registry"
"github.com/apache/dubbo-go-pixiu/pkg/registry/nacos"
"github.com/apache/dubbo-go-pixiu/pkg/server"
"net"
"strconv"
"strings"
)

const (
Expand All @@ -40,13 +47,13 @@ type (

// CloudAdapter the adapter for spring cloud
CloudAdapter struct {
boot *model.Bootstrap
cfg *Config
cfg *Config
LoaderUsed registry.Loader
}

// Config the config for CloudAdapter
Config struct {
Registry *Registry `yaml:"registry" json:"registry" default:"registry"`
Registry *Registry `yaml:"registrys" json:"registrys" default:"registrys"`
}

// Registry remote registry where spring cloud apis are registered.
Expand All @@ -65,17 +72,35 @@ func (p *CloudPlugin) Kind() string {
}

// CreateAdapter create adapter
func (p *CloudPlugin) CreateAdapter(config interface{}, bs *model.Bootstrap) (adapter.Adapter, error) {
return &CloudAdapter{
cfg: &Config{},
boot: bs,
}, nil
func (p *CloudPlugin) CreateAdapter(config interface{}, ad *model.Adapter) (adapter.Adapter, error) {
//registryUsed := ad.Config["registry"].(map[string]interface{})
registryUsed := config.(map[string]interface{})
fmt.Println(registryUsed)
var loader registry.Loader
reg := registryUsed["registries"].(map[string]interface{})
for key, _ := range reg {
switch key {
case "Nacos":
loader = new(nacos.NacosRegistry)
case "consul":
loader = new(registry.ConsulRegistryLoad)
case "zookeeper":
loader = new(registry.ZookeeperRegistryLoad)
}
}
return &CloudAdapter{cfg: &Config{}, LoaderUsed: loader}, nil
}

// Start start the adapter
func (a *CloudAdapter) Start() {
func (a *CloudAdapter) Start(adapter *model.Adapter) {
reg := a.LoaderUsed
RegistryLoader, err := reg.NewRegistryLoader(adapter)
if err != nil {
logger.Info("fail to get registyloader")
}
// do not block the main goroutine
go func() {
//init registry client

// init SpringCloud Manager for control initialize
cloudManager := NewSpringCloudManager(&SpringCloudConfig{boot: a.boot})
Expand All @@ -85,33 +110,83 @@ func (a *CloudAdapter) Start() {
// fetch service instance from consul

// transform into endpoint and cluster
endpoint := &model.Endpoint{}
endpoint.ID = "user-mock-service"
endpoint.Address = model.SocketAddress{
Address: "127.0.0.1",
Port: 8080,
//endpoint := &model.Endpoint{}
//endpoint.ID = "user-mock-service"
//endpoint.Address = model.SocketAddress{
// Address: "127.0.0.1",
// Port: 8080,
//}
//cluster := &model.Cluster{}
//cluster.Name = "userservice"
//cluster.Lb = model.Rand
//cluster.Endpoints = []*model.Endpoint{
// endpoint,
//}
//// add cluster into manager
//cm := server.GetClusterManager()
//cm.AddCluster(cluster)
//
//// transform into route
//routeMatch := model.RouterMatch{
// Prefix: "/user",
//}
//routeAction := model.RouteAction{
// Cluster: "userservice",
//}
//route := &model.Router{Match: routeMatch, Route: routeAction}
//
//server.GetRouterManager().AddRouter(route)

urls, err := RegistryLoader.LoadAllServices()
if err != nil {
logger.Error("can't get service for %s re")
}
cluster := &model.Cluster{}
cluster.Name = "userservice"
cluster.Lb = model.Rand
cluster.Endpoints = []*model.Endpoint{
endpoint,
}
// add cluster into manager
cm := server.GetClusterManager()
cm.AddCluster(cluster)

// transform into route
routeMatch := model.RouterMatch{
Prefix: "/user",
var endpoints []*model.Endpoint
for _, url := range urls {
endpoint := url.GetParam("endpoint", "")
tmp := strings.Split(endpoint, ",")
for _, path := range tmp {
ep := &model.Endpoint{}
ip, port, err := net.SplitHostPort(path)
porti, err := strconv.Atoi(port)
if err != nil {
logger.Info("split ip & port failed")
}
ep.ID = url.GetParam("Name", "")
ep.Address = model.SocketAddress{
Address: ip,
Port: porti,
}
endpoints = append(endpoints, ep)
}
// get cluster
cluster := &model.Cluster{}
cluster.Name = url.GetParam(constant.NameKey, "")
lb := url.GetParam("loadbalance", "")
switch lb {
case "RoundRobin":
cluster.Lb = model.RoundRobin
case "IPHash":
cluster.Lb = model.IPHash
case "WightRobin":
cluster.Lb = model.WightRobin
case "Rand":
cluster.Lb = model.Rand
}
cluster.Endpoints = endpoints
cm := server.GetClusterManager()
cm.AddCluster(cluster)
// transform into route
routeMatch := model.RouterMatch{
Prefix: "/user",
}
routeAction := model.RouteAction{
Cluster: "userservice",
}
route := &model.Router{Match: routeMatch, Route: routeAction}

server.GetRouterManager().AddRouter(route)
}
routeAction := model.RouteAction{
Cluster: "userservice",
}
route := &model.Router{Match: routeMatch, Route: routeAction}

server.GetRouterManager().AddRouter(route)

}()
}

Expand All @@ -122,6 +197,7 @@ func (a *CloudAdapter) Stop() {

// Apply init
func (a *CloudAdapter) Apply() error {

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/common/extension/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ type (
Kind() string

// CreateAdapter return the Adapter callback
CreateAdapter(config interface{}, bs *model.Bootstrap) (Adapter, error)
CreateAdapter(config interface{}, ad *model.Adapter) (Adapter, error)
}

// Adapter adapter interface
Adapter interface {
// Start start adapter lifetime
Start()
Start(adapter *model.Adapter)
// Stop stop adapter lifetime
Stop()
// Apply init
Expand Down
2 changes: 2 additions & 0 deletions pkg/common/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package yaml

import (
"fmt"
"io/ioutil"
"path"
)
Expand Down Expand Up @@ -69,6 +70,7 @@ func ParseConfig(factoryConfStruct interface{}, conf map[string]interface{}) err
// Unmarshal yamlStr to factoryConf
err = yaml.Unmarshal(yamlBytes, factoryConfStruct)
if err != nil {
fmt.Println(err)
return err
}
return nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/registry/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package registry

import (
"github.com/apache/dubbo-go-pixiu/pkg/model"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -52,6 +53,10 @@ type ConsulRegistryLoad struct {
cluster string
}

func (crl *ConsulRegistryLoad) NewRegistryLoader(ad *model.Adapter) (Loader, error) {
return nil, nil
}

func newConsulRegistryLoad(address, cluster string) (Loader, error) {
config := &consul.Config{Address: address}
client, err := consul.NewClient(config)
Expand Down Expand Up @@ -121,3 +126,7 @@ func (crl *ConsulRegistryLoad) LoadAllServices() ([]*common.URL, error) {
}
return urls, nil
}

func (crl *ConsulRegistryLoad) NewRegistry(sr *model.StaticResources) Loader {
return nil
}
3 changes: 3 additions & 0 deletions pkg/registry/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package registry

import (
"github.com/apache/dubbo-go-pixiu/pkg/model"
"github.com/apache/dubbo-go/common"
)

// Loader this interface defined for load services from different kinds registry, such as nacos,consul,zookeeper.
type Loader interface {
//
NewRegistryLoader(*model.Adapter) (Loader, error)
// LoadAllServices load all services registered in registry
LoadAllServices() ([]*common.URL, error)
// GetCluster get the registry name
Expand Down
Loading