亲宝软件园·资讯

展开

spring cloud Ribbon 软负载均衡 详解spring cloud中使用Ribbon实现客户端的软负载均衡

牛奋lch 人气:0
想了解详解spring cloud中使用Ribbon实现客户端的软负载均衡的相关内容吗,牛奋lch在本文为您仔细讲解spring cloud Ribbon 软负载均衡的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,cloud,Ribbon,软负载均衡,springcloud,负载均衡,下面大家一起来学习吧。

开篇

本例是在springboot整合H2内存数据库,实现单元测试与数据库无关性和使用RestTemplate消费springhttps://m.qb5200.com/boot的Restful服务两个示例的基础上改造而来

在使用RestTemplate来消费springhttps://m.qb5200.com/boot的Restful服务示例中,我们提到,调用springhttps://m.qb5200.com/boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一种,一旦ip地址发生了变化,都需要改动程序,并重新部署服务,使用Ribbon的时候,可以有效的避免这个问题。

前言:

软负载均衡的实现方式有两种,分别是服务端的负载均衡和客户端的负载均衡

服务端负载均衡:当浏览器向后台发出请求的时候,会首先向反向代理服务器发送请求,反向代理服务器会根据客户端部署的ip:port映射表以及负载均衡策略,来决定向哪台服务器发送请求,一般会使用到nginx反向代理技术。

