Skip to content

Commit

Permalink
Merge pull request #425 from PhilYue/feature/sc_descovery_strategy
Browse files Browse the repository at this point in the history
Feat: SpringCloud subscribe strategy and Adapter add enabled config
  • Loading branch information
AlexStocks committed May 22, 2022
2 parents 43d5dfd + e08cb6e commit 77f13d7
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 67 deletions.
42 changes: 31 additions & 11 deletions pkg/adapter/springcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const (

Nacos = "nacos"
Zookeeper = "zookeeper"
Consul = "consul"
)

func init() {
Expand Down Expand Up @@ -73,15 +72,28 @@ type (
Registry *model.RemoteConfig `yaml:"registry" json:"registry" default:"registry"`
FreshInterval time.Duration `yaml:"freshInterval" json:"freshInterval" default:"freshInterval"`
Services []string `yaml:"services" json:"services" default:"services"`
// todo configuration the discovery config, like `zookeeper.discovery.root = "/services"`
//Discovery *model.DiscoveryConfig `yaml:"discovery" json:"discovery" default:"discovery"`
// SubscribePolicy subscribe config,
// - adapting : if there is no any Services (App) names, fetch All services from registry center
// - definitely : fetch services by the config Services (App) names
SubscribePolicy string `yaml:"subscribe-policy" json:"subscribe-policy" default:"adapting"`
}

Service struct {
Name string
}

SubscribePolicy int
)

const (
Adapting SubscribePolicy = iota
Definitely
)

func (sp SubscribePolicy) String() string {
return [...]string{"adapting", "definitely"}[sp]
}

