您的位置:首页 > 编程语言 > Java开发

spring-cloud-ribbon负载均衡

2017-06-11 13:58 811 查看

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/



alt

可以看到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在客户端已经实现了对服务调用的均衡负载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: