Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。它是一个基于HTTP和TCP的客户端负载均衡器。它能够经过在客户端中配置ribbonServerList来设置服务端列表去轮询访问以达到均衡负载的做用。html
当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务实例列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来肯定服务端是否已经启动。web
而当Ribbon与Consul联合使用时,ribbonServerList会被ConsulServerList来扩展成从Consul获取服务实例列表。同时由ConsulPing来做为IPing接口的实现。spring
咱们在使用Spring Cloud Ribbon的时候,不管是与Eureka仍是Consul结合,都会在引入Spring Cloud Eureka或Spring Cloud Consul依赖的时候经过自动化配置来加载上述所说的配置内容,因此咱们能够快速在Spring Cloud中实现服务间调用的负载均衡。架构
下面咱们经过具体的例子来看看如何使用Spring Cloud Ribbon来实现服务的调用以及客户端均衡负载。app
咱们将利用以前构建的eureka-server
做为服务注册中心、eureka-client
做为服务提供者做为基础。而基于Spring Cloud Ribbon实现的消费者,咱们能够根据eureka-consumer
实现的内容进行简单改在就能完成,具体步骤以下:负载均衡
eureka-consumer
复制一个服务消费者工程,命名为:eureka-consumer-ribbon
。在pom.xml
中增长下面的依赖:
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies>
...
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency>
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>org.springframework.cloud</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">groupId>
<<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>spring-cloud-starter-ribbon</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">artifactId>
</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependency>
</<span class="name" style="box-sizing: border-box; margin: 0px; padding: 0px; font-weight: inherit; border: 0px; font-style: inherit; font-family: inherit; vertical-align: baseline; color: rgb(38, 139, 210);">dependencies>
|
RestTemplate
增长@LoadBalanced
注解:
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
|
LoadBalancerClient
选取实例和拼接URL的步骤,直接经过RestTemplate发起请求。
@RestController
public class DcController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/consumer")
public String dc() {
return restTemplate.getForObject( String.class);
}
}
|
能够看到这里,咱们除了去掉了原来与LoadBalancerClient
相关的逻辑以外,对于RestTemplate
的使用,咱们的第一个url参数有一些特别。这里请求的host位置并无使用一个具体的IP地址和端口的形式,而是采用了服务名的方式组成。那么这样的请求为何能够调用成功呢?由于Spring Cloud Ribbon有一个拦截器,它可以在这里进行实际调用的时候,自动的去选取服务实例,并将实际要请求的IP地址和端口替换这里的服务名,从而完成服务接口的调用。框架
从如今开始,我这边会将近期研发的springcloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,但愿能够帮助更多的好学者。你们来一块儿探讨spring cloud架构的搭建过程及如何运用于企业项目。源码来源微服务