Spring Cloud Ribbon 是Spring Cloud Netflix 子项目的核心组件之一,主要给服务间调用及API网关转发提供负载均衡的功能,本文将对其用法进行详细介绍。
在微服务架构中,很多服务都会部署多个,其他服务去调用该服务的时候,如何保证负载均衡是个不得不去考虑的问题。负载均衡可以增加系统的可用性和扩展性,当我们使用RestTemplate来调用其他服务时,Ribbon可以很方便的实现负载均衡功能。
RestTemplate是一个HTTP客户端,使用它我们可以方便的调用HTTP接口,支持GET、POST、PUT、DELETE等方法。
<T>TgetForObject(Stringurl,Class<T>responseType,Object...uriVariables);<T>TgetForObject(Stringurl,Class<T>responseType,Map<String,?>uriVariables);<T>TgetForObject(URIurl,Class<T>responseType);<T>ResponseEntity<T>getForEntity(Stringurl,Class<T>responseType,Object...uriVariables);<T>ResponseEntity<T>getForEntity(Stringurl,Class<T>responseType,Map<String,?>uriVariables);<T>ResponseEntity<T>getForEntity(URIvar1,Class<T>responseType);Copy to clipboardErrorCopied
返回对象为响应体中数据转化成的对象,举例如下:
@GetMapping("/{id}")publicCommonResultgetUser(@PathVariableLongid){returnrestTemplate.getForObject(userServiceUrl+"/user/{1}",CommonResult.class,id);}Copy to clipboardErrorCopied
返回对象为ResponseEntity对象,包含了响应中的一些重要信息,比如响应头、响应状态码、响应体等,举例如下:
@GetMapping("/getEntityByUsername")publicCommonResultgetEntityByUsername(@RequestParamStringusername){ResponseEntity<CommonResult>entity=restTemplate.getForEntity(userServiceUrl+"/user/getByUsername?username={1}",CommonResult.class,username);if(entity.getStatusCode().is2xxSuccessful()){returnentity.getBody();}else{returnnewCommonResult("操作失败",500);}}Copy to clipboardErrorCopied
<T>TpostForObject(Stringurl,@NullableObjectrequest,Class<T>responseType,Object...uriVariables);<T>TpostForObject(Stringurl,@NullableObjectrequest,Class<T>responseType,Map<String,?>uriVariables);<T>TpostForObject(URIurl,@NullableObjectrequest,Class<T>responseType);<T>ResponseEntity<T>postForEntity(Stringurl,@NullableObjectrequest,Class<T>responseType,Object...uriVariables);<T>ResponseEntity<T>postForEntity(Stringurl,@NullableObjectrequest,Class<T>responseType,Map<String,?>uriVariables);<T>ResponseEntity<T>postForEntity(URIurl,@NullableObjectrequest,Class<T>responseType);Copy to clipboardErrorCopied
@PostMapping("/create")publicCommonResultcreate(@RequestBodyUseruser){returnrestTemplate.postForObject(userServiceUrl+"/user/create",user,CommonResult.class);}Copy to clipboardErrorCopied
@PostMapping("/create")publicCommonResultcreate(@RequestBodyUseruser){returnrestTemplate.postForEntity(userServiceUrl+"/user/create",user,CommonResult.class).getBody();}Copy to clipboardErrorCopied
voidput(Stringurl,@NullableObjectrequest,Object...uriVariables);voidput(Stringurl,@NullableObjectrequest,Map<String,?>uriVariables);voidput(URIurl,@NullableObjectrequest);Copy to clipboardErrorCopied
@PutMapping("/update")publicCommonResultupdate(@RequestBodyUseruser){restTemplate.put(userServiceUrl+"/user/update",user);returnnewCommonResult("操作成功",200);}Copy to clipboardErrorCopied
voiddelete(Stringurl,Object...uriVariables);voiddelete(Stringurl,Map<String,?>uriVariables);voiddelete(URIurl);Copy to clipboardErrorCopied
@DeleteMapping("/delete/{id}")publicCommonResultdelete(@PathVariableLongid){restTemplate.delete(userServiceUrl+"/user/delete/{1}",null,id);returnnewCommonResult("操作成功",200);}Copy to clipboardErrorCopied
首先我们创建一个user-service,用于给Ribbon提供服务调用。
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>Copy to clipboardErrorCopied
主要是配置了端口和注册中心地址。
server:
port: 8201
spring:
application:
name: user-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/Copy to clipboardErrorCopied
UserController类定义了对User对象常见的CRUD接口。
/**
* Created by macro on 2019/8/29.
*/@RestController@RequestMapping("/user")publicclassUserController{privateLoggerLOGGER=LoggerFactory.getLogger(this.getClass());@AutowiredprivateUserServiceuserService;@PostMapping("/create")publicCommonResultcreate(@RequestBodyUseruser){userService.create(user);returnnewCommonResult("操作成功",200);}@GetMapping("/{id}")publicCommonResult<User>getUser(@PathVariableLongid){Useruser=userService.getUser(id);LOGGER.info("根据id获取用户信息,用户名称为:{}",user.getUsername());returnnewCommonResult<>(user);}@GetMapping("/getUserByIds")publicCommonResult<List<User>>getUserByIds(@RequestParamList<Long>ids){List<User>userList=userService.getUserByIds(ids);LOGGER.info("根据ids获取用户信息,用户列表为:{}",userList);returnnewCommonResult<>(userList);}@GetMapping("/getByUsername")publicCommonResult<User>getByUsername(@RequestParamStringusername){Useruser=userService.getByUsername(username);returnnewCommonResult<>(user);}@PostMapping("/update")publicCommonResultupdate(@RequestBodyUseruser){userService.update(user);returnnewCommonResult("操作成功",200);}@PostMapping("/delete/{id}")publicCommonResultdelete(@PathVariableLongid){userService.delete(id);returnnewCommonResult("操作成功",200);}}Copy to clipboardErrorCopied
这里我们创建一个ribbon-service模块来调用user-service模块演示负载均衡的服务调用。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency>Copy to clipboardErrorCopied
主要是配置了端口、注册中心地址及user-service的调用路径。
server:
port: 8301
spring:
application:
name: ribbon-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
service-url:
user-service: http://user-serviceCopy to clipboardErrorCopied
使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
可以看出使用Ribbon的负载均衡功能非常简单,和直接使用RestTemplate没什么两样,只需给RestTemplate添加一个@LoadBalanced即可。
/**
* Created by macro on 2019/8/29.
*/@ConfigurationpublicclassRibbonConfig{@Bean@LoadBalancedpublicRestTemplaterestTemplate(){returnnewRestTemplate();}}Copy to clipboardErrorCopied
注入RestTemplate,使用其调用user-service中提供的相关接口,这里对GET和POST调用进行了演示,其他方法调用均可参考。
/**
* Created by macro on 2019/8/29.
*/@RestController@RequestMapping("/user")publicclassUserRibbonController{@AutowiredprivateRestTemplaterestTemplate;@Value("${service-url.user-service}")privateStringuserServiceUrl;@GetMapping("/{id}")publicCommonResultgetUser(@PathVariableLongid){returnrestTemplate.getForObject(userServiceUrl+"/user/{1}",CommonResult.class,id);}@GetMapping("/getByUsername")publicCommonResultgetByUsername(@RequestParamStringusername){returnrestTemplate.getForObject(userServiceUrl+"/user/getByUsername?username={1}",CommonResult.class,username);}@GetMapping("/getEntityByUsername")publicCommonResultgetEntityByUsername(@RequestParamStringusername){ResponseEntity<CommonResult>entity=restTemplate.getForEntity(userServiceUrl+"/user/getByUsername?username={1}",CommonResult.class,username);if(entity.getStatusCode().is2xxSuccessful()){returnentity.getBody();}else{returnnewCommonResult("操作失败",500);}}@PostMapping("/create")publicCommonResultcreate(@RequestBodyUseruser){returnrestTemplate.postForObject(userServiceUrl+"/user/create",user,CommonResult.class);}@PostMapping("/update")publicCommonResultupdate(@RequestBodyUseruser){returnrestTemplate.postForObject(userServiceUrl+"/user/update",user,CommonResult.class);}@PostMapping("/delete/{id}")publicCommonResultdelete(@PathVariableLongid){returnrestTemplate.postForObject(userServiceUrl+"/user/delete/{1}",null,CommonResult.class,id);}}Copy to clipboardErrorCopied
启动eureka-server于8001端口;
启动user-service于8201端口;
启动另一个user-service于8202端口,可以通过修改IDEA中的SpringBoot的启动配置实现:
此时运行中的服务如下:
调用接口进行测试:http://localhost:8301/user/1
可以发现运行在8201和8202的user-service控制台交替打印如下信息:
ribbon:
ConnectTimeout: 1000 #服务请求连接超时时间(毫秒)
ReadTimeout: 3000 #服务请求处理超时时间(毫秒)
OkToRetryOnAllOperations: true #对超时请求启用重试机制
MaxAutoRetriesNextServer: 1 #切换重试实例的最大个数
MaxAutoRetries: 1 # 切换实例后重试最大次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #修改负载均衡算法Copy to clipboardErrorCopied
与全局配置的区别就是ribbon节点挂在服务名称下面,如下是对ribbon-service调用user-service时的单独配置。
user-service:
ribbon:
ConnectTimeout: 1000 #服务请求连接超时时间(毫秒)
ReadTimeout: 3000 #服务请求处理超时时间(毫秒)
OkToRetryOnAllOperations: true #对超时请求启用重试机制
MaxAutoRetriesNextServer: 1 #切换重试实例的最大个数
MaxAutoRetries: 1 # 切换实例后重试最大次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #修改负载均衡算法Copy to clipboardErrorCopied
所谓的负载均衡策略,就是当A服务调用B服务时,此时B服务有多个实例,这时A服务以何种方式来选择调用的B实例,ribbon可以选择以下几种负载均衡策略。
com.netflix.loadbalancer.RandomRule:从提供服务的实例中以随机的方式;
com.netflix.loadbalancer.RoundRobinRule:以线性轮询的方式,就是维护一个计数器,从提供服务的实例中按顺序选取,第一次选第一个,第二次选第二个,以此类推,到最后一个以后再从头来过;
com.netflix.loadbalancer.RetryRule:在RoundRobinRule的基础上添加重试机制,即在指定的重试时间内,反复使用线性轮询策略来选择可用实例;
com.netflix.loadbalancer.WeightedResponseTimeRule:对RoundRobinRule的扩展,响应速度越快的实例选择权重越大,越容易被选择;
com.netflix.loadbalancer.BestAvailableRule:选择并发较小的实例;
com.netflix.loadbalancer.AvailabilityFilteringRule:先过滤掉故障实例,再选择并发较小的实例;
com.netflix.loadbalancer.ZoneAwareLoadBalancer:采用双重过滤,同时过滤不是同一区域的实例和故障实例,选择并发较小的实例。
springcloud-learning
├── eureka-server -- eureka注册中心
├── user-service -- 提供User对象CRUD接口的服务
└── ribbon-service -- ribbon服务调用测试服务
网友评论