文章目录
-
前言
-
@LoadBalanced
-
LoadBalancerAutoConfiguration
* LoadBalancerInterceptor拦截器
* RibbonLoadBalancerClient -
小结
前言
上节我们主要介绍了RestTemplate几种常见的REST请求:Spring Cloud Ribbon客户端负载均衡(一)
本文将通过源码分析来了解Ribbon实现客户端负载均衡的基本原理,从@LoadBalanced注解入手展开
@LoadBalanced
1
2
3
4
5
6
7
8
9
10
11
12
13 1/**
2 * Annotation to mark a RestTemplate bean to be configured to use a LoadBalancerClient
3 * @author Spencer Gibb
4 */
5@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
6@Retention(RetentionPolicy.RUNTIME)
7@Documented
8@Inherited
9@Qualifier
10public @interface LoadBalanced {
11}
12
13
从@LoadBalanced注解我们可以发现,该注解用来给RestTemplate做标记,以使用负载均衡的客户端(LoadBalancerClient)来配置它。
通过搜索LoadBalancerClient可以发现,这是一个继承ServiceInstanceChooser 接口的接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1public interface LoadBalancerClient extends ServiceInstanceChooser {
2 <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException;
3
4 <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException;
5
6 URI reconstructURI(ServiceInstance instance, URI original);
7}
8
9public interface ServiceInstanceChooser {
10
11 /**
12 * Choose a ServiceInstance from the LoadBalancer for the specified service
13 * @param serviceId the service id to look up the LoadBalancer
14 * @return a ServiceInstance that matches the serviceId
15 */
16 ServiceInstance choose(String serviceId);
17}
18
19
从上面两个接口中,我们可以通过定义的抽象方法来了解客户端负载均衡器中应具备的几种能力。
- ServiceInstance choose(String serviceId)
根据传入的服务名serviceId,从负载均衡器中选择对应服务的实例
- T execute(String serviceId, LoadBalancerRequest request)
使用从负载均衡器中挑选出的服务实例来执行请求内容
- T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest request)
作用和上面的方法一样,多了一个serviceInstance参数
- URI reconstructURI(ServiceInstance instance, URI original)
为系统构建一个合适的host:port形式的URI吗,在分布式系统中,我们使用逻辑上的服务名称作为host来构建URI(替代服务实例的host:port形式)进行请求,比如http://myservice/path/to/service。在该操作的定义中,**ServiceInstance**对象是带有host和port的具体服务实例,URI对象则是使用逻辑服务名定义为host的URI,而返回的URI则是通过ServiceInstance的服务实例详情拼接出的具体host:post形式的请求地址。
LoadBalancerAutoConfiguration
顺着LoadBalancerClient接口的所属包org.springframework.cloud.client.loadbalancer,可以得出下图所示的关系图
从类的命名上可初步判断LoadBalancerAutoConfiguration为实现客户端负载均衡器的自动化配置类。
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 1@Configuration
2@ConditionalOnClass(RestTemplate.class)
3@ConditionalOnBean(LoadBalancerClient.class)
4@EnableConfigurationProperties(LoadBalancerRetryProperties.class)
5public class LoadBalancerAutoConfiguration {
6
7 @LoadBalanced
8 @Autowired(required = false)
9 private List<RestTemplate> restTemplates = Collections.emptyList();
10
11 @Bean
12 public SmartInitializingSingleton loadBalancedRestTemplateInitializerDeprecated(
13 final ObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers) {
14 return () -> restTemplateCustomizers.ifAvailable(customizers -> {
15 for (RestTemplate restTemplate : LoadBalancerAutoConfiguration.this.restTemplates) {
16 for (RestTemplateCustomizer customizer : customizers) {
17 customizer.customize(restTemplate);
18 }
19 }
20 });
21 }
22
23 @Autowired(required = false)
24 private List<LoadBalancerRequestTransformer> transformers = Collections.emptyList();
25
26 @Bean
27 @ConditionalOnMissingBean
28 public LoadBalancerRequestFactory loadBalancerRequestFactory(
29 LoadBalancerClient loadBalancerClient) {
30 return new LoadBalancerRequestFactory(loadBalancerClient, transformers);
31 }
32
33 @Configuration
34 @ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
35 static class LoadBalancerInterceptorConfig {
36 @Bean
37 public LoadBalancerInterceptor ribbonInterceptor(
38 LoadBalancerClient loadBalancerClient,
39 LoadBalancerRequestFactory requestFactory) {
40 return new LoadBalancerInterceptor(loadBalancerClient, requestFactory);
41 }
42
43 @Bean
44 @ConditionalOnMissingBean
45 public RestTemplateCustomizer restTemplateCustomizer(
46 final LoadBalancerInterceptor loadBalancerInterceptor) {
47 return restTemplate -> {
48 List<ClientHttpRequestInterceptor> list = new ArrayList<>(
49 restTemplate.getInterceptors());
50 list.add(loadBalancerInterceptor);
51 restTemplate.setInterceptors(list);
52 };
53 }
54 }
55
56 @Configuration
57 @ConditionalOnClass(RetryTemplate.class)
58 public static class RetryAutoConfiguration {
59
60 @Bean
61 @ConditionalOnMissingBean
62 public LoadBalancedRetryFactory loadBalancedRetryFactory() {
63 return new LoadBalancedRetryFactory() {};
64 }
65 }
66
67 @Configuration
68 @ConditionalOnClass(RetryTemplate.class)
69 public static class RetryInterceptorAutoConfiguration {
70 @Bean
71 @ConditionalOnMissingBean
72 public RetryLoadBalancerInterceptor ribbonInterceptor(
73 LoadBalancerClient loadBalancerClient, LoadBalancerRetryProperties properties,
74 LoadBalancerRequestFactory requestFactory,
75 LoadBalancedRetryFactory loadBalancedRetryFactory) {
76 return new RetryLoadBalancerInterceptor(loadBalancerClient, properties,
77 requestFactory, loadBalancedRetryFactory);
78 }
79
80 @Bean
81 @ConditionalOnMissingBean
82 public RestTemplateCustomizer restTemplateCustomizer(
83 final RetryLoadBalancerInterceptor loadBalancerInterceptor) {
84 return restTemplate -> {
85 List<ClientHttpRequestInterceptor> list = new ArrayList<>(
86 restTemplate.getInterceptors());
87 list.add(loadBalancerInterceptor);
88 restTemplate.setInterceptors(list);
89 };
90 }
91 }
92}
93
94
95
从LoadBalancerAutoConfiguration类上面的注解可以知道,Ribbon实现的负载均衡自动化配置需要满足下面两个条件:
- @ConditionalOnClass(RestTemplate.class)
RestTemplate类必须存在于当前工程的环境中
- @ConditionalOnBean(LoadBalancerClient.class)
在Spring的Bean工程中必须有LoadBalancerClient的实现Bean
在该自动化配置类中,主要做了下面三件事:
- 创建了一个LoadBalancerInterceptor的Bean,用于实现客户端发起请求时进行拦截,以实现客户端负载均衡
- 创建了一个RestTemplateCustomizer的Bean,用于给RestTemplate增加LoadBalancerInterceptor拦截器
- 维护了一个被@LoadBalanced注解修饰的List,并在这里进行初始化,通过调用RestTemplateCustomizer的实例来给需要客户端负载均衡的RestTemplate增加LoadBalancerInterceptor拦截器
LoadBalancerInterceptor拦截器
LoadBalancerInterceptor拦截器是如何将一个普通的RestTemplate变成客户端负载均衡?
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 1public class LoadBalancerInterceptor implements ClientHttpRequestInterceptor {
2
3 private LoadBalancerClient loadBalancer;
4 private LoadBalancerRequestFactory requestFactory;
5
6 public LoadBalancerInterceptor(LoadBalancerClient loadBalancer, LoadBalancerRequestFactory requestFactory) {
7 this.loadBalancer = loadBalancer;
8 this.requestFactory = requestFactory;
9 }
10
11 public LoadBalancerInterceptor(LoadBalancerClient loadBalancer) {
12 // for backwards compatibility
13 this(loadBalancer, new LoadBalancerRequestFactory(loadBalancer));
14 }
15
16 @Override
17 public ClientHttpResponse intercept(final HttpRequest request, final byte[] body,
18 final ClientHttpRequestExecution execution) throws IOException {
19 final URI originalUri = request.getURI();
20 String serviceName = originalUri.getHost();
21 Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
22 return this.loadBalancer.execute(serviceName, requestFactory.createRequest(request, body, execution));
23 }
24}
25
26
27
可以看到在拦截器中注入了LoadBalancerClient的实现。当一个被@LoadBalanced注解修饰的RestTemplate对象向外发起HTTP请求时,会被LoadBalancerInterceptor类的intercept方法所拦截。在使用RestTemplate时采用了服务名作为host,所以直接从HttpRequest 的URI对象中getHost就能得到服务名,然后调用execute函数根据服务名选择实例并发起请求。
RibbonLoadBalancerClient
LoadBalancerClient其实只是一个抽象的负载均衡器接口,而org.springframework.cloud.netflix.ribbon包下的RibbonLoadBalancerClient才是它的具体实现类。
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 1@Override
2 public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
3 ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
4 Server server = getServer(loadBalancer);
5 if (server == null) {
6 throw new IllegalStateException("No instances available for " + serviceId);
7 }
8 RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server,
9 serviceId), serverIntrospector(serviceId).getMetadata(server));
10
11 return execute(serviceId, ribbonServer, request);
12 }
13
14 @Override
15 public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException {
16 Server server = null;
17 if(serviceInstance instanceof RibbonServer) {
18 server = ((RibbonServer)serviceInstance).getServer();
19 }
20 if (server == null) {
21 throw new IllegalStateException("No instances available for " + serviceId);
22 }
23
24 RibbonLoadBalancerContext context = this.clientFactory
25 .getLoadBalancerContext(serviceId);
26 RibbonStatsRecorder statsRecorder = new RibbonStatsRecorder(context, server);
27
28 try {
29 T returnVal = request.apply(serviceInstance);
30 statsRecorder.recordStats(returnVal);
31 return returnVal;
32 }
33 // catch IOException and rethrow so RestTemplate behaves correctly
34 catch (IOException ex) {
35 statsRecorder.recordStats(ex);
36 throw ex;
37 }
38 catch (Exception ex) {
39 statsRecorder.recordStats(ex);
40 ReflectionUtils.rethrowRuntimeException(ex);
41 }
42 return null;
43 }
44
45
可以看到。在execute函数的实现中,第一步做的就是通过getServer根据传入的服务名serviceId去获得具体的服务实例
1
2
3
4
5
6
7
8 1protected Server getServer(ILoadBalancer loadBalancer) {
2 if (loadBalancer == null) {
3 return null;
4 }
5 return loadBalancer.chooseServer("default"); // TODO: better handling of key
6 }
7
8
通过getServer函数的实现源码,发现这里获取具体服务实例的时候并没有使用LoadBalancerClient接口的choose函数,而是使用了Netflix Ribbon自身的ILoadBalancer接口中定义的chooseServer函数,具体实现则是在ZoneAwareLoadBalancer类。
接下来我们看一下ILoadBalancer 这个接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1public interface ILoadBalancer {
2 //向负载均衡器中维护的实例列表增加服务实例
3 public void addServers(List<Server> newServers);
4
5 //通过某种策略从负载均衡器中选择一个具体的服务实例
6 public Server chooseServer(Object key);
7
8 //用来标识负载均衡器中的某个具体实例已经停止服务
9 public void markServerDown(Server server);
10
11 //获取当前正常服务的实例列表
12 public List<Server> getReachableServers();
13
14 //获取所有已知的服务实例列表,包括正常服务和停止服务的实例
15 public List<Server> getAllServers();
16}
17
18
19
对ILoadBalancer接口的实现,我们整理出如下所示的结构,可以看到BaseLoadBalancer类实现了基础的负载均衡,而DynamicServerListLoadBalancer和ZoneAwareLoadBalancer在负载均衡的策略上做了一些功能扩展。
回到RibbonLoadBalancerClient的execute函数逻辑,在通过ZoneAwareLoadBalancer的chooseServer函数获取了负载均衡策略分配到的服务实例对象Server之后,将其内容包装成RibbonServer对象,然后使用该对象再回调LoadBalancerRequest的apply(final ServiceInstance instance)函数,向一个实际的具体服务实例发起请求,从而实现一开始以服务名为host的URI请求到host:post形式的实际访问地址的转换。
在**apply(final ServiceInstance instance)**函数中传入的ServiceInstance接口对象是对服务实例的抽象定义。在该接口中暴露了服务治理系统中每个服务实例需要提供的一些基本信息。
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 1public interface ServiceInstance {
2
3 /**
4 * @return the service id as registered.
5 */
6 String getServiceId();
7
8 /**
9 * @return the hostname of the registered ServiceInstance
10 */
11 String getHost();
12
13 /**
14 * @return the port of the registered ServiceInstance
15 */
16 int getPort();
17
18 /**
19 * @return if the port of the registered ServiceInstance is https or not
20 */
21 boolean isSecure();
22
23 /**
24 * @return the service uri address
25 */
26 URI getUri();
27
28 /**
29 * @return the key value pair metadata associated with the service instance
30 */
31 Map<String, String> getMetadata();
32 }
33
34
之前提到的具体包装Server服务实例的RibbonServer对象就是ServiceInstance接口的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1public static class RibbonServer implements ServiceInstance {
2 private final String serviceId;
3 private final Server server;
4 private final boolean secure;
5 private Map<String, String> metadata;
6
7 public RibbonServer(String serviceId, Server server) {
8 this(serviceId, server, false, Collections.<String, String> emptyMap());
9 }
10
11 public RibbonServer(String serviceId, Server server, boolean secure,
12 Map<String, String> metadata) {
13 this.serviceId = serviceId;
14 this.server = server;
15 this.secure = secure;
16 this.metadata = metadata;
17 }
18 ...
19 }
20
21
那么apply(final ServiceInstance instance)函数在接收到具体的ServiceInstance 实例后是如果操作的?
1
2
3
4
5
6
7
8 1public ListenableFuture<ClientHttpResponse> apply(final ServiceInstance instance)
2 throws Exception {
3 HttpRequest serviceRequest = new ServiceRequestWrapper(request,
4 instance, loadBalancer);
5 return execution.executeAsync(serviceRequest, body);
6 }
7
8
从apply的实现中还传入了ServiceRequestWrapper对象,该对象继承了HttpRequestWrapper并重写了getURI函数,重写后的getURI通过调用LoadBalancerClient接口的reconstructURI函数来重新构建一个URI来进行访问。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1public class ServiceRequestWrapper extends HttpRequestWrapper {
2 private final ServiceInstance instance;
3 private final LoadBalancerClient loadBalancer;
4
5 public ServiceRequestWrapper(HttpRequest request, ServiceInstance instance,
6 LoadBalancerClient loadBalancer) {
7 super(request);
8 this.instance = instance;
9 this.loadBalancer = loadBalancer;
10 }
11
12 @Override
13 public URI getURI() {
14 URI uri = this.loadBalancer.reconstructURI(
15 this.instance, getRequest().getURI());
16 return uri;
17 }
18}
19
20
进入到getURI()函数,可以看到回调了RibbonLoadBalancerClient的reconstructURI(ServiceInstance instance, URI original)函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 1@Override
2 public URI reconstructURI(ServiceInstance instance, URI original) {
3 Assert.notNull(instance, "instance can not be null");
4 String serviceId = instance.getServiceId();
5 RibbonLoadBalancerContext context = this.clientFactory
6 .getLoadBalancerContext(serviceId);
7
8 URI uri;reconstructURI
9 Server server;
10 if (instance instanceof RibbonServer) {
11 RibbonServer ribbonServer = (RibbonServer) instance;
12 server = ribbonServer.getServer();
13 uri = updateToSecureConnectionIfNeeded(original, ribbonServer);
14 } else {
15 server = new Server(instance.getScheme(), instance.getHost(), instance.getPort());
16 IClientConfig clientConfig = clientFactory.getClientConfig(serviceId);
17 ServerIntrospector serverIntrospector = serverIntrospector(serviceId);
18 uri = updateToSecureConnectionIfNeeded(original, clientConfig,
19 serverIntrospector, server);
20 }
21 return context.reconstructURIWithServer(server, uri);
22 }
23
24
从reconstructURI函数中我们可以看到,它通过ServiceInstance实例对象的serviceId,从SpringClientFactory类的clientFactory对象中获取对应的serviceId的负载均衡的上下文RibbonLoadBalancerContext对象。然后根据ServiceInstance中的信息来构建具体服务实例信息的Server对象,并使用RibbonLoadBalancerContext的reconstructURIWithServer(Server server, URI original)函数来构建服务实例的URI。
这里提到的SpringClientFactory类是一个用来创建客户端负载均衡器的工厂类,该工厂类会为每一个不同名的Ribbon客户端生成不同的Spring上下文。RibbonLoadBalancerContext类是LoadBalancerContext的子类,该类用于存储一些被负载均衡器使用的上下文内容和API操作。
再来看一下LoadBalancerContext 的reconstructURIWithServer函数的具体实现。
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 1public class LoadBalancerContext implements IClientConfigAware {
2 ...
3 public URI reconstructURIWithServer(Server server, URI original) {
4 String host = server.getHost();
5 int port = server.getPort();
6 String scheme = server.getScheme();
7
8 if (host.equals(original.getHost())
9 && port == original.getPort()
10 && scheme == original.getScheme()) {
11 return original;
12 }
13 if (scheme == null) {
14 scheme = original.getScheme();
15 }
16 if (scheme == null) {
17 scheme = deriveSchemeAndPortFromPartialUri(original).first();
18 }
19
20 try {
21 StringBuilder sb = new StringBuilder();
22 sb.append(scheme).append("://");
23 if (!Strings.isNullOrEmpty(original.getRawUserInfo())) {
24 sb.append(original.getRawUserInfo()).append("@");
25 }
26 sb.append(host);
27 if (port >= 0) {
28 sb.append(":").append(port);
29 }
30 sb.append(original.getRawPath());
31 if (!Strings.isNullOrEmpty(original.getRawQuery())) {
32 sb.append("?").append(original.getRawQuery());
33 }
34 if (!Strings.isNullOrEmpty(original.getRawFragment())) {
35 sb.append("#").append(original.getRawFragment());
36 }
37 URI newURI = new URI(sb.toString());
38 return newURI;
39 } catch (URISyntaxException e) {
40 throw new RuntimeException(e);
41 }
42 }
43}
44
45
从reconstructURIWithServer的实现中可以看到,它同reconstructURI的定义类似。只是reconstructURI的第一个参数为Spring Cloud定义的ServiceInstance,而reconstructURIWithServer的第一个参数是Netflix中定义的Server,所以在RibbonLoadBalancerClient实现reconstructURI的时候做了一次转换,使用ServiceInstance的host和port信息构建了一个Server对象来给reconstructURIWithServer使用。reconstructURIWithServer从Server对象中获取host和port信息,然后根据以服务名为host的URI对象original中获取其他请求信息,再进行拼接整合,形成最终要访问的服务实例的具体地址。
小结
Spring Cloud Ribbon中实现客户端负载均衡,通过LoadBalancerInterceptor拦截器对RestTemplate的请求进行拦截,并利用Spring Cloud的负载均衡器LoadBalancerClient将以逻辑服务名为host的URI转换成具体的服务实例地址的过程。同时通过分析LoadBalancerClient的Ribbon实现RibbonLoadBalancerClient,可以知道在使用Ribbon实现负载均衡器的时候,实际使用的还是会Ribbon中定义的ILoadBanlancer接口的实现,自动化配置会采用ZoneAwareLoadBalancer的实例来实现客户端负载均衡。