SpringCloud(11)之Ribbon负载均衡配置规则

╰半橙微兮° 2022-12-02 13:54 225阅读 0赞

一、Ribbon是什么?

Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随即连接等)去连接这些机器。

二、4种方式使用Ribbon

①使用注解@loadBalanced

1、添加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>

2、application启动类

  1. /**
  2. * @author liangshaofeng
  3. */
  4. @SpringBootApplication
  5. public class ConsumerMovieApplication {
  6. @Bean
  7. @LoadBalanced
  8. public RestTemplate restTemplate() {
  9. return new RestTemplate();
  10. }
  11. public static void main(String[] args) {
  12. SpringApplication.run(ConsumerMovieApplication.class, args);
  13. }
  14. }

3、**controller**

  1. /**
  2. * @author liangshaofeng
  3. */
  4. @RequestMapping("/movies")
  5. @RestController
  6. public class MovieController {
  7. @Autowired
  8. private RestTemplate restTemplate;
  9. @GetMapping("/users/{id}")
  10. public User findById(@PathVariable Long id) {
  11. // 这里用到了RestTemplate的占位符能力
  12. User user = this.restTemplate.getForObject(
  13. "http://microservice-provider-user/users/{id}",
  14. User.class,
  15. id
  16. );
  17. // ...电影微服务的业务...
  18. return user;
  19. }
  20. }

4、启动提供者多个相同名服务类,ribbon的服务

这样就起到负载均衡的效果

②通过配置java文件实现

1、@Configuration配置类

  1. /**
  2. * 使用RibbonClient,为特定的目标服务自定义配置。
  3. * 使用@RibbonClient的configuration属性,指定Ribbon的配置类。
  4. * 可参考的示例:
  5. * http://spring.io/guides/gs/client-side-load-balancing/
  6. *
  7. * @author 梁少锋
  8. */
  9. @Configuration
  10. @RibbonClient(name = "microservice-provider-user", configuration = RibbonConfiguration.class)
  11. public class TestConfiguration {
  12. }
  13. /**
  14. * 该类为Ribbon的配置类
  15. * 注意:该类不能放在主应用程序上下文@ComponentScan所扫描的包中,否则配置将会被所有Ribbon Client共享。
  16. * @author 梁少锋
  17. */
  18. @Configuration
  19. public class RibbonConfiguration {
  20. @Bean
  21. public IRule ribbonRule() {
  22. // 负载均衡规则,改为随机
  23. return new RandomRule();
  24. }
  25. }

2、application启动类

  1. /**
  2. * @author liangshaofeng
  3. */
  4. @SpringBootApplication
  5. public class ConsumerMovieApplication {
  6. @Bean
  7. @LoadBalanced
  8. public RestTemplate restTemplate() {
  9. return new RestTemplate();
  10. }
  11. public static void main(String[] args) {
  12. SpringApplication.run(ConsumerMovieApplication.class, args);
  13. }
  14. }

3、controller

  1. /**
  2. * @author liangshaofeng
  3. */
  4. @RequestMapping("/movies")
  5. @RestController
  6. public class MovieController {
  7. @Autowired
  8. private RestTemplate restTemplate;
  9. @GetMapping("/users/{id}")
  10. public User findById(@PathVariable Long id) {
  11. // 这里用到了RestTemplate的占位符能力
  12. User user = this.restTemplate.getForObject(
  13. "http://microservice-provider-user/users/{id}",
  14. User.class,
  15. id
  16. );
  17. // ...电影微服务的业务...
  18. return user;
  19. }
  20. }

③通过yml配置

1、同上controller**,application**

2、yml

  1. server:
  2. port: 8010
  3. spring:
  4. application:
  5. # 指定注册到eureka server上的服务名称
  6. name: microservice-consumer-movie
  7. eureka:
  8. client:
  9. service-url:
  10. # 指定eureka server通信地址,注意/eureka/小尾巴不能少
  11. defaultZone: http://localhost:8761/eureka/
  12. instance:
  13. # 是否注册IP到eureka server,如不指定或设为false,那就会注册主机名到eureka server
  14. prefer-ip-address: true
  15. microservice-provider-user:
  16. ribbon:
  17. NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

④Ribbon和Hystrix结合使用

1、application启动类

  1. /**
  2. * @author liangshaofeng
  3. */
  4. @SpringBootApplication
  5. @EnableCircuitBreaker //启动断路器 配置本应用将使用服务注册和服务发现
  6. public class ConsumerMovieApplication {
  7. @Bean
  8. @LoadBalanced
  9. public RestTemplate restTemplate() {
  10. return new RestTemplate();
  11. }
  12. public static void main(String[] args) {
  13. SpringApplication.run(ConsumerMovieApplication.class, args);
  14. }
  15. }

2、controller

  1. /**
  2. * @author liangshaofeng
  3. */
  4. @RequestMapping("/movies")
  5. @RestController
  6. @Slf4j
  7. public class MovieController {
  8. @Autowired
  9. private RestTemplate restTemplate;
  10. //此注解表示此方法是hystrix方法,其中fallbackMethod定义回退方法的名称
  11. @HystrixCommand(fallbackMethod = "findByIdFallback")
  12. @GetMapping("/users/{id}")
  13. public User findById(@PathVariable Long id) {
  14. // 这里用到了RestTemplate的占位符能力
  15. User user = this.restTemplate.getForObject(
  16. "http://microservice-provider-user/users/{id}",
  17. User.class,
  18. id
  19. );
  20. // ...电影微服务的业务...
  21. return user;
  22. }
  23. public User findByIdFallback(Long id, Throwable throwable) {
  24. log.error("进入回退方法", throwable);
  25. return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1));
  26. }
  27. }

3、yml配置

  1. server:
  2. port: 8010
  3. spring:
  4. application:
  5. # 指定注册到eureka server上的服务名称
  6. name: microservice-consumer-movie
  7. eureka:
  8. client:
  9. service-url:
  10. # 指定eureka server通信地址,注意/eureka/小尾巴不能少
  11. defaultZone: http://localhost:8761/eureka/
  12. instance:
  13. # 是否注册IP到eureka server,如不指定或设为false,那就会注册主机名到eureka server
  14. prefer-ip-address: true
  15. management:
  16. endpoint:
  17. health:
  18. show-details: always
  19. endpoints:
  20. web:
  21. exposure:
  22. include: 'hystrix.stream'

以上就是整合Ribbon的4种实现方式!加油!!!一起学习交流讨论!

发表评论

表情:
评论列表 (有 0 条评论,225人围观)

还没有评论,来说两句吧...

相关阅读