Ribbon是Netflix下的负载均衡项目,它主要实现中间层应用程序的负载均衡。为Ribbon配置服务提供者地址列表后,Ribbon就会基于某种负载均衡算法,自动帮助服务调用者去请求。Ribbon默认提供的负载均衡算法有多种,例如轮询、随即、加权轮训等,也可以为Ribbon实现自定义的负载均衡算法。

Ribbon有以下特性:

  • 负载均衡器,可支持插拔式的负载均衡规则
  • 对多种协议提供支持,如HTTP、TCP、UDP
  • 集成了负载均衡功能的客户端

Feign利用Ribbon实现负载均衡的过程:

  • 通过在启动类加@EnableFeignCleints注解开启FeignCleint
  • 根据Feign的规则实现接口,并加在接口定义处添加@FeignCleint注解
  • 服务启动后,扫描带有@ FeignCleint的注解的类,并将这些信息注入到ioc容器中
  • 当接口的方法被调用,通过jdk的代理,来生成具体的RequesTemplate
  • RequesTemplate再生成Request
  • Request交给Client去处理,其中Client可以是HttpUrlConnection、HttpClient也可以是Okhttp

    最后Client被封装到LoadBalanceClient类,这个类结合类Ribbon做到了负载均衡。

OpenFeign 中使用 Ribbon 进行负载均衡,所以 OpenFeign 直接内置了 Ribbon。在导入OpenFeign 依赖后,无需再专门导入 Ribbon 依赖。接下来,我们把gitegg-service-base作为服务的调用方,启动两个不同端口的gitegg-service-system作为服务的被调用方,测试Ribbon的负载均衡。

1、首先在gitegg-service-system工程中,新建被调用的controller方法,返回系统配置的端口号以区分是哪个服务被调用了。

package com.gitegg.service.system.controller;

import com.gitegg.platform.boot.common.base.Result;
import com.gitegg.service.system.dto.SystemDTO;
import com.gitegg.service.system.service.ISystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @RestController
@RequestMapping(value = "system")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "gitegg-system")
@RefreshScope
public class SystemController { private final ISystemService systemService; @Value("${spring.datasource.maxActive}")
private String nacosMaxActiveType; @Value("${server.port}")
private Integer serverPort; @GetMapping(value = "list")
@ApiOperation(value = "system list接口")
public Object list() {
return systemService.list();
} @GetMapping(value = "page")
@ApiOperation(value = "system page接口")
public Object page() {
return systemService.page();
} @GetMapping(value = "exception")
@ApiOperation(value = "自定义异常及返回测试接口")
public Result<string> exception() {
return Result.data(systemService.exception());
} @PostMapping(value = "valid")
@ApiOperation(value = "参数校验测试接口")
public Result<systemdto> valid(@Valid @RequestBody SystemDTO systemDTO) {
return Result.data(systemDTO);
} @PostMapping(value = "nacos")
@ApiOperation(value = "Nacos读取配置文件测试接口")
public Result<string> nacos() {
return Result.data(nacosMaxActiveType);
} @GetMapping(value = "api/by/id")
@ApiOperation(value = "Fegin Get调用测试接口")
public Result<object> feginById(@RequestParam("id") String id) {
return Result.data(systemService.list());
} @PostMapping(value = "api/by/dto")
@ApiOperation(value = "Fegin Post调用测试接口")
public Result<object> feginByDto(@Valid @RequestBody SystemDTO systemDTO) {
return Result.data(systemDTO);
} @GetMapping("/api/ribbon")
@ApiOperation(value = "Ribbon调用测试接口")
public Result<string> testRibbon() {
return Result.data("现在访问的服务端口是:" + serverPort);
}
}

2、在gitegg-service-system-api工程中,编写使用OpenFeign调用testRibbon的公共方法

package com.gitegg.service.system.api.feign;

import com.gitegg.platform.boot.common.base.Result;
import com.gitegg.service.system.api.dto.ApiSystemDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "gitegg-service-system")
public interface ISystemFeign { /**
* OpenFeign测试Get
*
* @param id
* @return
*/
@GetMapping("/system/api/by/id")
Result<object> querySystemById(@RequestParam("id") Long id); /**
* OpenFeign测试Post
*
* @param apiSystemDTO
* @return ApiSystemDTO
*/
@PostMapping("/system/api/by/dto")
Result<apisystemdto> querySystemByDto(@RequestBody ApiSystemDTO apiSystemDTO); /**
* OpenFeign测试Ribbon负载均衡功能
* @return
*/
@GetMapping("/system/api/ribbon")
Result<string> testRibbon(); }

3、在gitegg-service-base中添加测试Ribbon负载均衡的Feign调用方法

package com.gitegg.service.base.controller;

