spring-cloud-ribbon負載均衡


Ribbon

Ribbon是一個基於HTTP和TCP客戶端的負載均衡器。Feign中也使用Ribbon,后續會介紹Feign的使用。

Ribbon可以在通過客戶端中配置的ribbonServerList服務端列表去輪詢訪問以達到均衡負載的作用。

當Ribbon與Eureka聯合使用時,ribbonServerList會被DiscoveryEnabledNIWSServerList重寫,擴展成從Eureka注冊中心中獲取服務端列表。同時它也會用NIWSDiscoveryPing來取代IPing,它將職責委托給Eureka來確定服務端是否已經啟動。

下面我們通過實例看看如何使用Ribbon來調用服務,並實現客戶端的均衡負載。

准備工作

  • 啟動Chapter-9-1-1中的服務注冊中心:eureka-server
  • 啟動Chapter-9-1-1中的服務提供方:compute-service
  • 修改compute-service中的server-port為2223,再啟動一個服務提供方:compute-service

此時訪問:http://localhost:1111/

altalt

可以看到COMPUTE-SERVICE服務有兩個單元正在運行:

  • 192.168.21.101:compute-service:2222
  • 192.168.21.101:compute-service:2223

使用Ribbon實現客戶端負載均衡的消費者

構建一個基本Spring Boot項目,並在pom.xml中加入如下內容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
 
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</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-web</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>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在應用主類中,通過@EnableDiscoveryClient注解來添加發現服務能力。創建RestTemplate實例,並通過@LoadBalanced注解開啟均衡負載能力。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
 
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
 
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
 
}

創建ConsumerController來消費COMPUTE-SERVICE的add服務。通過直接RestTemplate來調用服務,計算10 + 20的值。

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class ConsumerController {
 
@Autowired
RestTemplate restTemplate;
 
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(@RequestParam("param") String a) {
return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a="+a, String.class).getBody();
}
 
}

q將傳來的參數a,拼接到鏈接上,負載均衡到兩個COMPUTE-SERVICE上,“COMPUTE-SERVICE”是eureka中的注冊結點名

application.properties中配置eureka服務注冊中心

1
2
3
4
5
 
spring.application.name=ribbon-consumer
server.port=3333
 
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

啟動該應用,並訪問兩次:http://localhost:3333/add?a=111

然后,打開compute-service的兩個服務提供方,分別輸出了類似下面的日志內容:

  • 端口為2222服務提供端的日志:
1
2016-06-02 11:16:26.787 INFO 90014 --- [io-2222-exec-10] com.didispace.web.ComputeController : /add, host:192.168.21.101, service_id:compute-service, result:30
  • 端口為2223服務提供端的日志:
1
2016-06-02 11:19:41.241 INFO 90122 --- [nio-2223-exec-1] com.didispace.web.ComputeController : /add, host:192.168.21.101, service_id:compute-service, result:30

可以看到,之前啟動的兩個compute-service服務端分別被調用了一次。到這里,我們已經通過Ribbon在客戶端已經實現了對服務調用的均衡負載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM