SpringCloud 进阶之Ribbon和Feign(负载均衡)

1. Ribbon 负载均衡

  • Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡的工具;

1.1 Ribbon 配置初步

1.1.1 修改 microservicecloud-consumer-dept-80

1// pom.xml 2<!-- Ribbon相关 --> 3<dependency> 4 <groupId>org.springframework.cloud</groupId> 5 <artifactId>spring-cloud-starter-eureka</artifactId> 6</dependency> 7<dependency> 8 <groupId>org.springframework.cloud</groupId> 9 <artifactId>spring-cloud-starter-ribbon</artifactId> 10</dependency> 11<dependency> 12 <groupId>org.springframework.cloud</groupId> 13 <artifactId>spring-cloud-starter-config</artifactId> 14</dependency> 15 16 17// 修改application.yml,追加eureka的服务注册地址 18eureka: 19 client: 20 register-with-eureka: false 21 service-url: 22 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ 23 24 25// ConfigBean 添加新注解 @LoadBalanced, 用于加入 Ribbon 配置 26@Configuration 27public class ConfigBean { 28 @Bean 29 @LoadBalanced 30 public RestTemplate getRestTemplate() { 31 return new RestTemplate(); 32 } 33} 34 35 36// 主启动类 DeptConsumer80_App添加 @EnableEurekaClient 37@SpringBootApplication 38@EnableEurekaClient 39public class DeptConsumer80_App { 40 41 public static void main(String[] args) { 42 43 SpringApplication.run(DeptConsumer80_App.class, args); 44 } 45 46} 47 48 49// 修改 DeptController_Consumer 客户端访问类 50private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT"; 51 52 53// 测试访问: 54// http://localhost:8082/consumer/dept/get/1 55// http://localhost:8082/consumer/dept/list 56// http://localhsot:8082/consumer/dept/add?dname=广告部 57

1.2 Ribbon 负载均衡

1222878-20180616191755869-1130950597.png

1// 新建microservicecloud-provider-dept-8002 2// 新建microservicecloud-provider-dept-8003 3 4// 新建8002/8003数据库 5 6// 修改8002/8003各自YML 7

1.3 Ribbon 核心组件IRule

  • 根据特定算法,从服务列表中选取一个要访问的服务;

  • RoundRobinRule:轮询

    • RandomRule:随机
    • AvailabilityFilteringRule: 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,以及并发的连接数量

超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问; * WeightedResponseTimeRule: 根据平均响应时间计算所有服务的权重,响应时间越快,服务权重越大,被选中的机率越高;
刚启动时,如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够时,会切换到WeightedResponseTimeRule * RetryRule: 先按照RoundRobinRule的策略获取服务,如果获取服务失败,则在指定时间内会进行重试,获取可用的服务; * BestAvailableRule: 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务; * ZoneAvoidanceRule: 默认规则,复合判断server所在区域的性能和server的可用性选择服务器;

1// 修改 microservicecloud-consumer-dept-80 2// ConfigBean 3@Configuration 4public class ConfigBean { 5 6 @Bean 7 @LoadBalanced 8 public RestTemplate getRestTemplate() { 9 return new RestTemplate(); 10 } 11 12 @Bean 13 public IRule myRule() { 14 return new RoundRobinRule(); // 显式的指定使用轮询算法 15 } 16} 17

1.4 自定义Ribbon的负载均衡策略

1// 修改主启动类 2@SpringBootApplication 3@EnableEurekaClient 4@RibbonClient(name="MICROSERVICECLOUD-DEPT", configuration=MySelfRule.class) // 自定义Ribbon配置类 5public class DeptConsumer80_App { 6 7 public static void main(String[] args) { 8 9 SpringApplication.run(DeptConsumer80_App.class, args); 10 } 11 12} 13 14 15// com.noodles.myrule 16// 自定义Robbin规则类 17@Configuration 18public class MySelfRule{ 19 @Bean 20 public IRule myRule(){ 21 return new RandomRule(); //自定义均衡策略 22 } 23} 24

2. Feign 负载均衡

  • Feign 是一个声明式WebService客户端:

  • 使用方法:定义一个接口,然后在上面添加注解;

2.1 创建microservicecloud-consumer-dept-feign

1// 参考 microservicecloud-consumer-dept-80 2 3// pom.xml 4<!-- Feign相关 --> 5<dependency> 6 <groupId>org.springframework.cloud</groupId> 7 <artifactId>spring-cloud-starter-feign</artifactId> 8</dependency> 9 10 11// 修改 microservicecloud-api 工程 12 // pom.xml 13 <!-- Feign相关 --> 14 <dependency> 15 <groupId>org.springframework.cloud</groupId> 16 <artifactId>spring-cloud-starter-feign</artifactId> 17 </dependency> 18 19 20 // 新建DeptClientService接口,并新增注解@FeignClient 21 @FeignClient(value="MICROSERVICECLOUD-DEPT") 22 public interface DeptClientService { 23 24 @RequestMapping(value="/dept/get/{id}", method= RequestMethod.GET) 25 public Dept get(@PathVariable("id") long id); 26 27 @RequestMapping(value="/dept/list", method= RequestMethod.GET) 28 public List<Dept> list(); 29 30 @RequestMapping(value="/dept/add", method= RequestMethod.POST) 31 public boolean add(Dept dept); 32 } 33 34 // mvn clean 35 // mvn install 36 37 38// microservice-consumer-dept-feign 工程修改Controller 39@RestController 40public class DeptController_Consumer { 41 42 @Autowired 43 private DeptClientService service; 44 45 @RequestMapping(value="/consumer/dept/get/{id}") 46 public Dept get(@PathVariable("id") Long id) { 47 return this.service.get(id); 48 } 49 50 @RequestMapping(value="/consumer/dept/list") 51 public List<Dept> list(){ 52 return this.service.list(); 53 } 54 55 @RequestMapping(value="/consumer/dept/add") 56 public Object add(Dept dept) { 57 return this.service.add(dept); 58 } 59} 60 61 62// 修改主启动类 63@SpringBootApplication 64@EnableEurekaClient 65@EnableFeignClients(basePackages = {"com.noodles.springcloud"}) 66@ComponentScan("com.noodles.springcloud") 67public class DeptConsumer80_Feign_App { 68 69 public static void main(String[] args) { 70 71 SpringApplication.run(DeptConsumer80_Feign_App.class, args); 72 } 73 74} 75

参考资料:

  • SpringCloud 教程

转载于:https://www.cnblogs.com/linkworld/p/9191161.html

代码交流 2021