Spring Cloud 入门教程(05):Ribbon实现客户端的负载均衡

接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群。

很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改为8763。然后启动这两个Spring Boot应用, 就可以得到两个Hello World服务。这两个Hello world都注册到了eureka服务中心。这时候再访问 http://localhost:8761 ,可以看到两个hello world服务已经注册。(服务与注册参见 Spring Cloud 入门教程(01):服务注册)。

enter description here

1. 客户端的负载均衡

负载均衡可分为服务端负载均衡和客户端负载均衡,服务端负载均衡完全由服务器处理,客户端不需要做任何事情。而客户端负载均衡技术,客户端需要维护一组服务器引用,每次客户端向服务端发请求的时候,会根据算法主动选中一个服务节点。常用的负载均衡算法有: Round Robbin, Random,Hash,StaticWeighted等。

Spring 提供两辆种服务调度方式:Ribbon+restful和Feign。Ribbon就是一个基于客户端的负载均衡器, Ribbon提供了很多在HTTP和TCP客户端之上的控制.

Feign内部也已经使用了Ribbon, 所以只要使用了@FeignClient注解,那么这一章的内容也都是适用的。

下面就看看如何Spring Cloud如何用Ribbon来实现两个Hello World服务的负载均衡。以下是Spring cloud的ribbon客户端负载均衡架构图。

enter description here

hello world服务和ribbon均注册到服务中心

service-hi工程跑了两个副本,端口分别为8762,8763,分别向服务注册中心注册, 当sercvice-ribbon通过restTemplate调用service-Hellowworld的接口时,利用用ribbon进行负载均衡,会轮流的调用处于两个不同端口的Hello world服务

2. 创建一个Ribbon服务

1) 创建一个maven工程,取名叫service-ribbon, 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.cwalzy</groupId>
<artifactId>springcloud.helloworld.ribbon.service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud.helloworld.ribbon.service</name>
<description>Demo project for Spring Cloud Ribbon</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</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>

</project>

2). 创建主类ServiceRibbonApplication

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
40
41
package org.cwalzy.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

@SpringBootApplication
@EnableDiscoveryClient//@EnableDiscoveryClient向服务中心注册
@EnableCircuitBreaker
@EnableHystrixDashboard
public class ServiceRibbonApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}

@Bean//注册了一个叫restTemplate的bean
@LoadBalanced//@ LoadBalanced注册表明,这个restRemplate是需要做负载均衡的。 利用用ribbon进行负载均衡,会轮流的调用处于两个不同端口的Hello world服务
RestTemplate restTemplate() {
return new RestTemplate();
}

//https://www.cnblogs.com/mark7/p/8920288.html
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}

@EnableDiscoveryClient向服务中心注册,并且注册了一个叫restTemplate的bean。

@ LoadBalanced注册表明,这个restRemplate是需要做负载均衡的。

3). 创建获取一个获取Hello内容的service类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.cwalzy.ribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {
@Autowired RestTemplate restTemplate;
public String getHelloContent() {
//这里关键代码就是, restTemplate.getForObject方法会通过ribbon负载均衡机制, 自动选择一个Hello word服务,
return restTemplate.getForObject("http://SERVICE-HELLOWORLD/",String.class);
}
}

这里关键代码就是, restTemplate.getForObject方法会通过ribbon负载均衡机制, 自动选择一个Hello word服务,

这里的URL是 “http://SERVICE-HELLOWORLD/" ,其中的SERVICE-HELLOWORLD是Hello world服务的名字,而注册到服务中心的有两个SERVICE-HELLOWORLD。 所以,这个调用本质是ribbon-service作为客户端根据负载均衡算法自主选择了一个作为服务端的SERVICE-HELLOWORLD服务。然后再访问选中的SERVICE-HELLOWORLD来执行真正的Hello world调用。

4). 创建HelloController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package org.cwalzy.ribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
@Autowired HelloService helloService;

@RequestMapping("/")
public String home() {
return helloService.getHelloContent();
}
}

启动ribbon-service应用 我们就可以访问 http://localhost:8901/ , 然后每次刷新可以看到以下两种结果交替出现,表明实际调用的是在不同端口的不同的SERVICE-HELLOWORLD。
enter description here
enter description here

本文转载自 https://www.cnblogs.com/chry/p/7263281.html
原文基于 spring boot 1.5.3.RELEASE 版本
而本文中有一些改动 是基于 spring boot 2.1.5.RELEASE 版本