客户端负载均衡:当浏览器向后台发出请求的时候,客户端会向服务注册器(例如:Eurekahttps://m.qb5200.com/Server),拉取注册到服务器的可用服务信息,然后根据负载均衡策略,直接命中哪台服务器发送请求。这整个过程都是在客户端完成的,并不需要反向代理服务器的参与。

一、启动Eurekahttps://m.qb5200.com/Server

请参考该例:springhttps://m.qb5200.com/cloud中启动Eurekahttps://m.qb5200.com/Serverhttps://m.qb5200.com/

二、启动微服务,并注册到Eurekahttps://m.qb5200.com/Server上

springhttps://m.qb5200.com/cloud-将springhttps://m.qb5200.com/boot服务注册到Eurekahttps://m.qb5200.com/Server上

为了演示负载均衡的效果,再启动一个为服务,注意需要将端口号改成不一致

三、添加Ribbon支持

1、添加Ribbon的依赖

2、添加负载均衡支持

packagehttps://m.qb5200.com/com.chhliu.springboot.restful;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.beans.factory.annotation.Autowired;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.boot.SpringApplication;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.boot.autoconfigure.SpringBootApplication;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.boot.web.client.RestTemplateBuilder;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.cloud.client.loadbalancer.LoadBalanced;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.cloud.netflix.eureka.EnableEurekaClient;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.context.annotation.Bean;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.web.client.RestTemplate;https://m.qb5200.com/ @SpringBootApplicationhttps://m.qb5200.com/ @EnableEurekaClienthttps://m.qb5200.com/ publichttps://m.qb5200.com/classhttps://m.qb5200.com/SpringbootRestTemplateApplicationhttps://m.qb5200.com/{https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/@Autowiredhttps://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/privatehttps://m.qb5200.com/RestTemplateBuilderhttps://m.qb5200.com/builder;https://m.qb5200.com/ https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/@Beanhttps://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/@LoadBalancedhttps://m.qb5200.com///https://m.qb5200.com/添加负载均衡支持,很简单,只需要在RestTemplate上添加@LoadBalanced注解,那么RestTemplate即具有负载均衡的功能,如果不加@LoadBalanced注解的话,会报java.net.UnknownHostException:springboot-h2异常,此时无法通过注册到Eurekahttps://m.qb5200.com/Server上的服务名来调用服务,因为RestTemplate是无法从服务名映射到ip:port的,映射的功能是由LoadBalancerClient来实现的。https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/publichttps://m.qb5200.com/RestTemplatehttps://m.qb5200.com/restTemplate()https://m.qb5200.com/{https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/returnhttps://m.qb5200.com/builder.build();https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/}https://m.qb5200.com/ https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/publichttps://m.qb5200.com/statichttps://m.qb5200.com/voidhttps://m.qb5200.com/main(String[]https://m.qb5200.com/args)https://m.qb5200.com/{https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/SpringApplication.run(SpringbootRestTemplateApplication.class,https://m.qb5200.com/args);https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/}https://m.qb5200.com/ }https://m.qb5200.com/

3、修改调用微服务的URL

packagehttps://m.qb5200.com/com.chhliu.springboot.restful.controller;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.beans.factory.annotation.Autowired;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.web.bind.annotation.GetMapping;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.web.bind.annotation.PathVariable;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.web.bind.annotation.RestController;https://m.qb5200.com/ importhttps://m.qb5200.com/org.springframework.web.client.RestTemplate;https://m.qb5200.com/https://m.qb5200.com/ importhttps://m.qb5200.com/com.chhliu.springboot.restful.vo.User;https://m.qb5200.com/ https://m.qb5200.com/ @RestControllerhttps://m.qb5200.com/ publichttps://m.qb5200.com/classhttps://m.qb5200.com/RestTemplateControllerhttps://m.qb5200.com/{https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/@Autowiredhttps://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/privatehttps://m.qb5200.com/RestTemplatehttps://m.qb5200.com/restTemplate;https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/@GetMapping("/template/{id}")https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/publichttps://m.qb5200.com/Userhttps://m.qb5200.com/findById(@PathVariablehttps://m.qb5200.com/Longhttps://m.qb5200.com/id)https://m.qb5200.com/{//https://m.qb5200.com/将原来的ip:port的形式,改成注册到Eurekahttps://m.qb5200.com/Server上的应用名即可https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/Userhttps://m.qb5200.com/uhttps://m.qb5200.com/=https://m.qb5200.com/this.restTemplate.getForObject("http://springboot-h2/user/"https://m.qb5200.com/+https://m.qb5200.com/id,https://m.qb5200.com/User.class);https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/System.out.println(u);https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/https://m.qb5200.com/returnhttps://m.qb5200.com/u;https://m.qb5200.com/ https://m.qb5200.com/https://m.qb5200.com/}https://m.qb5200.com/ }https://m.qb5200.com/

四、查看Eurekahttps://m.qb5200.com/Server状态

五,在浏览器中,多次刷新http://localhost:7904/template/2地址

六、测试结果

7900端口服务:

Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/ Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/ Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/ Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/

7901端口服务:

Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/ Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/ Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/ Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/ Hibernate:https://m.qb5200.com/selecthttps://m.qb5200.com/user0_.idhttps://m.qb5200.com/ashttps://m.qb5200.com/id1_0_0_,https://m.qb5200.com/user0_.agehttps://m.qb5200.com/ashttps://m.qb5200.com/age2_0_0_,https://m.qb5200.com/user0_.balancehttps://m.qb5200.com/ashttps://m.qb5200.com/balance3_0_0_,https://m.qb5200.com/user0_.namehttps://m.qb5200.com/ashttps://m.qb5200.com/name4_0_0_,https://m.qb5200.com/user0_.usernamehttps://m.qb5200.com/ashttps://m.qb5200.com/username5_0_0_https://m.qb5200.com/fromhttps://m.qb5200.com/userhttps://m.qb5200.com/user0_https://m.qb5200.com/wherehttps://m.qb5200.com/user0_.id=?https://m.qb5200.com/

7904端口服务:

Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ 2017-01-23https://m.qb5200.com/09:58:05.682https://m.qb5200.com/INFOhttps://m.qb5200.com/7436https://m.qb5200.com/---https://m.qb5200.com/[erListUpdater-0]https://m.qb5200.com/c.netflix.config.ChainedDynamicPropertyhttps://m.qb5200.com/:https://m.qb5200.com/Flippinghttps://m.qb5200.com/property:https://m.qb5200.com/springboot-h2.ribbon.ActiveConnectionsLimithttps://m.qb5200.com/tohttps://m.qb5200.com/usehttps://m.qb5200.com/NEXThttps://m.qb5200.com/property:https://m.qb5200.com/niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimithttps://m.qb5200.com/=https://m.qb5200.com/2147483647https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/ Userhttps://m.qb5200.com/[id=2,https://m.qb5200.com/username=user2,https://m.qb5200.com/name=李四,https://m.qb5200.com/age=20,https://m.qb5200.com/balance=100.00]https://m.qb5200.com/

从上面的测试结果可以看出,总共调了7904端口服务9次,其中7904端口服务调7900端口服务4次,调7901端口5次,刚好是9次

经过上面的几个步骤,就基本使用Ribbon实现了客户端负载均衡的功能

加载全部内容

相关教程
猜你喜欢
用户评论