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

Spring Cloud微服务(4)之Ribbon客户端负载均衡

2017-07-30 17:06 811 查看
1.介绍

Ribbon 是 Netflix 发布的开源项目,主要功能是提供客户端软件的负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列的配置项,如连接超时、

重试等。Ribbon提供了客户端负载均衡的功能, Ribbon利用从Eureka中读取到的服务信息列表,在调用服务实例时,合理的进行负载。

客户端负载均衡具体来说是通过采取某种策略为客户端选择一个服务端,从而尽可能减少对服务端的压力,达到负载均衡的效果。当请求面对同一服务的众多实例时就

懵了,应该选择哪个呢。

请求不会智能到应该自己知道应该选择哪个实例,它会始终如一的向同一实例不断发起请求,这一个被请求的实例有分分钟累死的节奏,其他实例无能为力,只有干瞪眼的

份。这时需要有人对请求做出判断和选择,通过负载均衡将请求发给不同的实例去处理。

2.如何使用

通过负载均衡服务来调用订单服务,验证负载均衡服务每次是否对订单服务的两个实例进行选择,从而达到负载均衡的作用。

请求处理流程:由负载均衡服务将请求进行分发,每次将请求发送到不同的实例上面。

demo需要五个工程: eureka-server、loadbalance-service、order-service、oeder-service2、zuul-service

第一步:创建 loadbalance-service 工程。

目录结构如图:



(1)启动代码。

加了注解 @LoadBanlanced 之后,这就变成了可以被注入到任意service bean的强化型 restTemplate实例,通过一行注解就能实现负载均衡的能力。 @LoadBanlaned

使用的是 spring-cloud-comons包下的org.springframework.cloud.lient.loadbalancer的LoadBalanced接口。

代码如下:

package com.hole;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class LoadbalanceServiceApplication {

@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(LoadbalanceServiceApplication.class, args);
}
}


(2)在controller中增加两个路径映射,分别对应产品服务和订单服务,这样就可以根据不同的请求地址,将请求分发给不同的服务,继而通过负载均衡调用不同的实例。

package com.hole.web;

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

/**
* Created by helei on 2017/7/16.
*/
@RestController
public class LoadbalanceController {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/product/hello",method = RequestMethod.GET)
public String productHello(){
return restTemplate.getForEntity("http://PRODUCT-SERVICE/hello",String.class).getBody();
}

@RequestMapping(value = "/order/hello",method = RequestMethod.GET)
public String orderHello(){
return restTemplate.getForEntity("http://ORDER-SERVICE/hello",String.class).getBody();
}
}

第二步:创建两个订单工程 order-service、order-service2。

(1)order-service和order-service2端口设置成不一样的:

order-service 端口 3331

order-service2 端口 3332

(2)两个工程controller分别设置路径映射,设置不同的返回结果以便于验证每次请求的实例不同。

order-service 显示 hello order service

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public ResponseEntity<String> hello(){
return new ResponseEntity<String>("hello order service!", HttpStatus.OK);
}

order-service2显示 hello order service2

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public ResponseEntity<String> hello(){
return new ResponseEntity<String>("hello order service2!", HttpStatus.OK);
}


第三步:创建网关zuul-service工程


创建方法请参照上节,不再赘述。这里只讲变化的部分。

再application.properties配置中增加路由配置:

zuul.routes.loadbalance-service.path=/loadbalance-service/**
zuul.routes.loadbalance-service.serviceId=loadbalance-service

或:

zuul.routes..loadbalance-service-url.path=/loadbalance-service-url/**
zuul.routes..loadbalance-service-url.url=http://localhost:8763/


请求处理流程:

请求经过网关zuul-service路由到负责均衡服务上,然后负载均衡在将请求进行分发到不同的实例上。

第四步:启动。

启动这五个工程,启动成功后监控页面可以看到相关服务已经启动。



第五步:验证。
请求网关服务http://localhost:8765/loadbalance-service/order/hello,通过网关路由到负载均衡器,再由负载均衡分发到订单服务。



再次请求相同地址,会发现此时请求被分发到了订单服务 order-service2上,多试几次试试效果。



产品服务product-service同理,因为篇幅原因就不再一一演示了,有兴趣的童鞋可以自己动手测验。有错误或问题欢迎留言交流。

揉揉眼睛,天都黑了......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息