// Kind return plugin kind
func (p *CloudPlugin) Kind() string {
return Kind
Expand All @@ -94,26 +106,24 @@ func (p *CloudPlugin) CreateAdapter(ad *model.Adapter) (adapter.Adapter, error)

// Start start the adapter
func (a *CloudAdapter) Start() {

// do not block the main goroutine
// init get all service instance
err := a.firstFetch()
if err != nil {
logger.Errorf("init fetch service fail", err.Error())
//return
}

// background sync service instance from remote
err = a.backgroundSyncPeriod()
if err != nil {
logger.Errorf("init periodicity fetch service task fail", err.Error())
//return
}

// watch then fetch is more safety for consistent but there is background fresh mechanism
err = a.watch()
if err != nil {
logger.Errorf("init watch the register fail", err.Error())
//return
}
}

Expand All @@ -136,7 +146,6 @@ func (a *CloudAdapter) Apply() error {
switch strings.ToLower(a.cfg.Registry.Protocol) {
case Nacos:
sd, err = nacos.NewNacosServiceDiscovery(a.cfg.Services, a.cfg.Registry, a)
case Consul:
case Zookeeper:
sd, err = zookeeper.NewZKServiceDiscovery(a.cfg.Services, a.cfg.Registry, a)
default:
Expand Down Expand Up @@ -208,10 +217,18 @@ func (a *CloudAdapter) fetchServiceByConfig() ([]servicediscovery.ServiceInstanc
var instances []servicediscovery.ServiceInstance
var err error
// if configure specific services, then fetch those service instance only
if len(a.cfg.Services) > 0 {
instances, err = a.sd.QueryServicesByName(a.cfg.Services)
if a.subscribeServiceDefinitely() {
if len(a.cfg.Services) > 0 {
instances, err = a.sd.QueryServicesByName(a.cfg.Services)
} else {
logger.Warnf("No any Service(App) need Subscribe, config the Service(App) Names or make the `subscribe-policy: adapting` pls.")
}
} else {
instances, err = a.sd.QueryAllServices()
if len(a.cfg.Services) > 0 {
instances, err = a.sd.QueryServicesByName(a.cfg.Services)
} else {
instances, err = a.sd.QueryAllServices()
}
}

if err != nil {
Expand Down Expand Up @@ -347,8 +364,11 @@ func (a *CloudAdapter) stop() error {
err := a.sd.Unsubscribe()
if err != nil {
logger.Errorf("unsubscribe registry fail ", err.Error())
//return err
}
close(a.stopChan)
return nil
}

func (a *CloudAdapter) subscribeServiceDefinitely() bool {
return strings.EqualFold(a.cfg.SubscribePolicy, Definitely.String())
}
1 change: 1 addition & 0 deletions pkg/filter/http/httpproxy/routerfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (f *Filter) Decode(hc *http.HttpContext) filter.FilterStatus {
clusterManager := server.GetClusterManager()
endpoint := clusterManager.PickEndpoint(clusterName)
if endpoint == nil {
logger.Debugf("[dubbo-go-pixiu] cluster not found endpoint")
bt, _ := json.Marshal(http.ErrResponse{Message: "cluster not found endpoint"})
hc.SendLocalReply(http3.StatusServiceUnavailable, bt)
return filter.Stop
Expand Down
2 changes: 1 addition & 1 deletion pkg/filter/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (f *Filter) Encode(c *http.HttpContext) filter.FilterStatus {
atomic.AddInt64(&totalElapsed, latency.Nanoseconds())
atomic.AddInt64(&totalCount, 1)

logger.Infof("[Metric] [UPSTREAM] receive request | %d | %s | %s | %s | ", c.GetStatusCode(), latency, c.GetMethod(), c.GetUrl())
logger.Debugf("[Metric] [UPSTREAM] receive request | %d | %s | %s | %s | ", c.GetStatusCode(), latency, c.GetMethod(), c.GetUrl())
return filter.Continue
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/model/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package model

// Adapter the adapter plugin for manage cluster or router
type Adapter struct {
ID string `yaml:"id" json:"id"`
Name string `yaml:"name" json:"name"` // Name the adapter unique name
Config map[string]interface{} `yaml:"config" json:"config" mapstructure:"config"` // Config adapter config
ID string `yaml:"id" json:"id"`
Name string `yaml:"name" json:"name"` // Name the adapter unique name
Enabled string `yaml:"enabled" json:"enabled" default:"true"`
Config map[string]interface{} `yaml:"config" json:"config" mapstructure:"config"` // Config adapter config
}
9 changes: 9 additions & 0 deletions pkg/server/adapter_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package server

import (
"strings"
)

import (
"github.com/apache/dubbo-go-pixiu/pkg/common/extension/adapter"
"github.com/apache/dubbo-go-pixiu/pkg/common/yaml"
Expand Down Expand Up @@ -57,6 +61,11 @@ func (am *AdapterManager) initAdapters() {
logger.Error("initAdapters get plugin error %s", err)
}

if len(f.Enabled) > 0 && !strings.EqualFold(f.Enabled, "true") {
logger.Warnf("the Adapter %s will stop starting, by config of Enabled : %s", f.Name, f.Enabled)
return
}

hf, err := hp.CreateAdapter(f)
if err != nil {
logger.Error("initFilterIfNeed create adapter error %s", err)
Expand Down
8 changes: 5 additions & 3 deletions samples/springcloud/nacos/pixiu/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ static_resources:
adapters:
- id: "springcloud"
name: "dgp.adapter.springcloud"
# enabled: false
config:
freshInterval: 60s # 刷新配置时间
# services:
# - user-service
# - auth-service
subscribe-policy: definitely # adapting-订阅根据系统自适应策略,definitely-服务订阅根据用户配置的services
services:
- user-service
- auth-service
registry:
name: naocs
protocol: nacos
Expand Down
52 changes: 30 additions & 22 deletions samples/springcloud/nacos/server/auth-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,48 @@
<artifactId>spring-cloud-auth-service</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<relativePath/>
</parent>

<properties>
<spring-boot.version>2.5.12</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<spring-boot.version>2.5.13</spring-boot.version>
<spring-cloud.version>2020.0.5</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public class AuthProviderApplication {

public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
SpringApplication.run(AuthProviderApplication.class, args);
}

@RestController
Expand Down
57 changes: 34 additions & 23 deletions samples/springcloud/nacos/server/user-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,56 @@
<artifactId>spring-cloud-user-service</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<relativePath/>
</parent>

<properties>
<spring-boot.version>2.5.12</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>

<spring-boot.version>2.5.13</spring-boot.version>
<spring-cloud.version>2020.0.5</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>

</dependencies>


<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public class UserProviderApplication {

public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
SpringApplication.run(UserProviderApplication.class, args);
}

@RestController
Expand Down

0 comments on commit 77f13d7

Please sign in to comment.