Spring Cloud学习02--Ribbon基本使用

Ribbon功能:提供云端负载均衡,有多种负载均衡策略可供选择,可配合服务发现和断路器使用。

Ribbon是一种客户端负载均衡工具,而Dubbo是服务端负载均衡工具。

上文的项目基础上,继续添加Ribbon的功能。

1.启动两个search模块:

在03-search项目的项目启动下拉框中,选择edit功能:

Spring Cloud学习02--Ribbon基本使用

 

选中03-search项目,点击上方的复制按钮,复制一个启动项:

Spring Cloud学习02--Ribbon基本使用

 

修改新复制的search项目的启动项,名称修改为SearchApplication 8082,添加启动参数:-Dserver.port=8082

Spring Cloud学习02--Ribbon基本使用

 

修改好之后,启动这两个search项目,通过浏览器,分别访问:http://localhost:8081/search与http://localhost:8082:search

均可正常访问,则说明此启动项复制和配置是正确的。

 

2.修改02-customer项目:

在customer项目的pom文件中,添加Ribbon的引用:

 1  <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.springframework.cloud</groupId>
 8             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 9         </dependency>
10         <dependency>
11             <groupId>org.springframework.boot</groupId>
12             <artifactId>spring-boot-starter-web</artifactId>
13         </dependency>
14     </dependencies>

 

修改启动类,为RestTemplate对象添加@LoadBalanced注解:

 1 package com.yangasen;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.web.client.RestTemplate;
 9 
10 
11 @SpringBootApplication
12 @EnableEurekaClient
13 public class CustomerApplication {
14     public static void main(String[] args) {
15         SpringApplication.run(CustomerApplication.class,args);
16     }
17     @Bean
18     @LoadBalanced
19     public RestTemplate restTemplate(){
20         return new RestTemplate();
21     }
22 }

 

修改Controller代码:

 1 package com.yangasen.controller;
 2 
 3 import com.netflix.appinfo.InstanceInfo;
 4 import com.netflix.discovery.EurekaClient;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.GetMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 import org.springframework.web.client.RestTemplate;
 9 
10 @RestController
11 public class CustomerController {
12 
13     @Autowired
14     private RestTemplate restTemplate;
15 
16     @Autowired
17     private EurekaClient eurekaClient;
18 
19     @GetMapping("/customer")
20     public String customer(){
21         String result = restTemplate.getForObject("http://SEARCH/search",String.class);
22         return result;
23     }
24 }

 

3.测试效果:

启动eureka服务和customer,在浏览器中访问:http://localhost:8080/customer

显示如下效果:

Spring Cloud学习02--Ribbon基本使用

 

再次刷新此页面,访问端口发生变化:

Spring Cloud学习02--Ribbon基本使用

Robbin的默认配置,是采用轮询的方式进行负载均衡。

 

4.负载均衡策略有:

RoundRobbinRule  轮询策略

RandomRule  随机策略

WeightedResponseTimeRule  默认采用轮询,后续会根据服务器响应时间来分配

BestAvailableRule  被分配房并发数最小策略

 

可以实现IRule接口的配置类或者配置文件两种方式,实现负载均衡策略的配置。

 

配置文件方式示例如下:

 1 eureka:
 2   client:
 3     serviceUrl:
 4       defaultZone: http://root:root@localhost:8761/eureka,http://root:root@localhost:8762/eureka
 5 
 6 SEARCH:
 7   ribbon:
 8     NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
 9 
10 spring:
11   application:
12     name: CUSTOMER

 

上一篇:Android主题切换方案总结


下一篇:springcloud入门resttemplate&eureka&ribbon&hystrix