• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Spring Cloud Ribbon实现客户端负载均衡的示例

java 搞代码 4年前 (2022-01-05) 22次浏览 已收录 0个评论

本篇文章主要介绍了Spring Cloud Ribbon实现客户端负载均衡的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

前面我们已经完成了注册中心和服务提供者两个基础组件。本文就介绍使用Spring Cloud Ribbon在客户端负载均衡的调用服务。

 对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题。在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如F5,或者是基于软件的,如VS、HAproxy等。LB上有所有服务的地址映射表,当服务消费者调用某个目标服务时,它先向LB发起请求,由LB以某种策略(比如:Round-Robin)做负载均衡后将请求转发到目标服务。

而微服务的出现,则为LB的实现提供了另外一种思路:把LB的功能以库的方式集成到服务消费方的进程内,而不是由一个集中的设备或服务器提供。这种方案称为软负载均衡(Soft Load Balancing)或者客户端负载均衡。在Spring Cloud中配合Eureka的服务注册功能,Ribbon子项目则为REST客户端实现了负载均衡。

使用Spring Cloud Ribbon实现服务消费者

新建spring-cloud-sample-tutorial-consumer项目

在spring-cloud-sample-tutorial下新建spring-cloud-sample-tutorial-consumer子项目

添加ribbon和eureka依赖

   org.springframework.cloudspring-cloud-starter-eureka org.springframework.cloudspring-cloud-starter-ribbon org.springframework.bootspring-boot-starter-web

配置applicatioin.properties,注册中心地址

 spring.application.name=consu<span style="color:transparent">来源gaodai#ma#com搞*!代#%^码网</span>mer server.port=30001 eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/

编写UserController,添加@LoadBalanced注解,启用Ribbon负载均衡

 @Controller @RequestMapping("user") public class UserController { @Bean @LoadBalanced RestTemplate initRestTemplate(){ return new RestTemplate(); } @Autowired private RestTemplate restTemplate; @RequestMapping("add") @ResponseBody public String add(String userName, String age){ return restTemplate.getForEntity("http://PRODUCER/user/add",String.class,userName,age).getBody(); } } 

编写ConsumerApplication,添加@EnableEurekaClient,启用服务注册

 @EnableEurekaClient @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); } } 

集群部署Producer

为了模拟集群的Producer,在producer项目中新建application-profile1.properties和application-profile2.properties。

application-profile1.properties

 spring.application.name=producer server.port=20001 eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/

application-profile2.properties

 spring.application.name=producer server.port=20002 eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/

为了测试负载的效果,我们把被调用服务的端口打出来

 @Controller @RequestMapping("user") public class UserController { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private DiscoveryClient client; @RequestMapping("add") @ResponseBody public String addUser(String userName, String age) { return "Success from " + client.getLocalServiceInstance().getHost() + ":" + client.getLocalServiceInstance().getPort() ; } } 

启动测试

启动注册中心

分别配置Active Profiles为profile1和profile2,启动两次,完成注册中心集群的服务启动。

启动服务提供者

按上面同样的方法,启动服务提供者。

启动服务消费者

服务提供者单机就可以了,正常启动ConsumerApplication即可。

验证

从浏览器输入http://localhost:30001/user/add

再次访问:

可以看到,我们的负载均衡调用服务,已经成功了,默认是按轮训的方式做负载均衡。

总结

本文介绍并完成了使用Spring Cloud Ribbon进行客户端负载均衡的调用。

接下来我们继续介绍怎么使用Spring Cloud进行服务监控。

源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Spring Cloud Ribbon实现客户端负载均衡的示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring Cloud Ribbon实现客户端负载均衡的示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址