import com.gitegg.platform.boot.common.base.Result;
import com.gitegg.service.system.api.dto.ApiSystemDTO;
import com.gitegg.service.system.api.feign.ISystemFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @RestController
@RequestMapping(value = "base")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "gitegg-base")
@RefreshScope
public class BaseController { private final ISystemFeign systemFeign; @GetMapping(value = "api/by/id")
@ApiOperation(value = "Fegin Get调用测试接口")
public Result<object> feginById(@RequestParam("id") Long id) {
return Result.data(systemFeign.querySystemById(id));
} @PostMapping(value = "api/by/dto")
@ApiOperation(value = "Fegin Post调用测试接口")
public Result<object> feginByDto(@Valid @RequestBody ApiSystemDTO systemDTO) {
return Result.data(systemFeign.querySystemByDto(systemDTO));
} @PostMapping(value = "api/ribbon")
@ApiOperation(value = "Ribbon调用测试接口")
public Result<object> testRibbon() {
return Result.data(systemFeign.testRibbon());
}
}

4、先启动gitegg-service-base服务,再启动gitegg-service-system服务,服务启动成功之后,将gitegg-service-system下bootstrap.yml里面server.port改为8011,然后再点击启动,这样就启动了两个gitegg-service-system服务(如果运行两个服务时提示:gitegg-service-system is not allowed to run in parallel. Would you like to stop the running one? 这时,在IDEA中点击Run-Edit configurations-勾选Allow parallel run即可),服务全部启动完毕之后,可以在Console窗口里面看到三个服务的Console

三个服务:

5、打开浏览器访问:http://127.0.0.1:8001/doc.html,点击Ribbon调用测试接口

菜单,进行测试,点击请求,我们可以看到每次返回的端口都是变化的,一会儿是8001一会儿是8011,因为Ribbon负载均衡默认是使用的轮询策略



6、如果我们需要修改负载均衡策略或者自定义负载均衡策略,根据我们的架构设计,我们在GitEgg-Platform的子工程gitegg-platform-cloud中设置公共的负载均衡策略,然后每个微服务需要不同的策略的话,可以在自己的工程中添加配置文件。接下来,在gitegg-platform-cloud中新建Ribbon配置类

package com.gitegg.platform.cloud.ribbon.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @Description Ribbon公共负载均衡策略配置
*/
@Configuration
public class RibbonConfig { /**
* 负载均衡策略配置
* @return
*/
@Bean
public IRule rule(){
//随机策略 从所有可用的提供者中随机选择一个
return new RandomRule();
} }

7、修改完成之后,GitEgg_Platform工程重新执行install,GitEgg_Cloud刷新导入的包,参照步骤5再执行测试,这时我们发现微服务返回的端口,不再是有规律的切换,而是随机不确定的出现。

注意:

这里RibbonConfig只用于测试负载均衡策略,请不要在生产环境中这样使用,否则会出现问题:在微服务A中调用微服务B和微服务C,然后再调用微服务B,这是RibbonLoadBalancerClient在获取微服务时,渠到的serviceId为null,就会获取到上次的微服务,进而导致404错误。因为OpenFeign默认使用的是Ribbon提供的负载均衡策略,我们在实际应用中可以选择Nacos提供的NacosRule策略,利用Nacos权重进行负载均衡:
  #负载均衡策略
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule

本文源码在https://gitee.com/wmz1930/GitEgg 的chapter-12分支。

最新文章

  1. spring3.0+mybatis+spring快速入门
  2. Bean不同配置方式的比较
  3. js 数组删除指定元素
  4. JAVA标签的使用跳出循环
  5. windows下编译支持https的libcurl
  6. Ext.Net中的Task控件的使用
  7. POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配
  8. C#同步SQL Server数据库中的数据--数据库同步工具[同步新数据]
  9. HBase表删除问题
  10. 【机器学习PAI实践一】搭建心脏病预测案例
  11. C# 构造函数中base和this的使用。
  12. 软件补丁问题(SPFA+位运算)
  13. Django聚合与分组查询中value与annotate的顺序问题
  14. JDK源码分析(6)ConcurrentHashMap
  15. linux下命令行工具gcp显示拷贝进度条
  16. Hibernate 抛出的 Could not execute JDBC batch update
  17. 20172305 2018-2019-1 《Java软件结构与数据结构》第九周学习总结
  18. [Linux 005]——IO重定向
  19. 配置hadoop集群的lzo压缩
  20. JS原生评分组件

热门文章

  1. 密码学系列之:Argon2加密算法详解
  2. CORS跨域请求规则以及在Spring中的实现
  3. 学习PDO中的错误与错误处理模式
  4. PHP的另一个高效缓存扩展:Yac
  5. 彻底搞明白PHP的中引用的概念
  6. Java面向对象系列(9)- 方法重写
  7. spring入门1-IOC和DI
  8. sonar扫描java项目报错
  9. px em rem区别
  10. Spring,IOC源码分析