什么是Ribbon
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients.
????上述是官方文檔對Ribbon的解釋,意思是Ribbon是一個客戶端負(fù)載均衡器匙赞,它可以有效的控制HTTP和TCP客戶端的訪問会宪。上述一段話有個很關(guān)鍵的詞“客戶端負(fù)載均衡器”,我們常用的負(fù)載均衡模式就是nginx反向代理模式,這種模式我們是不知道服務(wù)究竟請求的是那臺機(jī)器的揩懒。這種負(fù)載均衡模式是服務(wù)端負(fù)載均衡。
而客戶端負(fù)載均衡就是在發(fā)起請求是有明確的目標(biāo)被辑,請求精確指向集群中的某臺機(jī)器燎悍。
</br>????Ribbon的負(fù)載均衡默認(rèn)是依靠Eureka來實(shí)現(xiàn)的,Ribbon通過Eureka客戶端定期拉取注冊服務(wù)盼理,通過客戶端的輪訓(xùn)谈山、隨機(jī)等算法來實(shí)現(xiàn)負(fù)載均衡。
快速開始
這個案例依賴Eureka-Server,請準(zhǔn)備好server服務(wù)宏怔。Eureka相關(guān)查看Spring-Cloud系列(一) Eureka
添加依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
????看上面的依賴有些同學(xué)可能會奇怪,為什么沒有引入Ribbon依賴奏路,只引入了Eureka客戶端。下面我們來看一下Eureka客戶端的依賴關(guān)系圖就明白了举哟。
????從上圖中我們明顯可以看出來,Eureka客戶端的依賴是包含Ribbon依賴的思劳。所以我們不需要添加依賴。
代碼
初始化bean
/**
* @LoadBalanced Ribbon整合注解妨猩,Rebbon本身依賴在EurekClient中存在潜叛。所以不需要繼續(xù)引入
* Ribbon是客戶端負(fù)載均衡實(shí)現(xiàn)之一,具有輪訓(xùn),隨機(jī)等等多種算法壶硅。
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
使用方式
@RestController
public class RibbonControlller {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/url")
public String postUser() {
return this.restTemplate.getForObject("http://eureka-client-one/url", String.class);
}
}
application配置
server:
</br>??port: 8082
</br>spring:
</br>??application:
</br>????name: ribbon-one
</br>eureka:
</br>??instance:
</br>????prefer-ip-address: true
</br>????instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
</br>??client:
</br>????serviceUrl:
</br>??????defaultZone: http://user:Pass123456@localhost:8761/eureka
啟動效果
自定義RibbonClient
代碼自定義
定義配置
@Configuration
public class RibbonConfiguration {
@Autowired
private IClientConfig config;
@Bean
public IRule ribbonRule(IClientConfig config){
return new RandomRule();
}
}
The FooConfiguration has to be @Configuration but take care that it is not in a @ComponentScan for the main application context, otherwise it will be shared by all the @RibbonClients. If you use @ComponentScan (or @SpringBootApplication) you need to take steps to avoid it being included (for instance put it in a separate, non-overlapping package, or specify the packages to scan explicitly in the @ComponentScan).
注意Configuration類不能和主類平級威兜,也不能在主類的子包下,否則該配置會覆蓋本項(xiàng)目下所有的Ribben配置
配置注入
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "eureka-client-one",configuration = RibbonConfiguration.class)
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
使用方式
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
ServiceInstance instance = this.loadBalancerClient.choose("eureka-client-one");
return instance.toString();
}
可被重寫的其他配置
????上面的代碼配置我們改變了IRule配置,更多例子參考官方文檔庐椒。以下配置可以通過代碼修改:
- IClientConfig ribbonClientConfig: DefaultClientConfigImpl
- IRule ribbonRule: ZoneAvoidanceRule
- IPing ribbonPing: NoOpPing
- ServerList<Server> ribbonServerList: ConfigurationBasedServerList
- ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter
- ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
- ServerListUpdater ribbonServerListUpdater: PollingServerListUpdater
配置文件自定義
配置文件
users:#即需要調(diào)用的應(yīng)用名
</br>??ribbon:
</br>????NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
使用方式
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
ServiceInstance instance = this.loadBalancerClient.choose("eureka-client-one");
return instance.toString();
}
注意椒舵!
</br>Ribbon的配置優(yōu)先級順序是 yml>javaConfig>default
其他配置選項(xiàng)
上面的配置文件我們改變IRule配置,更多例子參考官方文檔约谈。以下配置可以通過配置文件修改
- NFLoadBalancerClassName: should implement ILoadBalancer
- NFLoadBalancerRuleClassName: should implement IRule
- NFLoadBalancerPingClassName: should implement IPing
- NIWSServerListClassName: should implement ServerList
- NIWSServerListFilterClassName should implement ServerListFilter
Ribbon的其他配置
Ribbon禁用Eureka
ribbon:
</br>??eureka:
</br>????enabled: false
</br>users:#即需要調(diào)用的應(yīng)用名
</br>??ribbon:
</br>????listOfServers: localhost:8080,localhost:8081 #服務(wù)IP List
延遲加載Ribbon
ribbon:
</br>??eager-load:
</br>????enabled: true
</br>????clients: client1, client2, client3
????本文參考Spring-Cloud-Ribbon笔宿,Ribbon延遲加載