From 0863e6f6d225f0d28edc0fb1806ebcf1011b59cc Mon Sep 17 00:00:00 2001 From: yamicro <75002842+yamicro@users.noreply.github.com> Date: Sun, 12 Sep 2021 22:28:13 +0800 Subject: [PATCH] 'get nacos client' --- configs/api_config.yaml | 2 +- configs/conf.yaml | 14 ++- go.mod | 1 + go.sum | 13 +- pkg/adapter/springcloud/cloud.go | 150 +++++++++++++++++------- pkg/common/extension/adapter/adapter.go | 4 +- pkg/common/yaml/yaml.go | 2 + pkg/registry/consul.go | 9 ++ pkg/registry/load.go | 3 + pkg/registry/nacos/nacos.go | 89 ++++++++++++-- pkg/registry/zookeeper.go | 9 ++ pkg/server/adapter_manager.go | 16 ++- 12 files changed, 248 insertions(+), 64 deletions(-) diff --git a/configs/api_config.yaml b/configs/api_config.yaml index e8696c5d4..82f36b7eb 100644 --- a/configs/api_config.yaml +++ b/configs/api_config.yaml @@ -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" diff --git a/configs/conf.yaml b/configs/conf.yaml index 0dec7c157..1a374c315 100644 --- a/configs/conf.yaml +++ b/configs/conf.yaml @@ -43,7 +43,7 @@ static_resources: http_filters: - name: dgp.filter.http.apiconfig config: - path: $PROJECT_DIR/pixiu/api_config.yaml + path: /Users/yami/Documents/dubbo-go-pixiu/configs/api_config.yaml - name: dgp.filter.http.dubboproxy config: dubboProxyConfig: @@ -80,4 +80,14 @@ static_resources: shutdown_config: timeout: "60s" step_timeout: "10s" - reject_policy: "immediacy" \ No newline at end of file + reject_policy: "immediacy" + adapters: + - name: "dgp.adapter.springcloud" + config: + registries: + "Nacos": + protocol: "Nacos" + timeout: "3s" + address: "127.0.0.1:8848" + username: "" + password: "" \ No newline at end of file diff --git a/go.mod b/go.mod index 9f552ba95..70c4efae8 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/google/uuid v1.2.0 // indirect github.com/hashicorp/consul/api v1.5.0 github.com/mitchellh/mapstructure v1.4.1 + github.com/nacos-group/nacos-sdk-go v1.0.8 github.com/pkg/errors v0.9.1 github.com/prometheus/common v0.29.0 // indirect github.com/shirou/gopsutil v3.21.3+incompatible // indirect diff --git a/go.sum b/go.sum index b6bd9a275..602e2bc51 100644 --- a/go.sum +++ b/go.sum @@ -97,6 +97,7 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alibaba/sentinel-golang v1.0.2 h1:Acopq74hOtZN4MV1v811MQ6QcqPFLDSczTrRXv9zpIg= github.com/alibaba/sentinel-golang v1.0.2/go.mod h1:QsB99f/z35D2AiMrAWwgWE85kDTkBUIkcmPrRt+61NI= +github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 h1:zOVTBdCKFd9JbCKz9/nt+FovbjPFmb7mUnp8nH9fQBA= github.com/aliyun/alibaba-cloud-sdk-go v1.61.18/go.mod h1:v8ESoHo4SyHmuB4b1tJqDHxfTGEciD+yhvOU/5s1Rfk= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/dubbo-getty v1.4.3 h1:PCKpryDasKOxwT5MBC6MIMO+0NLOaHF6Xco9YXQw7HI= @@ -135,6 +136,7 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= +github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23 h1:D21IyuvjDCshj1/qq+pCNd3VZOAEI9jy6Bi131YlXgI= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= @@ -207,8 +209,6 @@ github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyG github.com/docker/go-connections v0.3.0 h1:3lOnM9cSzgGwx8VfK/NGOW5fLQ0GjIlCkaktF+n1M6o= github.com/docker/go-connections v0.3.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= -github.com/dubbogo/dubbo-go-pixiu-filter v0.1.4-0.20210613012702-8488bf80772c h1:Hff+hNfuM7lx01sJy0ZmEZELNGZkNEgU8WQOz8D4Tno= -github.com/dubbogo/dubbo-go-pixiu-filter v0.1.4-0.20210613012702-8488bf80772c/go.mod h1:o0tgVjbQyVxVq27Av9VvB5ZBv6tk4ypfIPkhfTLfmuw= github.com/dubbogo/dubbo-go-pixiu-filter v0.1.4 h1:R7SZYqn+trTQNehpZIOy+ywG2UjkvjJ+FlcPdVjPzXE= github.com/dubbogo/dubbo-go-pixiu-filter v0.1.4/go.mod h1:d6SDK5BHl/QCvg84BN+g6LZS9QzVqnI2+yw0NBu0uac= github.com/dubbogo/go-zookeeper v1.0.3 h1:UkuY+rBsxdT7Bs63QAzp9z7XqQ53W1j8E5rwl83me8g= @@ -240,6 +240,7 @@ github.com/envoyproxy/protoc-gen-validate v0.0.14/go.mod h1:iSmxcyjqTsJpI2R4NaDN github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 h1:Ghm4eQYC0nEPnSJdVkTrXpu9KtoVCSo1hg7mtI7G9KU= github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239/go.mod h1:Gdwt2ce0yfBxPvZrHkprdPPTTS3N5rwmLE8T22KBXlw= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= @@ -262,6 +263,7 @@ github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2/go.mod github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24= github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0= github.com/go-co-op/gocron v0.1.1/go.mod h1:Y9PWlYqDChf2Nbgg7kfS+ZsXHDTZbMZYPEQ0MILqH+M= +github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -517,6 +519,7 @@ github.com/jackc/fake v0.0.0-20150926172116-812a484cc733/go.mod h1:WrMFNQdiFJ80s github.com/jackc/pgx v3.3.0+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I= github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da h1:FjHUJJ7oBW4G/9j1KzlHaXL09LyMVM9rupS39lncbXk= github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da/go.mod h1:ks+b9deReOc7jgqp+e7LuFiCBH6Rm5hL32cLcEAArb4= +github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4= github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag= github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8 h1:mGIXW/lubQ4B+3bXTLxcTMTjUNDqoF6T/HUW9LbFx9s= github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8/go.mod h1:yL958EeXv8Ylng6IfnvG4oflryUi3vgA3xPs9hmII1s= @@ -564,8 +567,11 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 h1:0iQektZGS248WXmGIYOwRXSQhD4qn3icjMpuxwO7qlo= github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570/go.mod h1:BLt8L9ld7wVsvEWQbuLrUZnCMnUmLZ+CGDzKtclrTlE= +github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f h1:sgUSP4zdTUZYZgAGGtN5Lxk92rK+JUFOwf+FT99EEI4= github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f/go.mod h1:UGmTpUd3rjbtfIpwAPrcfmGf/Z1HS95TATB+m57TPB8= +github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 h1:Bvq8AziQ5jFF4BHGAEDSqwPW1NJS3XshxbRCxtjFAZc= github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042/go.mod h1:TPpsiPUEh0zFL1Snz4crhMlBe60PYxRHr5oFF3rRYg0= github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= @@ -636,6 +642,7 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= +github.com/nacos-group/nacos-sdk-go v1.0.8 h1:8pEm05Cdav9sQgJSv5kyvlgfz0SzFUUGI3pWX6SiSnM= github.com/nacos-group/nacos-sdk-go v1.0.8/go.mod h1:hlAPn3UdzlxIlSILAyOXKxjFSvDJ9oLzTJ9hLAK1KzA= github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg= github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU= @@ -830,6 +837,7 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/tebeka/strftime v0.1.3 h1:5HQXOqWKYRFfNyBMNVc9z5+QzuBtIXy03psIhtdJYto= github.com/tebeka/strftime v0.1.3/go.mod h1:7wJm3dZlpr4l/oVK0t1HYIc4rMzQ2XJlOMIUJUJH6XQ= github.com/tencentcloud/tencentcloud-sdk-go v3.0.83+incompatible h1:8uRvJleFpqLsO77WaAh2UrasMOzd8MxXrNj20e7El+Q= github.com/tencentcloud/tencentcloud-sdk-go v3.0.83+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4= @@ -845,6 +853,7 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1 github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 h1:uruHq4dN7GR16kFc5fp3d1RIYzJW5onx8Ybykw2YQFA= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3 h1:kF/7m/ZU+0D4Jj5eZ41Zm3IH/J8OElK1Qtd7tVKAwLk= github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3/go.mod h1:QDlpd3qS71vYtakd2hmdpqhJ9nwv6mD6A30bQ1BPBFE= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 h1:G3dpKMzFDjgEh2q1Z7zUUtKa8ViPtH+ocF0bE0g00O8= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= diff --git a/pkg/adapter/springcloud/cloud.go b/pkg/adapter/springcloud/cloud.go index e54f8ffdb..4f740565c 100644 --- a/pkg/adapter/springcloud/cloud.go +++ b/pkg/adapter/springcloud/cloud.go @@ -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 ( @@ -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. @@ -65,53 +72,115 @@ 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 SpringCloud Manager for control initialize - cloudManager := SpringCloudManager(a.boot) - - cloudManager.initAll() - + //init registry client // 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, + 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) } - // 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) - }() } @@ -122,6 +191,7 @@ func (a *CloudAdapter) Stop() { // Apply init func (a *CloudAdapter) Apply() error { + return nil } diff --git a/pkg/common/extension/adapter/adapter.go b/pkg/common/extension/adapter/adapter.go index 9e24b0e9a..874666b59 100644 --- a/pkg/common/extension/adapter/adapter.go +++ b/pkg/common/extension/adapter/adapter.go @@ -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 diff --git a/pkg/common/yaml/yaml.go b/pkg/common/yaml/yaml.go index 547a44dba..a0254c240 100644 --- a/pkg/common/yaml/yaml.go +++ b/pkg/common/yaml/yaml.go @@ -18,6 +18,7 @@ package yaml import ( + "fmt" "io/ioutil" "path" ) @@ -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 diff --git a/pkg/registry/consul.go b/pkg/registry/consul.go index f64d04cd5..77dbcaeb0 100644 --- a/pkg/registry/consul.go +++ b/pkg/registry/consul.go @@ -18,6 +18,7 @@ package registry import ( + "github.com/apache/dubbo-go-pixiu/pkg/model" "net/url" "strconv" "strings" @@ -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) @@ -121,3 +126,7 @@ func (crl *ConsulRegistryLoad) LoadAllServices() ([]*common.URL, error) { } return urls, nil } + +func (crl *ConsulRegistryLoad) NewRegistry(sr *model.StaticResources) Loader { + return nil +} diff --git a/pkg/registry/load.go b/pkg/registry/load.go index 6784fb02f..57028a18c 100644 --- a/pkg/registry/load.go +++ b/pkg/registry/load.go @@ -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 diff --git a/pkg/registry/nacos/nacos.go b/pkg/registry/nacos/nacos.go index d6d6bb56c..8597526eb 100644 --- a/pkg/registry/nacos/nacos.go +++ b/pkg/registry/nacos/nacos.go @@ -2,26 +2,99 @@ package nacos import ( "bytes" + "fmt" + "github.com/apache/dubbo-go-pixiu/pkg/logger" + "github.com/apache/dubbo-go-pixiu/pkg/model" + "github.com/nacos-group/nacos-sdk-go/vo" "strings" ) import ( + "github.com/apache/dubbo-go-pixiu/pkg/registry" "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" - nacosClient "github.com/dubbogo/gost/database/kv/nacos") + nacosClient "github.com/dubbogo/gost/database/kv/nacos" +) + +func init() { + var _ registry.Loader = new(NacosRegistry) +} // nacos registry type NacosRegistry struct { - - *common.URL + url []*common.URL namingClient *nacosClient.NacosNamingClient registryUrls []*common.URL +} + +const ( + NacosDefaultContext = "/nacos" +) +func (receiver *NacosRegistry) NewRegistryLoader(ad *model.Adapter) (registry.Loader, error) { + return newNacosRegistryLoad(ad), nil } -func (receiver *NacosRegistry) LoadAllServices() ([]*common.URL, error) { +func newNacosRegistryLoad(ad *model.Adapter) registry.Loader { + if ad == nil { + return nil + } + sc, cc, err := GetNacosConfig(ad) + if err != nil { + logger.Errorf("nameClient failed") + } + var tmpClient *NacosRegistry + //for _, adapter := range sr.Adapters { + application := ad.Name + Client, err := nacosClient.NewNacosNamingClient(application, false, sc, cc) + if err != nil { + logger.Info("fail to init Nacos NamingClient") + } + tmpClient = &NacosRegistry{ + namingClient: Client, + registryUrls: []*common.URL{}, + } + //} + return tmpClient +} - return nil, nil +func (receiver *NacosRegistry) NewResistry(ad *model.Adapter) registry.Loader { + return newNacosRegistryLoad(ad) +} + +func (receiver *NacosRegistry) LoadAllServices() ([]*common.URL, error) { + namingClient := receiver.namingClient.Client() + serviceInfo, err := namingClient.GetAllServicesInfo(vo.GetAllServiceInfoParam{ + NameSpace: "", + GroupName: "DEFAULT_GROUP", + PageNo: 1, + PageSize: 20, + }) + dom := serviceInfo.Doms + for d := range dom { + fmt.Println(d) + } + var urlList = []*common.URL{} + if err != nil { + return nil, err + } + for i := 0; int64(i) < serviceInfo.Count; i++ { + serviceInstance, err := namingClient.SelectInstances(vo.SelectInstancesParam{ + ServiceName: serviceInfo.Doms[i], + GroupName: "DEFAULT_GROUP", + HealthyOnly: true, + }) + if err != nil { + logger.Errorf("fail to get instance", err) + } + url := generateUrl(serviceInstance[0]) + urlList = append(urlList, url) + } + receiver.url = urlList + for i, url := range urlList { + fmt.Println("The %d's url is %s", i, url) + } + return urlList, err } func (receiver NacosRegistry) GetCluster() (string, error) { @@ -36,9 +109,3 @@ func appendParam(target *bytes.Buffer, url *common.URL, key string) { target.Write([]byte(value)) } } - - - - - - diff --git a/pkg/registry/zookeeper.go b/pkg/registry/zookeeper.go index 299625719..72150eb2d 100644 --- a/pkg/registry/zookeeper.go +++ b/pkg/registry/zookeeper.go @@ -18,6 +18,7 @@ package registry import ( + "github.com/apache/dubbo-go-pixiu/pkg/model" "path" "strings" "time" @@ -65,6 +66,10 @@ func newZookeeperRegistryLoad(address, cluster string) (Loader, error) { return r, nil } +func (crl *ZookeeperRegistryLoad) NewRegistryLoader(ad *model.Adapter) (Loader, error) { + return nil, nil +} + // nolint func (crl *ZookeeperRegistryLoad) GetCluster() (string, error) { return crl.cluster, nil @@ -96,3 +101,7 @@ func (crl *ZookeeperRegistryLoad) LoadAllServices() ([]*common.URL, error) { } return urls, nil } + +func (crl *ZookeeperRegistryLoad) NewRegistry(sr *model.StaticResources) Loader { + return nil +} diff --git a/pkg/server/adapter_manager.go b/pkg/server/adapter_manager.go index 96b8a71ff..682c933fd 100644 --- a/pkg/server/adapter_manager.go +++ b/pkg/server/adapter_manager.go @@ -31,13 +31,16 @@ type AdapterManager struct { func CreateDefaultAdapterManager(server *Server, bs *model.Bootstrap) *AdapterManager { am := &AdapterManager{configs: bs.StaticResources.Adapters} - am.initAdapters(server, bs) + sr := bs.StaticResources + for _, ad := range sr.Adapters { + am.initAdapters(server, ad) + } return am } func (am *AdapterManager) Start() { - for _, a := range am.adapters { - a.Start() + for i, a := range am.adapters { + a.Start(am.configs[i]) } } @@ -47,7 +50,8 @@ func (am *AdapterManager) Stop() { } } -func (am *AdapterManager) initAdapters(server *Server, bs *model.Bootstrap) { +func (am *AdapterManager) initAdapters(server *Server, ad *model.Adapter) { + var ads []adapter.Adapter for _, f := range am.configs { @@ -56,13 +60,13 @@ func (am *AdapterManager) initAdapters(server *Server, bs *model.Bootstrap) { logger.Error("initAdapters get plugin error %s", err) } - hf, err := hp.CreateAdapter(f.Config, bs) + hf, err := hp.CreateAdapter(f.Config, ad) if err != nil { logger.Error("initFilterIfNeed create adapter error %s", err) } cfg := hf.Config() - if err := yaml.ParseConfig(cfg, f.Config); err != nil { + if err := yaml.ParseConfig(&cfg, f.Config["registries"].(map[string]interface{})); err != nil { logger.Error("initAdapters init config error %s", err) return }