diff --git a/pom.xml b/pom.xml index efba570c8..576f340ce 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-build - 2.3.4.RELEASE + 3.0.1 4.0.0 @@ -71,10 +71,10 @@ - 1.1.4.Hoxton.SR9-SNAPSHOT + 2020.0.1-SNAPSHOT - Hoxton.SR9 + 2020.0.1 0.8.3 diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 7922fa66d..a3d57f40a 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-dependencies-parent - 2.3.1.RELEASE + 3.0.1 4.0.0 @@ -62,7 +62,8 @@ - 1.1.4.Hoxton.SR9-SNAPSHOT + 2020.0.1-SNAPSHOT + 23.0 1.2.0 10.0.0-M6 2.0.0 diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/pom.xml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/pom.xml index 5bf16820e..162294685 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/pom.xml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/pom.xml @@ -20,7 +20,7 @@ ch.qos.logback logback-core - 1.2.3 + 1.2.6 compile @@ -53,12 +53,6 @@ feign-okhttp - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - - org.springframework.retry diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/application.yml similarity index 53% rename from spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/application.yml index 32f2eb16c..78a6fbe2b 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/application.yml @@ -12,8 +12,6 @@ global: timeout: 5s feign: - hystrix: - enabled: true #在Feign中开启Hystrix compression: request: enabled: false #是否对请求进行GZIP压缩 @@ -22,19 +20,5 @@ feign: response: enabled: false #是否对响应进行GZIP压缩 -ribbon: - polaris: - enabled: true - # 同一实例最大重试次数,不包括首次调用 - MaxAutoRetries: 1 - # 重试其他实例的最大重试次数,不包括首次所选的server - MaxAutoRetriesNextServer: 2 - # 是否所有操作都进行重试 - OkToRetryOnAllOperations: false - ConnectionTimeout: 1000 - ReadTimeout: 1000 - eager-load: - enabled: on - serivceB: url: http://localhost:48081 \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/polaris.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/polaris.yml index 39d33bd6a..7ccb9ece0 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/polaris.yml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-a/src/main/resources/polaris.yml @@ -1,287 +1,194 @@ -#描述:全局配置项 global: - #描述系统相关配置 + #描述: 系统相关配置 system: - #描述:SDK运行模式 - #类型:enum - #范围:0(直连模式,SDK直接对接server); 1(代理模式,SDK只对接agent, 通过agent进行server的对接) - #默认值:0 - mode: 0 - #服务发现集群 + #描述: 服务发现集群 discoverCluster: namespace: Polaris service: polaris.discover - #可选:服务刷新间隔 - refreshInterval: 10m - #健康检查集群 + #描述: 服务刷新间隔 + refreshInterval: 1m + #描述: 是否使用埋点地址作为服务发现集群 + sameAsBuiltin: true + #描述: 系统服务使用的路由链 + routers: + - metadataRouter + - nearbyBasedRouter + #描述: 系统服务使用的负载均衡方式 + lbPolicy: weightedRandom + #描述: 健康检查集群 healthCheckCluster: namespace: Polaris service: polaris.healthcheck - #可选:服务刷新间隔 - refreshInterval: 10m - #监控上报集群 + refreshInterval: 1m + sameAsBuiltin: true + routers: + - metadataRouter + - nearbyBasedRouter + lbPolicy: ringHash + #描述: 监控上报集群 monitorCluster: namespace: Polaris service: polaris.monitor - #可选:服务刷新间隔 - refreshInterval: 10m + refreshInterval: 1m + sameAsBuiltin: false + routers: + - metadataRouter + - nearbyBasedRouter + lbPolicy: ringHash + #描述: SDK流程数据缓存配置 + flowCache: + enable: true + name: simpleCache + expireInterval: 60s + #描述: SDK api调用相关配置 api: - #描述:api超时时间 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1ms:...] - #默认值:1s + #描述: api超时时间 timeout: 5s - #描述:上报间隔 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1ms:...] - #默认值:10m + #描述: 客户端给服务端定时上报自身信息的间隔 reportInterval: 10m - #描述:API因为网络原因调用失败后的重试次数 - #类型:int - #范围:[0:...] - #默认值:5 - maxRetryTimes: 5 - #描述:重试间隔 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1s:...] - #默认值:1s - retryInterval: 1s - #描述:对接polaris server的相关配置 + #描述: api因为网络原因调用失败后的最大重试次数 + maxRetryTimes: 1 + #描述: 2次重试之间的重试间隔 + retryInterval: 500ms + #描述: 对接北极星服务端的相关配置 serverConnector: - #描述:访问server的连接协议,SDK会根据协议名称会加载对应的插件 - #类型:string - #范围:已注册的连接器插件名 - #默认值:grpc + #描述:默认服务端埋点接入地址 + addresses: + - 127.0.0.1:8091 + #描述: 访问server的连接协议,SDK会根据协议名称会加载对应的插件 protocol: grpc - #描述:发起连接后的连接超时时间 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1ms:...] - #默认值:200ms + #描述: 发起连接后的连接超时时间 connectTimeout: 500ms - #描述:远程请求超时时间 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1ms:...] - #默认值:1s + #描述: 与服务端发起远程请求超时时间 messageTimeout: 5s - #描述:连接空闲时间,长连接模式下,当连接空闲超过一定时间后,SDK会主动释放连接 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1ms:...] - #默认值:1s - connectionIdleTimeout: 1s - #描述:首次请求的任务队列长度,当用户发起首次服务访问请求时,SDK会对任务进行队列调度并连接server,当积压的任务数超过队列长度后,SDK会直接拒绝首次请求的发起。 - #类型:int - #范围:[0:...] - #默认值:1000 - requestQueueSize: 1000 - #描述:server节点的切换周期,为了使得server的压力能够均衡,SDK会定期针对最新的节点列表进行重新计算自己当前应该连接的节点,假如和当前不一致,则进行切换 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1m:...] - #默认值:10m + #描述: 连接空闲时间(以最后一次消息交互时间来算),长连接模式下,当连接空闲超过一定时间后,SDK会主动释放连接 + connectionIdleTimeout: 60s + #描述: server节点的切换周期,为了使得server的压力能够均衡,SDK会定期切换目标服务端节点 serverSwitchInterval: 10m - plugin: - grpc: - #描述:GRPC客户端单次最大链路接收报文 - #类型:int - #范围:(0:524288000] - maxCallRecvMsgSize: 52428800 - #统计上报设置 + #描述:重连间隔时间 + reconnectInterval: 500ms + #描述: 监控及日志数据上报相关配置 statReporter: - #描述:是否将统计信息上报至monitor - #类型:bool - #默认值:true - enable: true - #描述:启用的统计上报插件类型 - #类型:list - #范围:已经注册的统计上报插件的名字 - #默认值:stat2Monitor(将信息上报至monitor服务) - chain: - - stat2Monitor - - serviceCache - #描述:统计上报插件配置 + #描述: 是否启用上报 + enable: false plugin: - stat2Monitor: - #描述:每次上报多长一段时间的统计信息 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1m:...] - metricsReportWindow: 1m - #描述:将一段时间内的统计信息分为多少个基本单元收集 - #类型:int - #范围:[1:...] - #默认值:12 - metricsNumBuckets: 12 - serviceCache: - #描述:上报缓存信息的周期 - reportInterval: 3m -#描述:主调端配置 + pushgatewayConfig: + #描述: 上报给prometheus pushgateway的任务名称 + jobName: "defaultJob" + #描述: pushgateway的服务名称 + serviceName: "polaris.monitor" + #描述: 上报给pushgateway的时间间隔 + pushInterval: 10 +#描述: 主调端配置 consumer: - #描述:本地缓存相关配置 + #描述: 本地服务缓存相关配置 localCache: - #描述:缓存类型 - #类型:string - #范围:已注册的本地缓存插件名 - #默认值:inmemory(基于本机内存的缓存策略) + #描述: 缓存插件名 type: inmemory - #描述:服务过期淘汰时间 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1m:...] - #默认值:24h + #描述: 是否启用服务数据缓存 + serviceExpireEnable: true + #描述: 服务过期淘汰时间 serviceExpireTime: 24h - #描述:服务定期刷新周期 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1s:...] - #默认值:2s + #描述: 服务定期同步刷新周期 serviceRefreshInterval: 2s - #描述:服务缓存持久化目录,SDK在实例数据更新后,按照服务维度将数据持久化到磁盘 - #类型:string - #格式:本机磁盘目录路径,支持$HOME变量 - #默认值:$HOME/polaris/backup + #描述: 是否启用服务数据文件缓存 + persistEnable: true + #描述: 服务缓存持久化目录,SDK在实例数据更新后,按照服务维度将数据持久化到磁盘 persistDir: ./polaris/backup - #描述:缓存写盘失败的最大重试次数 - #类型:int - #范围:[1:...] - #默认值:5 - persistMaxWriteRetry: 5 - #描述:缓存从磁盘读取失败的最大重试次数 - #类型:int - #范围:[1:...] - #默认值:1 - persistMaxReadRetry: 1 - #描述:缓存读写磁盘的重试间隔 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1ms:...] - #默认值:1s - persistRetryInterval: 1s - #描述:服务路由相关配置 + #描述: 缓存写盘失败的最大重试次数 + persistMaxWriteRetry: 1 + #描述: 缓存从磁盘读取失败的最大重试次数 + persistMaxReadRetry: 0 + #描述: 缓存读写磁盘的重试间隔 + persistRetryInterval: 500ms + #描述: 服务路由相关配置 serviceRouter: - # 服务路由链 + #描述: 前置路由链 + beforeChain: + # 隔离路由 + - isolatedRouter + #描述: 服务路由链 chain: - # 基于主调和被调服务规则的路由策略(默认的路由策略) + # 元数据路由 + - metadataRouter + # 规则路由 - ruleBasedRouter - # 就近路由策略 + # 就近路由 - nearbyBasedRouter + #描述: 后置路由链 + afterChain: + # 兜底(全死全活)路由 + - recoverRouter #描述:服务路由插件的配置 plugin: + metadataRouter: + #描述: 元数据路由降级策略。none(不降级), all(降级返回所有的节点), others(降级返回其他节点) + metadataFailOverType: none nearbyBasedRouter: - #描述:就近路由的最小匹配级别 - #类型:string - #范围:region(大区)、zone(区域)、campus(园区) - #默认值:zone + #描述: 就近路由的最小匹配级别。region(大区)、zone(区域)、campus(园区) matchLevel: zone - ruleBasedRouter: { } - recoverRouter: - #至少应该返回多少比率的实例,如果不填,默认0%,即全死全活 - percentOfMinInstances: 0 - #是否开启全死全活,默认开启 - enableRecoverAll: true + #描述: 最大匹配级别 + maxMatchLevel: all + #描述: 强制就近 + strictNearby: false + #描述: 全部实例不健康时是否降级其他地域 + enableDegradeByUnhealthyPercent: false + #描述: 达到降级标准的不健康实例百分比 + unhealthyPercentToDegrade: 100 + #描述: 是否通过上报方式获取地域信息 + enableReportLocalAddress: false #描述:负载均衡相关配置 loadbalancer: - #描述:负载均衡类型 - #范围:已注册的负载均衡插件名 - #默认值:权重随机负载均衡 + #描述: 负载均衡类型(已注册的负载均衡插件名) type: weightedRandom - plugin: - #描述:虚拟节点的数量 - #类型:int - #默认值:500 - ringHash: - vnodeCount: 500 - #描述:节点熔断相关配置 circuitBreaker: - #描述:是否启用节点熔断功能 - #类型:bool - #默认值:true + #描述: 是否启用本地节点熔断功能 enable: true - #描述:实例定时熔断检测周期 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[100ms:...] - #默认值:30s - checkPeriod: 100ms - #描述:熔断器半开后最大允许的请求数 - #类型:int - #范围:[3:...] - #默认值:10 - requestCountAfterHalfOpen: 10 - #描述:熔断器打开后,多久后转换为半开状态 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1s:...] - #默认值:30s - sleepWindow: 60s - #描述:熔断器半开到关闭所必须的最少成功请求数 - #类型:int - #范围:[1:requestCountAfterHalfOpen] - #默认值:8 - successCountAfterHalfOpen: 8 - #描述:熔断器半开到关闭的统计周期 - #类型:string - #范围:[10s:...] - #默认值:60s - recoverWindow: 60s - #描述:熔断器半开到关闭的统计滑桶数 - #类型:int - #范围:[1:...] - #默认值:10 - recoverNumBuckets: 10 - #描述:熔断策略,SDK会根据策略名称加载对应的熔断器插件 - #类型:list - #范围:已注册的熔断器插件名 - #默认值:基于周期连续错误数熔断(errorCount)、以及基于周期错误率的熔断策略(errorRate) + #描述: 故障检测周期,根据周期内故障进行熔断 + checkPeriod: 1m + #描述: 首次熔断时间,后续熔断时间=重试次数*sleepWindow + sleepWindow: 30s + #描述: 熔断器半开后最大允许的请求数 + requestCountAfterHalfOpen: 3 + #描述: 熔断器半开到关闭所必须的最少成功请求数 + successCountAfterHalfOpen: 3 + #描述:熔断策略,SDK会根据策略名称加载对应的熔断器插件(已注册的熔断器插件名) chain: - errorCount - errorRate - #描述:熔断插件配置 + #描述: 熔断插件配置 plugin: #描述:基于周期连续错误数熔断策略配置 errorCount: - #描述:触发连续错误熔断的阈值 - #类型:int - #范围:[1:...] - #默认值:10 - continuousErrorThreshold: 1 - #描述:连续错误数的最小统计单元数量 - #类型:int - #范围:[1:...] - #默认值:10 - metricNumBuckets: 1 - #描述:连续失败的统计周期 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[10ms:...] - #默认值:1m - metricStatTimeWindow: 100ms + #描述: 触发连续错误熔断的阈值 + continuousErrorThreshold: 10 #描述:基于周期错误率的熔断策略配置 errorRate: - #描述:触发错误率熔断的阈值 - #类型:double - #范围:(0:1] - #默认值:0.5 - errorRateThreshold: 0.01 - #描述:错误率熔断的最小统计单元数量 - #类型:int - #范围:[1:...] - #默认值:5 + #描述:触发错误率熔断的阈值百分比 + errorRateThreshold: 1 + #描述: 错误率熔断的滑窗数量 metricNumBuckets: 5 - #描述:错误率熔断的统计周期 - #类型:string - #格式:^\d+(ms|s|m|h)$ - #范围:[1s:...] - #默认值:1m - metricStatTimeWindow: 1s - #描述:触发错误率熔断的最低请求阈值 - #类型:int - #范围:(0:...] - #默认值:10 - requestVolumeThreshold: 1 + #描述: 触发错误率熔断的最低请求阈值 + requestVolumeThreshold: 10 + #描述:主动探测相关配置 + outlierDetection: + #描述:何时开启主动探测。never(永不开启),on_recover(恢复时才开启主动探测),always(一直开启主动探测) + when: never + #描述:主动探测周期 + checkPeriod: 30s + #描述:主动探测插件链 + chain: + - http + - tcp + - udp +# 被调方配置 +provider: + # 限流配置 + rateLimit: + # 是否开启限流功能 + enable: true + # 限流本地存在的最大窗口限制 + maxWindowCount: 10000000 + # 超过最大窗口限制时的策略。pass(放通), reject(限流) + fallbackOnExceedWindowCount: pass \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/pom.xml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/pom.xml index 2c56070ed..c2651bb25 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/pom.xml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/pom.xml @@ -20,7 +20,7 @@ ch.qos.logback logback-core - 1.2.3 + 1.2.6 compile @@ -51,12 +51,6 @@ feign-okhttp - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - - org.springframework.retry diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/application.yml similarity index 53% rename from spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/application.yml index eedf3e1ee..68a96039d 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/polaris-circuitbreaker-example-b/src/main/resources/application.yml @@ -12,8 +12,6 @@ global: timeout: 5s feign: - hystrix: - enabled: true #在Feign中开启Hystrix compression: request: enabled: false #是否对请求进行GZIP压缩 @@ -22,19 +20,5 @@ feign: response: enabled: false #是否对响应进行GZIP压缩 -ribbon: - polaris: - enabled: true - # 同一实例最大重试次数,不包括首次调用 - MaxAutoRetries: 1 - # 重试其他实例的最大重试次数,不包括首次所选的server - MaxAutoRetriesNextServer: 2 - # 是否所有操作都进行重试 - OkToRetryOnAllOperations: false - ConnectionTimeout: 1000 - ReadTimeout: 1000 - eager-load: - enabled: on - serivceB: url: http://localhost:48081 \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml index d337185ec..f5591f58c 100644 --- a/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-circuitbreaker-example/pom.xml @@ -42,18 +42,6 @@ spring-cloud-starter-openfeign - - - org.springframework.cloud - spring-cloud-starter-netflix-hystrix - - - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - - org.springframework.retry diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml index afdba7052..c225faf4a 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/pom.xml @@ -14,6 +14,11 @@ Polaris Discovery Callee Service + + org.springframework.boot + spring-boot-starter-web + + spring-cloud-starter-tencent-polaris-discovery com.tencent.cloud diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/application.yml similarity index 88% rename from spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/application.yml index ea3cd0aa8..cd2e70c62 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/application.yml @@ -1,5 +1,4 @@ server: - session-timeout: 1800 port: 48081 spring: application: diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml index e3372c51a..5b7aac6c9 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/pom.xml @@ -18,6 +18,11 @@ + + org.springframework.boot + spring-boot-starter-web + + spring-cloud-starter-tencent-polaris-discovery com.tencent.cloud diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java index af338861f..e73565acd 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java @@ -18,6 +18,8 @@ package com.tencent.cloud.polaris.discovery.service.caller; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -37,6 +39,12 @@ public class DiscoveryCallerController { @Autowired private DiscoveryCalleeService discoveryCalleeService; + @Configuration + @LoadBalancerClient(value = "service-provider") + class DiscoveryCallerConfiguration { + + } + /** * 获取相加完的结果 * diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/application.yml similarity index 92% rename from spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/application.yml index 43930793e..106b5ee89 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/application.yml @@ -1,10 +1,11 @@ server: - session-timeout: 1800 port: 48080 spring: application: name: DiscoveryCallerService cloud: + loadbalancer: + configurations: default polaris: address: grpc://127.0.0.1:8091 consul: diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml b/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml index f4f0add80..f6c4cbc3b 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/pom.xml @@ -20,6 +20,7 @@ + org.springframework.boot spring-boot-starter-web @@ -43,16 +44,9 @@ spring-cloud-starter-openfeign - - - org.springframework.cloud - spring-cloud-starter-netflix-hystrix - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon + spring-cloud-starter-loadbalancer diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/pom.xml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/pom.xml deleted file mode 100644 index 0c149c8d4..000000000 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - polaris-gateway-example - com.tencent.cloud - ${revision} - ../pom.xml - - 4.0.0 - - gateway-zuul-service - Spring Cloud Starter Tencent Polaris Gateway Zuul Example - - - - spring-cloud-starter-tencent-polaris-discovery - com.tencent.cloud - - - - com.tencent.cloud - spring-cloud-tencent-polaris-gateway - - - - org.springframework.cloud - spring-cloud-starter-netflix-zuul - - - - org.springframework.boot - spring-boot-starter-web - - - - \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/application.yml deleted file mode 100644 index c191c5e4b..000000000 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/application.yml +++ /dev/null @@ -1,20 +0,0 @@ -server: - session-timeout: 1800 - port: 48082 -spring: - application: - name: GatewayZuulService - cloud: - polaris: - address: grpc://127.0.0.1:8091 - tencent: - metadata: - content: - a: 1 - transitive: - - a -zuul: - routes: - GatewayCalleeService: - serviceId: GatewayCalleeService - path: /GatewayCalleeService/** diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/pom.xml b/spring-cloud-tencent-examples/polaris-gateway-example/pom.xml index 073bd0e6b..f3815f60d 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-gateway-example/pom.xml @@ -15,7 +15,6 @@ Spring Cloud Starter Tencent Polaris Gateway Example - gateway-zuul-service gateway-scg-service gateway-callee-service @@ -32,19 +31,6 @@ org.springframework.cloud spring-cloud-starter-openfeign - - - - org.springframework.cloud - spring-cloud-starter-netflix-hystrix - - - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - - org.springframework.retry diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/pom.xml b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/pom.xml index b37077e3d..47336b35b 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/pom.xml @@ -13,11 +13,24 @@ ratelimit-callee-service + + + ch.qos.logback + logback-core + 1.2.6 + compile + + org.springframework.boot spring-boot-starter-web + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + spring-cloud-starter-tencent-polaris-discovery com.tencent.cloud @@ -30,5 +43,9 @@ org.springframework.boot spring-boot-actuator-autoconfigure + + org.springframework.cloud + spring-cloud-loadbalancer + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java index 767907446..b9b8f04f3 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/BusinessController.java @@ -20,6 +20,8 @@ package com.tencent.cloud.ratelimit.example.service.callee; import java.util.concurrent.atomic.AtomicInteger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; +import org.springframework.context.annotation.Configuration; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -43,6 +45,12 @@ public class BusinessController { @Value("${spring.application.name}") private String appName; + @Configuration + @LoadBalancerClient(value = "service-provider") + class RateLimitCalleeConfiguration { + + } + /** * 获取当前服务的信息 diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/RateLimitCalleeService.java b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/RateLimitCalleeService.java index 342d12804..362f1d7e3 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/RateLimitCalleeService.java +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/java/com/tencent/cloud/ratelimit/example/service/callee/RateLimitCalleeService.java @@ -20,6 +20,7 @@ package com.tencent.cloud.ratelimit.example.service.callee; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @@ -28,7 +29,7 @@ import org.springframework.web.client.RestTemplate; * @author Haotian Zhang */ @SpringBootApplication -@EnableAutoConfiguration +@EnableDiscoveryClient public class RateLimitCalleeService { @Bean diff --git a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/application.yml similarity index 67% rename from spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/application.yml index 9974b810f..29a3125c6 100644 --- a/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-ratelimit-example/ratelimit-callee-service/src/main/resources/application.yml @@ -1,11 +1,12 @@ server: - session-timeout: 1800 port: 48081 spring: application: name: RateLimitCalleeService cloud: + loadbalancer: + configurations: default polaris: address: grpc://127.0.0.1:8091 discovery: - namespace: Test \ No newline at end of file + namespace: default \ No newline at end of file diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml index 632fde33f..c0697f5ad 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml @@ -52,11 +52,5 @@ spring-boot-starter-test test - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - test - \ No newline at end of file diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBeanPostProcessor.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBeanPostProcessor.java index 339f3a589..3d03524b4 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBeanPostProcessor.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBeanPostProcessor.java @@ -23,11 +23,11 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties; import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient; -import org.springframework.cloud.netflix.ribbon.SpringClientFactory; +import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient; -import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; -import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; +import org.springframework.cloud.openfeign.loadbalancer.RetryableFeignBlockingLoadBalancerClient; /** * Wrap Spring Bean and decorating proxy for Feign Client. @@ -50,16 +50,14 @@ public class PolarisFeignBeanPostProcessor implements BeanPostProcessor, BeanFac } private Object wrapper(Object bean) { + if (isNeedWrap(bean)) { - if (bean instanceof LoadBalancerFeignClient) { - LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean); - return new PolarisLoadBalancerFeignClient(createPolarisFeignClient(client.getDelegate()), factory(), - clientFactory()); - } - if (bean instanceof FeignBlockingLoadBalancerClient) { - FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) bean; + if (bean instanceof RetryableFeignBlockingLoadBalancerClient) { + RetryableFeignBlockingLoadBalancerClient client = (RetryableFeignBlockingLoadBalancerClient) bean; return new PolarisFeignBlockingLoadBalancerClient(createPolarisFeignClient(client.getDelegate()), - factory.getBean(BlockingLoadBalancerClient.class)); + factory.getBean(BlockingLoadBalancerClient.class), + factory.getBean(LoadBalancerProperties.class), + factory.getBean(LoadBalancerClientFactory.class)); } return createPolarisFeignClient((Client) bean); } @@ -68,8 +66,7 @@ public class PolarisFeignBeanPostProcessor implements BeanPostProcessor, BeanFac private boolean isNeedWrap(Object bean) { return bean instanceof Client && !(bean instanceof PolarisFeignClient) - && !(bean instanceof PolarisFeignBlockingLoadBalancerClient) - && !(bean instanceof PolarisLoadBalancerFeignClient); + && !(bean instanceof PolarisFeignBlockingLoadBalancerClient); } private PolarisFeignClient createPolarisFeignClient(Client delegate) { @@ -80,12 +77,4 @@ public class PolarisFeignBeanPostProcessor implements BeanPostProcessor, BeanFac public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.factory = beanFactory; } - - CachingSpringLoadBalancerFactory factory() { - return this.factory.getBean(CachingSpringLoadBalancerFactory.class); - } - - SpringClientFactory clientFactory() { - return this.factory.getBean(SpringClientFactory.class); - } } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java index 076e269e6..31c0d6f70 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClient.java @@ -18,7 +18,9 @@ package com.tencent.cloud.polaris.circuitbreaker.feign; import feign.Client; -import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient; +import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; +import org.springframework.cloud.client.loadbalancer.LoadBalancerProperties; +import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient; /** @@ -28,9 +30,11 @@ import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalance */ public class PolarisFeignBlockingLoadBalancerClient extends FeignBlockingLoadBalancerClient { + public PolarisFeignBlockingLoadBalancerClient(Client delegate, - BlockingLoadBalancerClient loadBalancerClient) { - super(delegate, loadBalancerClient); + LoadBalancerClient loadBalancerClient, + LoadBalancerProperties properties, + LoadBalancerClientFactory loadBalancerClientFactory) { + super(delegate, loadBalancerClient, properties, loadBalancerClientFactory); } - } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java index a6a15d19e..c85ac1614 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignClientTest.java @@ -51,9 +51,6 @@ public class PolarisFeignClientTest { if (client instanceof PolarisFeignClient) { return; } - if (client instanceof PolarisLoadBalancerFeignClient) { - return; - } if (client instanceof PolarisFeignBlockingLoadBalancerClient) { return; } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml index fa2cabb05..523a41b03 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml @@ -31,12 +31,23 @@ + + org.springframework.cloud + spring-cloud-loadbalancer + true + + com.tencent.polaris polaris-discovery-factory + + com.tencent.polaris + polaris-router-factory + + com.tencent.polaris polaris-test-common @@ -50,11 +61,6 @@ - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - - org.springframework.boot spring-boot-starter-web @@ -67,6 +73,12 @@ true + + org.springframework.cloud + spring-cloud-loadbalancer + true + + org.springframework.boot spring-boot-starter-test @@ -90,5 +102,6 @@ powermock-api-mockito2 test + \ No newline at end of file diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java index be31339d4..cbeb95379 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java @@ -69,8 +69,8 @@ public class PolarisProperties { /** * 使用spring cloud监听端口 */ - @Value("${server.port:}") - private int port; + @Value("${server.port:#{-1}}") + private int port = -1; /** * 是否开启负载均衡 @@ -78,14 +78,12 @@ public class PolarisProperties { @Value("${spring.cloud.polaris.discovery.loadbalancer.enabled:#{true}}") private Boolean loadbalancerEnabled; - /** * loadbalnce strategy */ @Value("${spring.cloud.polaris.discovery.loadbalancer.policy:#{'weightedRandom'}}") private String policy; - /** * loadbalnce strategy */ diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/ConditionalOnLoadBalancerPolaris.java similarity index 52% rename from spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java rename to spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/ConditionalOnLoadBalancerPolaris.java index 420cf27e3..e85b3de52 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/ConditionalOnLoadBalancerPolaris.java @@ -15,23 +15,20 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.circuitbreaker.feign; +package com.tencent.cloud.polaris.loadbalancer; -import feign.Client; -import org.springframework.cloud.netflix.ribbon.SpringClientFactory; -import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory; -import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; /** - * Wrap for {@link LoadBalancerFeignClient} - * - * @author Haotian Zhang + * @author liaochuntao */ -public class PolarisLoadBalancerFeignClient extends LoadBalancerFeignClient { +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@ConditionalOnProperty(value = "spring.cloud.loadbalancer.polaris.enabled", havingValue = "true") +public @interface ConditionalOnLoadBalancerPolaris { - public PolarisLoadBalancerFeignClient(Client delegate, - CachingSpringLoadBalancerFactory lbClientFactory, - SpringClientFactory clientFactory) { - super(delegate, lbClientFactory, clientFactory); - } } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerPolarisAutoConfiguration.java similarity index 72% rename from spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java rename to spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerPolarisAutoConfiguration.java index 869c73281..f6ae43f70 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerPolarisAutoConfiguration.java @@ -15,13 +15,11 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.ribbon; +package com.tencent.cloud.polaris.loadbalancer; import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; -import org.springframework.cloud.netflix.ribbon.RibbonClients; +import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; import org.springframework.context.annotation.Configuration; /** @@ -29,9 +27,9 @@ import org.springframework.context.annotation.Configuration; */ @Configuration(proxyBeanMethods = false) @EnableConfigurationProperties +@ConditionalOnLoadBalancerPolaris @ConditionalOnPolarisDiscoveryEnabled -@AutoConfigureAfter(RibbonAutoConfiguration.class) -@RibbonClients(defaultConfiguration = PolarisRibbonServerListConfiguration.class) -public class PolarisDiscoveryRibbonAutoConfiguration { +@LoadBalancerClients(defaultConfiguration = PolarisLoadBalancerClientConfiguration.class) +public class LoadBalancerPolarisAutoConfiguration { } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerClientConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerClientConfiguration.java new file mode 100644 index 000000000..149683435 --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancerClientConfiguration.java @@ -0,0 +1,93 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.loadbalancer; + +import com.tencent.cloud.constant.LoadbalancerConstant; +import com.tencent.cloud.polaris.PolarisProperties; +import com.tencent.polaris.router.api.core.RouterAPI; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled; +import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; +import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; +import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; +import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.core.env.Environment; + +/** + * @author liaochuntao + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnDiscoveryEnabled +public class PolarisLoadBalancerClientConfiguration { + + @Bean + @ConditionalOnMissingBean + public ReactorLoadBalancer polarisLoadBalancer(Environment environment, + LoadBalancerClientFactory loadBalancerClientFactory, + PolarisProperties polarisProperties, + RouterAPI routerAPI) { + String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); + return new PolarisLoadbalancer( + name, loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), + polarisProperties, routerAPI); + } + + @Configuration(proxyBeanMethods = false) + @ConditionalOnReactiveDiscoveryEnabled + @Order(LoadbalancerConstant.DISCOVERY_SERVICE_INSTANCE_SUPPLIER_ORDER) + public static class ReactiveSupportConfiguration { + + @Bean + @ConditionalOnBean(ReactiveDiscoveryClient.class) + @ConditionalOnMissingBean + @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true) + public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier( + ConfigurableApplicationContext context) { + return ServiceInstanceListSupplier.builder().withDiscoveryClient() + .build(context); + } + + } + + @Configuration(proxyBeanMethods = false) + @ConditionalOnBlockingDiscoveryEnabled + @Order(LoadbalancerConstant.DISCOVERY_SERVICE_INSTANCE_SUPPLIER_ORDER + 1) + public static class BlockingSupportConfiguration { + + @Bean + @ConditionalOnBean(DiscoveryClient.class) + @ConditionalOnMissingBean + @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true) + public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier( + ConfigurableApplicationContext context) { + return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient() + .build(context); + } + } + +} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadbalancer.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadbalancer.java new file mode 100644 index 000000000..755ffba42 --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadbalancer.java @@ -0,0 +1,114 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + + +package com.tencent.cloud.polaris.loadbalancer; + +import com.tencent.cloud.metadata.context.MetadataContextHolder; +import com.tencent.cloud.polaris.PolarisProperties; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; + +import com.tencent.cloud.polaris.pojo.PolarisServiceInstance; +import com.tencent.polaris.api.pojo.DefaultServiceInstances; +import com.tencent.polaris.api.pojo.Instance; +import com.tencent.polaris.api.pojo.ServiceInstances; +import com.tencent.polaris.api.pojo.ServiceKey; +import com.tencent.polaris.api.rpc.Criteria; +import com.tencent.polaris.router.api.core.RouterAPI; +import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest; +import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.loadbalancer.DefaultResponse; +import org.springframework.cloud.client.loadbalancer.EmptyResponse; +import org.springframework.cloud.client.loadbalancer.Request; +import org.springframework.cloud.client.loadbalancer.Response; +import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier; +import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer; +import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; +import reactor.core.publisher.Mono; + +/** + * @author liaochuntao + */ +public class PolarisLoadbalancer extends RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer { + + private static final Logger log = LoggerFactory.getLogger(PolarisLoadbalancer.class); + + private final String serviceId; + + private ObjectProvider supplierObjectProvider; + + private final PolarisProperties discoveryProperties; + + private final RouterAPI routerAPI; + + public PolarisLoadbalancer(String serviceId, + ObjectProvider supplierObjectProvider, + PolarisProperties discoveryProperties, RouterAPI routerAPI) { + super(supplierObjectProvider, serviceId); + this.serviceId = serviceId; + this.supplierObjectProvider = supplierObjectProvider; + this.discoveryProperties = discoveryProperties; + this.routerAPI = routerAPI; + } + + @Override + public Mono> choose(Request request) { + if (!discoveryProperties.getLoadbalancerEnabled()) { + return super.choose(request); + } + ServiceInstanceListSupplier supplier = supplierObjectProvider.getIfAvailable(NoopServiceInstanceListSupplier::new); + return supplier.get().next().map(this::getInstanceResponse); + } + + private Response getInstanceResponse(List serviceInstances) { + if (serviceInstances.isEmpty()) { + log.warn("No servers available for service: " + this.serviceId); + return new EmptyResponse(); + } + + ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest(); + request.setDstInstances(convertToPolarisServiceInstances(serviceInstances)); + request.setLbPolicy(discoveryProperties.getPolicy()); + request.setCriteria(new Criteria()); + + try { + ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(request); + return new DefaultResponse(new PolarisServiceInstance(response.getTargetInstance())); + } + catch (Exception e) { + log.warn("PolarisLoadbalancer error", e); + return new EmptyResponse(); + } + } + + private static ServiceInstances convertToPolarisServiceInstances(List serviceInstances) { + ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, serviceInstances.get(0).getServiceId()); + List polarisInstances = serviceInstances + .stream() + .map(serviceInstance -> ((PolarisServiceInstance) serviceInstance).getPolarisInstance()) + .collect(Collectors.toList()); + return new DefaultServiceInstances(serviceKey, polarisInstances); + } + +} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java index 9b33854f0..d22a3d04f 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java @@ -23,6 +23,7 @@ import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegis import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -43,9 +44,10 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat @Override protected PolarisRegistration getRegistration() { - if (this.registration.getPort() <= 0) { + if (this.registration.getPort() < 0 && this.getPort().get() > 0) { this.registration.setPort(this.getPort().get()); } + Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set"); return this.registration; } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index d99f30b9b..9a7c76236 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -175,16 +175,13 @@ public class PolarisServiceRegistry implements ServiceRegistry { * @param heartbeatRequest 心跳请求 */ public void heartbeat(InstanceHeartbeatRequest heartbeatRequest) { - heartbeatExecutor.scheduleWithFixedDelay(new Runnable() { - @Override - public void run() { - try { - polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); - } catch (PolarisException e) { - log.error("polaris heartbeat[{}]", e.getCode(), e); - } catch (Exception e) { - log.error("polaris heartbeat runtime error", e); - } + heartbeatExecutor.scheduleWithFixedDelay(() -> { + try { + polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); + } catch (PolarisException e) { + log.error("polaris heartbeat[{}]", e.getCode(), e); + } catch (Exception e) { + log.error("polaris heartbeat runtime error", e); } }, 0, ttl, TimeUnit.SECONDS); } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java deleted file mode 100644 index 74efa1320..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListConfiguration.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.ribbon; - -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.Server; -import com.netflix.loadbalancer.ServerList; -import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * @author Haotian Zhang, Andrew Shan, Jie Cheng - */ -@Configuration -public class PolarisRibbonServerListConfiguration { - - @Bean - @ConditionalOnMissingBean - public ServerList ribbonServerList(PolarisDiscoveryHandler polarisDiscoveryHandler, - IClientConfig iClientConfig) { - PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler); - serverList.initWithNiwsConfig(iClientConfig); - return serverList; - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java deleted file mode 100644 index 7ae86665b..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.ribbon; - -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.AbstractServerList; -import com.netflix.loadbalancer.Server; -import com.tencent.cloud.polaris.pojo.PolarisServer; -import com.tencent.polaris.api.pojo.Instance; -import com.tencent.polaris.api.pojo.ServiceInstances; -import com.tencent.polaris.api.rpc.InstancesResponse; -import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author Haotian Zhang, Andrew Shan, Jie Cheng - */ -public class PolarisServerList extends AbstractServerList { - - private String serviceId; - - private PolarisDiscoveryHandler polarisDiscoveryHandler; - - public PolarisServerList(PolarisDiscoveryHandler polarisDiscoveryHandler) { - this.polarisDiscoveryHandler = polarisDiscoveryHandler; - } - - @Override - public List getInitialListOfServers() { - return getServers(); - } - - @Override - public List getUpdatedListOfServers() { - return getServers(); - } - - private List getServers() { - InstancesResponse filteredInstances = polarisDiscoveryHandler.getInstances(serviceId); - ServiceInstances serviceInstances = filteredInstances.toServiceInstances(); - List polarisServers = new ArrayList<>(); - for (Instance instance : serviceInstances.getInstances()) { - polarisServers.add(new PolarisServer(serviceInstances, instance)); - } - return polarisServers; - } - - public String getServiceId() { - return serviceId; - } - - @Override - public void initWithNiwsConfig(IClientConfig iClientConfig) { - this.serviceId = iClientConfig.getClientName(); - } - -} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories index 75b02a918..26613d3af 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/spring.factories @@ -1,5 +1,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration,\ - com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration,\ com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListAutoConfigurationTest.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListAutoConfigurationTest.java index 93b5c7c47..59f4568cc 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListAutoConfigurationTest.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisRibbonServerListAutoConfigurationTest.java @@ -22,8 +22,6 @@ import static com.tencent.polaris.test.common.Consts.PORT; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; -import com.netflix.client.config.DefaultClientConfigImpl; -import com.netflix.client.config.IClientConfig; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import org.junit.Test; @@ -40,7 +38,6 @@ public class PolarisRibbonServerListAutoConfigurationTest { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration(AutoConfigurations.of( - PolarisRibbonClientTest.class, PolarisDiscoveryClientConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) @@ -48,36 +45,4 @@ public class PolarisRibbonServerListAutoConfigurationTest { .withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST) .withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx"); - @Test - public void testProperties() { - this.contextRunner.run(context -> { - PolarisDiscoveryHandler discoveryHandler = context.getBean(PolarisDiscoveryHandler.class); - PolarisServerList serverList = new PolarisServerList(discoveryHandler); - IClientConfig iClientConfig = context.getBean(IClientConfig.class); - serverList.initWithNiwsConfig(iClientConfig); - - assertThat(serverList.getServiceId()).isEqualTo(SERVICE_PROVIDER); - }); - } - - @Configuration - @EnableAutoConfiguration - @EnableDiscoveryClient - static class PolarisRibbonClientTest { - - @Bean - IClientConfig iClientConfig() { - DefaultClientConfigImpl config = new DefaultClientConfigImpl(); - config.setClientName(SERVICE_PROVIDER); - return config; - } - - @Bean - @LoadBalanced - RestTemplate restTemplate() { - return new RestTemplate(); - } - - } - } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java index f42fcb68b..12f03cc6a 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/ribbon/PolarisServerListTest.java @@ -24,13 +24,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.Server; import com.tencent.cloud.polaris.context.PolarisContextConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; -import java.util.List; import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.test.mock.discovery.NamingServer; @@ -75,56 +72,6 @@ public class PolarisServerListTest { } } - /** - * Test {@link PolarisServerList#getInitialListOfServers()} with empty server list. - */ - @Test - @SuppressWarnings("unchecked") - public void test1(){ - this.contextRunner.run(context -> { - // mock - IClientConfig iClientConfig = mock(IClientConfig.class); - when(iClientConfig.getClientName()).thenReturn(SERVICE_PROVIDER); - PolarisDiscoveryHandler polarisDiscoveryHandler = context.getBean(PolarisDiscoveryHandler.class); - PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler); - serverList.initWithNiwsConfig(iClientConfig); - - List servers = serverList.getInitialListOfServers(); - assertThat(servers).isEmpty(); - }); - } - - /** - * Test {@link PolarisServerList#getUpdatedListOfServers()} with server list of size 3. - */ - @Test - @SuppressWarnings("unchecked") - public void test2() throws Exception { - this.contextRunner.run(context -> { - // mock - IClientConfig iClientConfig = mock(IClientConfig.class); - when(iClientConfig.getClientName()).thenReturn(SERVICE_PROVIDER); - PolarisDiscoveryHandler polarisDiscoveryHandler = context.getBean(PolarisDiscoveryHandler.class); - PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler); - serverList.initWithNiwsConfig(iClientConfig); - - - // add service with 3 instances - NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter(); - instanceParameter.setHealthy(true); - instanceParameter.setIsolated(false); - instanceParameter.setWeight(100); - ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER); - namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3, instanceParameter); - - List servers = serverList.getUpdatedListOfServers(); - assertThat(servers).hasSize(3); - assertThat(servers.get(0).getPort()).isEqualTo(PORT); - assertThat(servers.get(1).getPort()).isEqualTo(PORT + 1); - assertThat(servers.get(2).getPort()).isEqualTo(PORT + 2); - }); - } - @Configuration @EnableAutoConfiguration @EnableDiscoveryClient diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-ratelimit/pom.xml b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-ratelimit/pom.xml index acf7e431c..568e0a975 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-ratelimit/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-ratelimit/pom.xml @@ -67,12 +67,6 @@ test - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - test - - org.powermock powermock-module-junit4 diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/pom.xml b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/pom.xml index 41697663a..cbfd8ad68 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/pom.xml @@ -31,6 +31,12 @@ + + org.springframework.cloud + spring-cloud-loadbalancer + true + + com.tencent.polaris @@ -43,12 +49,6 @@ test - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - - org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterAutoConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterAutoConfiguration.java new file mode 100644 index 000000000..01db9664f --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterAutoConfiguration.java @@ -0,0 +1,112 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.router; + +import com.tencent.cloud.constant.LoadbalancerConstant; +import com.tencent.cloud.polaris.router.PolarisRouterServiceInstanceListSupplier; +import com.tencent.polaris.api.exception.PolarisException; +import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.factory.api.RouterAPIFactory; +import com.tencent.polaris.router.api.core.RouterAPI; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled; +import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; + +/** + * Auto-configuration Ribbon for Polaris. + * + * @author Haotian Zhang + */ +@Configuration() +@EnableConfigurationProperties +@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true) +public class PolarisRouterAutoConfiguration { + + @Bean(name = "polarisRoute") + @ConditionalOnMissingBean + public RouterAPI polarisRouter(SDKContext polarisContext) throws PolarisException { + return RouterAPIFactory.createRouterAPIByContext(polarisContext); + } + + @Configuration(proxyBeanMethods = false) + @ConditionalOnReactiveDiscoveryEnabled + @Order(LoadbalancerConstant.ROUTER_SERVICE_INSTANCE_SUPPLIER_ORDER - 1000) + public static class ReactiveSupportConfiguration { + +// @Bean +// @ConditionalOnBean(ReactiveDiscoveryClient.class) +// @ConditionalOnMissingBean +// @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true) +// public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(RouterAPI routerAPI, +// ConfigurableApplicationContext context) { +// return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withDiscoveryClient() +// .build(context), routerAPI); +// } + + @Bean + @ConditionalOnBean(ReactiveDiscoveryClient.class) + @ConditionalOnMissingBean + @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-preference", matchIfMissing = true) + public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(RouterAPI routerAPI, + ConfigurableApplicationContext context) { + return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withDiscoveryClient() + .withZonePreference() + .build(context), routerAPI); + } + + } + + @Configuration(proxyBeanMethods = false) + @ConditionalOnBlockingDiscoveryEnabled + @Order(LoadbalancerConstant.ROUTER_SERVICE_INSTANCE_SUPPLIER_ORDER + 1 - 1000) + public static class BlockingSupportConfiguration { + +// @Bean +// @ConditionalOnBean(DiscoveryClient.class) +// @ConditionalOnMissingBean +// @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true) +// public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier( +// RouterAPI routerAPI, +// ConfigurableApplicationContext context) { +// return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient() +// .build(context), routerAPI); +// } + + @Bean + @ConditionalOnBean(DiscoveryClient.class) + @ConditionalOnMissingBean + @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-preference", matchIfMissing = true) + public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(RouterAPI routerAPI, + ConfigurableApplicationContext context) { + return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient() + .withZonePreference().build(context), routerAPI); + } + + } + +} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java similarity index 54% rename from spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java rename to spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java index 5c2258efe..a4148458a 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java @@ -17,16 +17,9 @@ package com.tencent.cloud.polaris.router; -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.DynamicServerListLoadBalancer; -import com.netflix.loadbalancer.IPing; -import com.netflix.loadbalancer.IRule; -import com.netflix.loadbalancer.PollingServerListUpdater; -import com.netflix.loadbalancer.Server; -import com.netflix.loadbalancer.ServerList; import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey; import com.tencent.cloud.metadata.context.MetadataContextHolder; -import com.tencent.cloud.polaris.pojo.PolarisServer; +import com.tencent.cloud.polaris.pojo.PolarisServiceInstance; import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultServiceInstances; import com.tencent.polaris.api.pojo.Instance; @@ -36,64 +29,63 @@ import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse; -import org.apache.commons.lang.StringUtils; -import org.springframework.util.CollectionUtils; - import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.apache.commons.lang.StringUtils; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.loadbalancer.Request; +import org.springframework.cloud.loadbalancer.core.DelegatingServiceInstanceListSupplier; +import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; +import org.springframework.util.CollectionUtils; +import reactor.core.publisher.Flux; /** * @author Haotian Zhang */ -public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer { +public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier { private final RouterAPI routerAPI; - public PolarisRoutingLoadBalancer(IClientConfig config, IRule rule, IPing ping, - ServerList serverList, RouterAPI routerAPI) { - super(config, rule, ping, serverList, null, new PollingServerListUpdater()); + public PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, RouterAPI routerAPI) { + super(delegate); this.routerAPI = routerAPI; } @Override - public List getReachableServers() { - List allServers = super.getAllServers(); + public Flux> get() { + return getDelegate().get().map(this::chooseInstances); + } + + @Override + public Flux> get(Request request) { + return super.get(request); + } + + public List chooseInstances(List allServers) { if (CollectionUtils.isEmpty(allServers)) { return allServers; } ServiceInstances serviceInstances = null; - if (allServers.get(0) instanceof PolarisServer) { - serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances(); - } else { - String serviceName; - // notice the difference between different service registries - if (StringUtils.isNotBlank(allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) { - serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery(); - } else { - serviceName = allServers.get(0).getMetaInfo().getAppName(); - } - if (StringUtils.isBlank(serviceName)) { - throw new IllegalStateException( - "PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); - } - ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, serviceName); - List instances = new ArrayList<>(8); - for (Server server : allServers) { - DefaultInstance instance = new DefaultInstance(); - instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE); - instance.setService(serviceName); - instance.setHealthy(server.isAlive()); - instance.setProtocol(server.getScheme()); - instance.setId(server.getId()); - instance.setHost(server.getHost()); - instance.setPort(server.getPort()); - instance.setZone(server.getZone()); - instance.setWeight(100); - instances.add(instance); - } - serviceInstances = new DefaultServiceInstances(serviceKey, instances); + String serviceName = allServers.get(0).getServiceId(); + if (StringUtils.isBlank(serviceName)) { + throw new IllegalStateException( + "PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); + } + ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, serviceName); + List instances = new ArrayList<>(8); + for (ServiceInstance server : allServers) { + DefaultInstance instance = new DefaultInstance(); + instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE); + instance.setService(serviceName); + instance.setProtocol(server.getScheme()); + instance.setId(server.getInstanceId()); + instance.setHost(server.getHost()); + instance.setPort(server.getPort()); + instance.setWeight(100); + instances.add(instance); } + serviceInstances = new DefaultServiceInstances(serviceKey, instances); ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest(); processRoutersRequest.setDstInstances(serviceInstances); String srcNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE); @@ -110,15 +102,10 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer filteredInstances = new ArrayList<>(); + List filteredInstances = new ArrayList<>(); for (Instance instance : filteredServiceInstances.getInstances()) { - filteredInstances.add(new PolarisServer(serviceInstances, instance)); + filteredInstances.add(new PolarisServiceInstance(instance)); } return filteredInstances; } - - @Override - public List getAllServers() { - return getReachableServers(); - } } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java deleted file mode 100644 index 8d33972ae..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.router.config; - -import com.tencent.polaris.api.exception.PolarisException; -import com.tencent.polaris.client.api.SDKContext; -import com.tencent.polaris.factory.api.RouterAPIFactory; -import com.tencent.polaris.router.api.core.RouterAPI; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; -import org.springframework.cloud.netflix.ribbon.RibbonClients; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * Auto-configuration Ribbon for Polaris. - * - * @author Haotian Zhang - */ -@Configuration(proxyBeanMethods = false) -@EnableConfigurationProperties -@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true) -@AutoConfigureAfter(RibbonAutoConfiguration.class) -@RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class) -public class PolarisRibbonAutoConfiguration { - - @Bean - @ConditionalOnMissingBean - public PolarisRibbonProperties polarisRibbonProperties() { - return new PolarisRibbonProperties(); - } - - @Bean(name = "polarisRoute") - @ConditionalOnMissingBean - public RouterAPI polarisRouter(SDKContext polarisContext) throws PolarisException { - return RouterAPIFactory.createRouterAPIByContext(polarisContext); - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java deleted file mode 100644 index 5d0ed05b0..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.router.config; - -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.ILoadBalancer; -import com.netflix.loadbalancer.IPing; -import com.netflix.loadbalancer.IRule; -import com.netflix.loadbalancer.Server; -import com.netflix.loadbalancer.ServerList; -import com.tencent.cloud.polaris.router.PolarisRoutingLoadBalancer; -import com.tencent.cloud.polaris.router.rule.PolarisLoadBalanceRule; -import com.tencent.cloud.polaris.router.rule.PolarisWeightedRandomRule; -import com.tencent.polaris.router.api.core.RouterAPI; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * @author Haotian Zhang - */ -@Configuration -public class PolarisRibbonClientConfiguration { - - @Bean - @ConditionalOnMissingBean - public IRule polarisRibbonRule(PolarisRibbonProperties polarisRibbonProperties) { - switch (PolarisLoadBalanceRule.fromStrategy(polarisRibbonProperties.getPolicy())) { - case WEIGHTED_RANDOM_RULE: - default: - return new PolarisWeightedRandomRule(); - } - } - - @Bean - @ConditionalOnMissingBean - public ILoadBalancer polarisRoutingLoadBalancer(IClientConfig iClientConfig, IRule iRule, IPing iPing, - ServerList serverList, RouterAPI polarisRouter) { - return new PolarisRoutingLoadBalancer(iClientConfig, iRule, iPing, serverList, polarisRouter); - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java deleted file mode 100644 index 6e6ab5014..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.router.config; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * @author Haotian Zhang - */ -@ConfigurationProperties("spring.cloud.polaris.ribbon") -public class PolarisRibbonProperties { - - /** - * 是否开启负载均衡 - */ - @Value("${spring.cloud.polaris.loadbalancer.enabled:#{true}}") - private Boolean loadbalancerEnabled; - - /** - * loadbalnce strategy - */ - @Value("${spring.cloud.polaris.loadbalancer.strategy:#{'weightedRandom'}}") - private String policy; - - public String getPolicy() { - return policy; - } - - public void setPolicy(String policy) { - this.policy = policy; - } - - public Boolean getLoadbalancerEnabled() { - return loadbalancerEnabled; - } - - public void setLoadbalancerEnabled(Boolean loadbalancerEnabled) { - this.loadbalancerEnabled = loadbalancerEnabled; - } - - @Override - public String toString() { - return "PolarisRibbonProperties{" + - "loadbalancerEnabled=" + loadbalancerEnabled + - ", policy='" + policy + '\'' + - '}'; - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisLoadBalanceRule.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisLoadBalanceRule.java deleted file mode 100644 index b39b9e170..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisLoadBalanceRule.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.router.rule; - -import java.util.Arrays; - -/** - * @author Haotian Zhang - */ -public enum PolarisLoadBalanceRule { - - /** - * 加权随机 - */ - WEIGHTED_RANDOM_RULE("weighted_random"); - - /** - * 策略 - */ - String policy; - - PolarisLoadBalanceRule(String strategy) { - this.policy = strategy; - } - - public static PolarisLoadBalanceRule fromStrategy(String strategy) { - return Arrays.stream(values()).filter(t -> t.getPolicy().equals(strategy)).findAny() - .orElse(WEIGHTED_RANDOM_RULE); - } - - /** - * {@link #policy}的getter方法。 - */ - public String getPolicy() { - return policy; - } - -} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java deleted file mode 100644 index d9dffa23a..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.router.rule; - -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.AbstractLoadBalancerRule; -import com.netflix.loadbalancer.Server; -import com.tencent.cloud.polaris.pojo.PolarisServer; -import com.tencent.polaris.api.config.consumer.LoadBalanceConfig; -import com.tencent.polaris.api.pojo.Instance; -import com.tencent.polaris.router.api.core.RouterAPI; -import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest; -import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.util.CollectionUtils; - -import java.util.List; - -/** - * @author Haotian Zhang - */ -public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule { - - private static final String POLICY = LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM; - - @Autowired - private RouterAPI polarisRouter; - - @Override - public void initWithNiwsConfig(IClientConfig clientConfig) { - - } - - @Override - public Server choose(Object key) { - List allServers = getLoadBalancer().getReachableServers(); - if (CollectionUtils.isEmpty(allServers)) { - return null; - } - Server server = allServers.get(0); - if (!(server instanceof PolarisServer)) { - throw new IllegalStateException("PolarisDiscoveryRule only support PolarisServer instances"); - } - PolarisServer polarisServer = (PolarisServer) server; - ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest(); - request.setDstInstances(polarisServer.getServiceInstances()); - request.setLbPolicy(POLICY); - ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter.processLoadBalance(request); - Instance targetInstance = processLoadBalanceResponse.getTargetInstance(); - return new PolarisServer(polarisServer.getServiceInstances(), targetInstance); - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories index 75c191046..8a988ebe7 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories @@ -1,2 +1,2 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.tencent.cloud.polaris.router.config.PolarisRibbonAutoConfiguration + com.tencent.cloud.polaris.router.PolarisRouterAutoConfiguration diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java index b6ec3abe6..c2140bc7d 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java @@ -17,6 +17,7 @@ package com.tencent.cloud.polaris.router.config; +import com.tencent.cloud.polaris.router.PolarisRouterAutoConfiguration; import com.tencent.polaris.router.api.core.RouterAPI; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -36,19 +37,11 @@ public class PolarisRibbonAutoConfigurationTest { private ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of( PolarisRibbonTest.class, - PolarisRibbonAutoConfiguration.class)) + PolarisRouterAutoConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081"); - @Test - public void testDefaultInitialization() { - this.contextRunner.run(context -> { - assertThat(context).hasSingleBean(RouterAPI.class); - assertThat(context).hasSingleBean(PolarisRibbonProperties.class); - }); - } - @Configuration @EnableAutoConfiguration static class PolarisRibbonTest { diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-commons/pom.xml b/spring-cloud-tencent-starters/spring-cloud-tencent-commons/pom.xml index 66d33281f..f880f91a7 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-commons/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-commons/pom.xml @@ -42,15 +42,11 @@ spring-cloud-commons - - com.netflix.ribbon - ribbon-loadbalancer - - - - com.google.guava - guava - + + + + + commons-collections diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java b/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/constant/LoadbalancerConstant.java similarity index 58% rename from spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java rename to spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/constant/LoadbalancerConstant.java index b748b986e..df5998908 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/constant/LoadbalancerConstant.java @@ -15,21 +15,20 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.gateway.example.zuul; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +package com.tencent.cloud.constant; /** - * @author Haotian Zhang + * @author liaochuntao */ -@SpringBootApplication -@EnableZuulProxy -public class GatewayZuulApplication { +public final class LoadbalancerConstant { + + private LoadbalancerConstant() {} + + public static final int DISCOVERY_SERVICE_INSTANCE_SUPPLIER_ORDER = 193827465 - 1000000; - public static void main(String[] args) { - SpringApplication.run(GatewayZuulApplication.class, args); - } + /** + * We need to ensure that the routing action is performed before the load balancing action can be performed + */ + public static final int ROUTER_SERVICE_INSTANCE_SUPPLIER_ORDER = DISCOVERY_SERVICE_INSTANCE_SUPPLIER_ORDER - 1000000; } diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/polaris/pojo/PolarisServer.java b/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/polaris/pojo/PolarisServer.java deleted file mode 100644 index eeb8c74b7..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/polaris/pojo/PolarisServer.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.pojo; - -import com.google.common.base.Objects; -import com.netflix.loadbalancer.Server; -import com.tencent.polaris.api.pojo.Instance; -import com.tencent.polaris.api.pojo.ServiceInstances; -import org.apache.commons.lang.StringUtils; - -import java.util.Map; - -/** - * Polaris implementation of {@link Server} - * - * @author Haotian Zhang - */ -public class PolarisServer extends Server { - - private final ServiceInstances serviceInstances; - - private final Instance instance; - - private final MetaInfo metaInfo; - - public PolarisServer(ServiceInstances serviceInstances, Instance instance) { - super(instance.getHost(), instance.getPort()); - if (StringUtils.equalsIgnoreCase(instance.getProtocol(), "https")) { - setSchemea("https"); - } else { - setSchemea("http"); - } - this.serviceInstances = serviceInstances; - this.instance = instance; - this.metaInfo = new MetaInfo() { - @Override - public String getAppName() { - return instance.getService(); - } - - @Override - public String getServerGroup() { - return null; - } - - @Override - public String getServiceIdForDiscovery() { - return instance.getService(); - } - - @Override - public String getInstanceId() { - return instance.getId(); - } - }; - } - - public Instance getInstance() { - return instance; - } - - @Override - public MetaInfo getMetaInfo() { - return metaInfo; - } - - public Map getMetadata() { - return instance.getMetadata(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - if (!super.equals(o)) { - return false; - } - PolarisServer that = (PolarisServer) o; - return Objects.equal(instance, that.instance); - } - - @Override - public int hashCode() { - return Objects.hashCode(super.hashCode(), instance); - } - - public ServiceInstances getServiceInstances() { - return serviceInstances; - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/polaris/pojo/PolarisServiceInstance.java b/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/polaris/pojo/PolarisServiceInstance.java index 056b063fe..609491b5d 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/polaris/pojo/PolarisServiceInstance.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/polaris/pojo/PolarisServiceInstance.java @@ -48,6 +48,10 @@ public class PolarisServiceInstance implements ServiceInstance { } } + public Instance getPolarisInstance() { + return instance; + } + @Override public String getInstanceId() { return ServiceInstance.super.getInstanceId(); diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeign.java b/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeign.java index d350f3be6..06a8adf65 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeign.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeign.java @@ -22,13 +22,16 @@ import feign.Contract; import feign.Feign; import feign.InvocationHandlerFactory; import feign.Target; -import feign.hystrix.FallbackFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.cloud.openfeign.FeignClientFactoryBean; import org.springframework.cloud.openfeign.FeignContext; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.context.support.GenericApplicationContext; import org.springframework.util.StringUtils; import java.lang.reflect.InvocationHandler; @@ -85,7 +88,10 @@ public class PluggableFeign { super.invocationHandlerFactory(new InvocationHandlerFactory() { @Override public InvocationHandler create(Target target, Map dispatch) { - Object feignClientFactoryBean = applicationContext.getBean("&" + target.type().getName()); + GenericApplicationContext gctx = (GenericApplicationContext) Builder.this.applicationContext; + BeanDefinition def = gctx.getBeanDefinition(target.type().getName()); + + FeignClientFactoryBean feignClientFactoryBean = (FeignClientFactoryBean) def.getAttribute("feignClientsRegistrarFactoryBean"); Class fallback = (Class) ReflectionUtils.getFieldValue(feignClientFactoryBean, "fallback"); Class fallbackFactory = (Class) ReflectionUtils.getFieldValue(feignClientFactoryBean, @@ -104,8 +110,7 @@ public class PluggableFeign { if (void.class != fallback) { fallbackInstance = getFallbackInstanceFromContext(beanName, "fallback", fallback, target.type()); - return new PluggableFeignInvocationHandler(target, dispatch, - new FallbackFactory.Default<>(fallbackInstance), pluggableFeignPlugins); + return new PluggableFeignInvocationHandler(target, dispatch, new FallbackFactory.Default(fallbackInstance), pluggableFeignPlugins); } if (void.class != fallbackFactory) { diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignContext.java b/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignContext.java index 574846006..2d4593630 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignContext.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignContext.java @@ -20,7 +20,7 @@ package com.tencent.cloud.feign; import feign.FeignException; import feign.InvocationHandlerFactory; import feign.Target; -import feign.hystrix.FallbackFactory; +import org.springframework.cloud.openfeign.FallbackFactory; import java.lang.reflect.Method; import java.util.Map; @@ -36,7 +36,7 @@ public class PluggableFeignContext { private Map dispatch; - private FallbackFactory fallbackFactory; + private FallbackFactory fallbackFactory; private Map fallbackMethodMap; @@ -66,11 +66,11 @@ public class PluggableFeignContext { this.dispatch = dispatch; } - public FallbackFactory getFallbackFactory() { + public FallbackFactory getFallbackFactory() { return fallbackFactory; } - public void setFallbackFactory(FallbackFactory fallbackFactory) { + public void setFallbackFactory(FallbackFactory fallbackFactory) { this.fallbackFactory = fallbackFactory; } diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignInvocationHandler.java b/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignInvocationHandler.java index f05d4a4fb..20adde80b 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignInvocationHandler.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-feign/src/main/java/com/tencent/cloud/feign/PluggableFeignInvocationHandler.java @@ -20,9 +20,9 @@ package com.tencent.cloud.feign; import feign.FeignException; import feign.InvocationHandlerFactory; import feign.Target; -import feign.hystrix.FallbackFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.util.CollectionUtils; import java.lang.reflect.InvocationHandler; @@ -48,7 +48,7 @@ public class PluggableFeignInvocationHandler implements InvocationHandler { private final Map dispatch; - private FallbackFactory fallbackFactory; + private FallbackFactory fallbackFactory; private Map fallbackMethodMap; @@ -59,7 +59,7 @@ public class PluggableFeignInvocationHandler implements InvocationHandler { private List exceptionPluggableFeignPlugins; PluggableFeignInvocationHandler(Target target, Map dispatch, - FallbackFactory fallbackFactory, List pluggableFeignPlugins) { + FallbackFactory fallbackFactory, List pluggableFeignPlugins) { this.target = checkNotNull(target, "target"); this.dispatch = checkNotNull(dispatch, "dispatch"); this.fallbackFactory = fallbackFactory; @@ -110,7 +110,9 @@ public class PluggableFeignInvocationHandler implements InvocationHandler { prePlugin.run(context); } - result = this.dispatch.get(method).invoke(args); + InvocationHandlerFactory.MethodHandler handler = this.dispatch.get(method); + + result = handler.invoke(args); context.setResult(result); diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/pom.xml b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/pom.xml index 1dd81df5c..2775d406e 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/pom.xml @@ -44,12 +44,6 @@ test - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - test - - org.powermock diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml index 261bd215c..888cd02b7 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml @@ -30,13 +30,6 @@ spring-cloud-starter-tencent-polaris-ratelimit - - - org.springframework.cloud - spring-cloud-starter-netflix-zuul - true - - org.springframework.cloud spring-cloud-starter-gateway diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java index cf8c17430..e548680e2 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java @@ -17,14 +17,9 @@ package com.tencent.cloud.polaris.gateway.config; -import com.netflix.zuul.ZuulFilter; -import com.netflix.zuul.http.ZuulServlet; import com.tencent.cloud.polaris.gateway.core.scg.filter.Metadata2HeaderScgFilter; import com.tencent.cloud.polaris.gateway.core.scg.filter.MetadataFirstScgFilter; import com.tencent.cloud.polaris.gateway.core.scg.filter.RateLimitScgFilter; -import com.tencent.cloud.polaris.gateway.core.zuul.filter.Metadata2HeaderZuulFilter; -import com.tencent.cloud.polaris.gateway.core.zuul.filter.MetadataFirstZuulFilter; -import com.tencent.cloud.polaris.gateway.core.zuul.filter.RateLimitZuulFilter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Bean; @@ -38,25 +33,7 @@ import org.springframework.context.annotation.Configuration; @Configuration public class PolarisGatewayAutoConfiguration { - @Configuration(proxyBeanMethods = false) - @ConditionalOnClass(ZuulServlet.class) - static class PolarisGatewayZuulAutoConfiguration { - @Bean - public ZuulFilter metadataFirstZuulFilter() { - return new MetadataFirstZuulFilter(); - } - - @Bean - public ZuulFilter rateLimitZuulFilter() { - return new RateLimitZuulFilter(); - } - @Bean - public ZuulFilter metadata2HeaderZuulFilter() { - return new Metadata2HeaderZuulFilter(); - } - } - - @Configuration(proxyBeanMethods = false) + @Configuration() @ConditionalOnClass(GlobalFilter.class) static class PolarisGatewayScgAutoConfiguration { @Bean diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/Metadata2HeaderScgFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/Metadata2HeaderScgFilter.java index 7ac146bc7..67739b7d4 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/Metadata2HeaderScgFilter.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/Metadata2HeaderScgFilter.java @@ -31,7 +31,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Map; -import static org.springframework.cloud.gateway.filter.LoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER; +import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER; /** * Scg filter used for writing metadata in HTTP request header. diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/Metadata2HeaderZuulFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/Metadata2HeaderZuulFilter.java deleted file mode 100644 index 4b743f070..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/Metadata2HeaderZuulFilter.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package com.tencent.cloud.polaris.gateway.core.zuul.filter; - -import com.netflix.zuul.ZuulFilter; -import com.netflix.zuul.context.RequestContext; -import com.tencent.cloud.metadata.constant.MetadataConstant; -import com.tencent.cloud.metadata.context.MetadataContext; -import com.tencent.cloud.metadata.context.MetadataContextHolder; -import com.tencent.cloud.metadata.util.JacksonUtils; -import org.springframework.util.CollectionUtils; - - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.Map; - -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.RIBBON_ROUTING_FILTER_ORDER; -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE; - -/** - * Zuul filter used for writing metadata in HTTP request header. - * - * @author skyehtzhang - */ -public class Metadata2HeaderZuulFilter extends ZuulFilter { - - @Override - public String filterType() { - return ROUTE_TYPE; - } - - @Override - public int filterOrder() { - return RIBBON_ROUTING_FILTER_ORDER - 1; - } - - @Override - public boolean shouldFilter() { - return true; - } - - @Override - public Object run() { - // get request context - RequestContext requestContext = RequestContext.getCurrentContext(); - - // get metadata of current thread - MetadataContext metadataContext = MetadataContextHolder.get(); - - // add new metadata and cover old - Map customMetadata = metadataContext.getAllTransitiveCustomMetadata(); - if (!CollectionUtils.isEmpty(customMetadata)) { - String metadataStr = JacksonUtils.serialize2Json(customMetadata); - try { - requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, - URLEncoder.encode(metadataStr, "UTF-8")); - } catch (UnsupportedEncodingException e) { - requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr); - } - } - return null; - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataFirstZuulFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataFirstZuulFilter.java deleted file mode 100644 index 2d158d0e5..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataFirstZuulFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package com.tencent.cloud.polaris.gateway.core.zuul.filter; - -import com.netflix.zuul.ZuulFilter; -import com.netflix.zuul.context.RequestContext; -import com.tencent.cloud.metadata.constant.MetadataConstant; -import com.tencent.cloud.metadata.context.MetadataContextHolder; - -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER; -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE; -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.REQUEST_URI_KEY; -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVICE_ID_KEY; - -/** - * Zuul filter used for setting peer info in context. - * - * @author Haotian Zhang - */ -public class MetadataFirstZuulFilter extends ZuulFilter { - @Override - public String filterType() { - return PRE_TYPE; - } - - @Override - public int filterOrder() { - return PRE_DECORATION_FILTER_ORDER + 1; - } - - @Override - public boolean shouldFilter() { - return true; - } - - @Override - public Object run() { - // get request context - RequestContext requestContext = RequestContext.getCurrentContext(); - - // TODO 对端命名空间暂时与本地命名空间相同 - MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE, - MetadataContextHolder.get().getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE)); - MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE, - (String) requestContext.get(SERVICE_ID_KEY)); - MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH, - (String) requestContext.get(REQUEST_URI_KEY)); - return null; - } -} diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/RateLimitZuulFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/RateLimitZuulFilter.java deleted file mode 100644 index 89cf7896d..000000000 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/RateLimitZuulFilter.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package com.tencent.cloud.polaris.gateway.core.zuul.filter; - -import com.netflix.zuul.ZuulFilter; -import com.netflix.zuul.context.RequestContext; -import com.tencent.cloud.metadata.constant.MetadataConstant; -import com.tencent.cloud.metadata.context.MetadataContextHolder; -import com.tencent.cloud.polaris.ratelimit.utils.Consts; -import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils; -import com.tencent.polaris.ratelimit.api.core.LimitAPI; -import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse; -import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.HashMap; -import java.util.Map; - -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE; -import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.RIBBON_ROUTING_FILTER_ORDER; -import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; - -/** - * Zuul filter used for rate limit. - * - * @author Haotian Zhang - */ -public class RateLimitZuulFilter extends ZuulFilter { - - private static final Logger LOG = LoggerFactory.getLogger(RateLimitZuulFilter.class); - - @Autowired(required = false) - private LimitAPI limitAPI; - - @Override - public String filterType() { - return PRE_TYPE; - } - - @Override - public int filterOrder() { - return RIBBON_ROUTING_FILTER_ORDER - 1; - } - - @Override - public boolean shouldFilter() { - return limitAPI != null; - } - - @Override - public Object run() { - // get request context - RequestContext requestContext = RequestContext.getCurrentContext(); - - String peerNamespace = - MetadataContextHolder.get().getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE); - String peerService = - MetadataContextHolder.get().getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE); - String peerPath = MetadataContextHolder.get().getSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH); - Map labels = null; - if (StringUtils.isNotBlank(peerPath)) { - labels = new HashMap<>(); - labels.put("method", peerPath); - } - - try { - QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI, peerNamespace, peerService, 1, labels, - null); - if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) { - requestContext.setSendZuulResponse(false); - requestContext.setResponseStatusCode(TOO_MANY_REQUESTS.value()); - requestContext.getResponse().getWriter().write(Consts.QUOTA_LIMITED_INFO + quotaResponse.getInfo()); - } - } catch (Throwable throwable) { - //限流API调用出现异常,不应该影响业务流程的调用 - LOG.error("fail to rate limit with QuotaRequest[{}-{}-{}].", peerNamespace, peerService, peerPath, - throwable); - } - - return null; - } -}