Netflix Ribbon 整合Eureka
- 激活服務發現的用戶端
@EnableDiscoveryClient
package com.example.springcloudlesson6;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@RibbonClients({
@RibbonClient(name = "spring-cloud-service-provider")
})
@EnableDiscoveryClient
public class SpringCloudLesson6Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudLesson6Application.class, args);
}
//聲明RestTemplate
@LoadBalanced//RestTemplate的行為變化
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
建立并且啟動Eureka Server
以
spring-cloud-lesson-6-eureka-server
為例
1.激活
@EnableEurekaServer
package com.example.springcloudlesson6eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudLesson6EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudLesson6EurekaServerApplication.class, args);
}
}
2.配置Eureka伺服器,在
application.properties
##定義應用名稱
spring.application.name=spring-cloud-eureka-server
##配置端口
server.port=10000
##取消向注冊中心注冊
eureka.client.register-with-eureka=false
##取消向注冊中心擷取注冊資訊,執行個體資訊
eureka.client.fetch-registry=false
##解決Peer/叢集連接配接問題
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
3.啟動Eureka Server
調整Ribbon用戶端
1.調整
application.properties
配置檔案
##服務提供方
spring.application.name=spring-cloud-ribbon-client
##服務端口
server.port=8080
##關閉注冊
#eureka.client.enabled=false
##連接配接 Eureka Server url
eureka.client.service-url.defaultZone=http://localhost:10000/eureka/
##服務提供方主機
service-provider.host=localhost
##提供方端口
service-provider.port=9090
service-provider.name=spring-cloud-service-provider
###配置服務提供方的ribbon
spring-cloud-service-provider.ribbon.listOfServers=\
http://${service-provider.host}:${service-provider.port}
服務提供方調整,并且連接配接 Eureka Server
1.修改
application.properties
##服務提供方
spring.application.name=spring-cloud-service-provider
##服務端口
server.port=9090
##關閉注冊
#eureka.client.enabled=false
##連接配接Eureka Server
eureka.client.service-url.defaultZone=http://localhost:10000/eureka/
在啟動兩台 服務提供方執行個體
--server=9091
--server=9092

負載均衡示意圖
程式界面示意圖
通過重新整理重連後的結論為,三個伺服器的權重是一緻的,是以輪詢的方式進行連接配接
Netflix Ribbon核心接口
-
實際請求用戶端
LoadBalancerClient
RibbonLoadBalancerClient
-
負載均衡上下文
LoadBalancerContext
RibbonLoadBalancerContext
負載均衡器
ILoadBalancer
- BaseLoadBalancer
- DynamicServerListLoadBalancer
- ZoneAwareLoadBalancer
- NoOploadBalancer
規則接口
IRule
- 随機規則:RandomRule
- 最可用原則:BestAvailableRule
- 輪訓規則:RoundRobinRule (預設情況)
- 重試實作:RetryRule
- 用戶端配置:ClientConfigEnabledRoundRobinRule
- 可用性過濾規則:AvailabilityFilteringRule
- RT權重規則:WeightedResponseTimeRule
- 規避區域規則: ZoneAvoidanceRule
PING政策
IPingStrategy
- NoOpPing (預設情況)
- DummyPing
- PingConstant
- pingUrl
- NIWSDiscoveryPing