文章目录
- 前言
- 一、Ribbon是什么
- 二、Ribbon的实现
- 三、Ribbon的配置
前言
作为一个分布式微服务框架,负载均衡是必不可少的。负载均衡的手段很多 比如硬件方面的F5 ,软件层面的ngnix,这里我们讲解一下springcloud的一个重要的负载均衡组件ribbon
提示:以下是本篇文章正文内容,下面案例可供参考
一、Ribbon是什么
Ribbon区别于Ngnix 他是一个基于客户端的负载均衡组件,他通过获取服务器列表 然后通过一定的策略进行负载均衡,而Ngnix 是一个服务端的负载均衡,利用反向代理的方式实现将客户端请求发送到服务端。
二、Ribbon的实现
Ribbon主要是通过@LoadBalanced注解的方式,添加了一个拦截器:LoadbanlanceInterceptor 代码如下
public class LoadBalancerInterceptor implements ClientHttpRequestInterceptor {
private LoadBalancerClient loadBalancer;
private LoadBalancerRequestFactory requestFactory;
public LoadBalancerInterceptor(LoadBalancerClient loadBalancer, LoadBalancerRequestFactory requestFactory) {
this.loadBalancer = loadBalancer;
this.requestFactory = requestFactory;
}
public LoadBalancerInterceptor(LoadBalancerClient loadBalancer) {
this(loadBalancer, new LoadBalancerRequestFactory(loadBalancer));
}
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException {
URI originalUri = request.getURI();
String serviceName = originalUri.getHost();
Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
return (ClientHttpResponse)this.loadBalancer.execute(serviceName, this.requestFactory.createRequest(request, body, execution));
}
}
代码里调用了LoadBalancerClient 的excute方法,这个方法就是用来获取服务器列表,然后进行根据策略发送请求,LoadBalancerClient 这个类是一个接口类 ,查看源码发现他有一个唯一的实现类 RibbonLoadBalancerClient
public class RibbonLoadBalancerClient implements LoadBalancerClient {
private SpringClientFactory clientFactory;
public RibbonLoadBalancerClient(SpringClientFactory clientFactory) {
this.clientFactory = clientFactory;
}
public URI reconstructURI(ServiceInstance instance, URI original) {
Assert.notNull(instance, "instance can not be null");
String serviceId = instance.getServiceId();
RibbonLoadBalancerContext context = this.clientFactory.getLoadBalancerContext(serviceId);
URI uri;
Server server;
if (instance instanceof RibbonLoadBalancerClient.RibbonServer) {
RibbonLoadBalancerClient.RibbonServer ribbonServer = (RibbonLoadBalancerClient.RibbonServer)instance;
server = ribbonServer.getServer();
uri = RibbonUtils.updateToSecureConnectionIfNeeded(original, ribbonServer);
} else {
server = new Server(instance.getScheme(), instance.getHost(), instance.getPort());
IClientConfig clientConfig = this.clientFactory.getClientConfig(serviceId);
ServerIntrospector serverIntrospector = this.serverIntrospector(serviceId);
uri = RibbonUtils.updateToSecureConnectionIfNeeded(original, clientConfig, serverIntrospector, server);
}
return context.reconstructURIWithServer(server, uri);
}
public ServiceInstance choose(String serviceId) {
Server server = this.getServer(serviceId);
return server == null ? null : new RibbonLoadBalancerClient.RibbonServer(serviceId, server, this.isSecure(server, serviceId), this.serverIntrospector(serviceId).getMetadata(server));
}
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
ILoadBalancer loadBalancer = this.getLoadBalancer(serviceId);
Server server = this.getServer(loadBalancer);
if (server == null) {
throw new IllegalStateException("No instances available for " + serviceId);
} else {
RibbonLoadBalancerClient.RibbonServer ribbonServer = new RibbonLoadBalancerClient.RibbonServer(serviceId, server, this.isSecure(server, serviceId), this.serverIntrospector(serviceId).getMetadata(server));
return this.execute(serviceId, ribbonServer, request);
}
}
public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException {
Server server = null;
if (serviceInstance instanceof RibbonLoadBalancerClient.RibbonServer) {
server = ((RibbonLoadBalancerClient.RibbonServer)serviceInstance).getServer();
}
if (server == null) {
throw new IllegalStateException("No instances available for " + serviceId);
} else {
RibbonLoadBalancerContext context = this.clientFactory.getLoadBalancerContext(serviceId);
RibbonStatsRecorder statsRecorder = new RibbonStatsRecorder(context, server);
try {
T returnVal = request.apply(serviceInstance);
statsRecorder.recordStats(returnVal);
return returnVal;
} catch (IOException var8) {
statsRecorder.recordStats(var8);
throw var8;
} catch (Exception var9) {
statsRecorder.recordStats(var9);
ReflectionUtils.rethrowRuntimeException(var9);
return null;
}
}
}
略。。。。。
}
}
负载均衡的是通过 ILoadBalancer 接口来完成的。 他又有几个实现类
其中默认的是ZoneAwareLoadBalancer 这个我们从spingBoot自动装配的配置文件中可以看出 如果不了解自动装配的原理 可以看一下这篇文章spring自动装配原理
@Bean
@ConditionalOnMissingBean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config, ServerList<Server> serverList, ServerListFilter<Server> serverListFilter, IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
return (ILoadBalancer)(this.propertiesFactory.isSet(ILoadBalancer.class, this.name) ? (ILoadBalancer)this.propertiesFactory.get(ILoadBalancer.class, config, this.name) : new ZoneAwareLoadBalancer(config, rule, ping, serverList, serverListFilter, serverListUpdater));
}
查看ZoneAwareLoadBalancer 的构造方法,里面有几个参数
:
1.IClientConfig 客户端或者负载均衡的配置
2.IRule 负载均衡策略
3.IPing 检查服务器是否存活
4.ServerList 获取所有的服务实例清单。
5.ServerListFilter 过滤服务器
6.ServerListUpdater 动态更新服务器列表
public ZoneAwareLoadBalancer(IClientConfig clientConfig, IRule rule, IPing ping, ServerList<T> serverList, ServerListFilter<T> filter, ServerListUpdater serverListUpdater) {
super(clientConfig, rule, ping, serverList, filter, serverListUpdater);
}
此处如果需要修改负载均衡的策略 可以在yml文件中做如下配置
应用名.ribbon.NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
然后需要在启动类中实例化IRule 类
//将负载均衡策略改成随机策略
@Bean
public IRule newRule() {
return new RandomRule();
}
默认配置类:
public class DefaultClientConfigImpl implements IClientConfig {
public static final Boolean DEFAULT_PRIORITIZE_VIP_ADDRESS_BASED_SERVERS;
public static final String DEFAULT_NFLOADBALANCER_PING_CLASSNAME = "com.netflix.loadbalancer.DummyPing";
public static final String DEFAULT_NFLOADBALANCER_RULE_CLASSNAME = "com.netflix.loadbalancer.AvailabilityFilteringRule";
public static final String DEFAULT_NFLOADBALANCER_CLASSNAME = "com.netflix.loadbalancer.ZoneAwareLoadBalancer";
public static final boolean DEFAULT_USEIPADDRESS_FOR_SERVER;
public static final String DEFAULT_CLIENT_CLASSNAME = "com.netflix.niws.client.http.RestClient";
public static final String DEFAULT_VIPADDRESS_RESOLVER_CLASSNAME = "com.netflix.client.SimpleVipAddressResolver";
public static final String DEFAULT_PRIME_CONNECTIONS_URI = "/";
public static final int DEFAULT_MAX_TOTAL_TIME_TO_PRIME_CONNECTIONS = 30000;
public static final int DEFAULT_MAX_RETRIES_PER_SERVER_PRIME_CONNECTION = 9;
public static final Boolean DEFAULT_ENABLE_PRIME_CONNECTIONS;
public static final int DEFAULT_MAX_REQUESTS_ALLOWED_PER_WINDOW = 2147483647;
public static final int DEFAULT_REQUEST_THROTTLING_WINDOW_IN_MILLIS = 60000;
public static final Boolean DEFAULT_ENABLE_REQUEST_THROTTLING;
public static final Boolean DEFAULT_ENABLE_GZIP_CONTENT_ENCODING_FILTER;
public static final Boolean DEFAULT_CONNECTION_POOL_CLEANER_TASK_ENABLED;
public static final Boolean DEFAULT_FOLLOW_REDIRECTS;
public static final float DEFAULT_PERCENTAGE_NIWS_EVENT_LOGGED = 0.0F;
public static final int DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER = 1;
public static final int DEFAULT_MAX_AUTO_RETRIES = 0;
public static final int DEFAULT_BACKOFF_INTERVAL = 0;
public static final int DEFAULT_READ_TIMEOUT = 5000;
public static final int DEFAULT_CONNECTION_MANAGER_TIMEOUT = 2000;
public static final int DEFAULT_CONNECT_TIMEOUT = 2000;
public static final Boolean DEFAULT_ENABLE_CONNECTION_POOL;
三、Ribbon的配置
由于eureka 中已经有了RIbbo的jar包 所以不需要额外添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
然后在启动类中在restTemplate方法中加上 @LoadBalanced注解即可:
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCaching
@MapperScan("org.woka.wokaadminweb.mapper.dsno1")
public class WokaAdminWebApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(WokaAdminWebApplication.class, args);
}
}