日系嫩模《果咩酱w》放飞自我写真
如果我答应了,以后我的生活会变成什么样子?...
2023-05-12

刚学习Java时,我们编写的程序,运行时只有一个进程,这种程序就是我们常说的“单体架构”,最典型的单体架构图如下:

单体架构的软件,我们可以认为是没有架构的。
软件的架构,从单体架构,发展到垂直多程序架构、分布式架构、SOA架构,一路狂奔,到了现在的微服务架构。
虽然我们推崇微服务架构,但客观地说,单体架构依然盛行,包括很多比较大的软件,就是用单体架构来开发的,发布时也只有一个可执行程序。
单体架构之所以盛行,是因为采用其他架构时,我们除了实现业务功能,还要实现架构,例如模块之间的相互调用,这些都很复杂。
当我们使用SpringCloud开发微服务架构的软件时,会发现事情变得很轻松。我们可以方便地对软件的处理能力扩容,可以用Eureka和Ribbon,实现模块之间基于RESTful的相互调用。
说了这么多理论,估计读者都有点烦了,还是直接上代码吧。
在上一讲《Java第66讲——微服务之Eureka》文章中,详细介绍了如何建立Eureka服务器和Eureka客户端,大家可以参考,因此这里不再赘述。
我们建立的Eureka服务器的信息如下:
模块名称:register监听端口:8800注册url:http://localhost:8800/eureka/Eureka客户端将会启动三个实例,其信息如下:
模块名称:producer监听端口:三个实例监听的端口分别为9901,9902,9903应用名称:service-producer这些信息与《Java第66讲——微服务之Eureka》文章中实现的代码完全一致。
从名称来看,register是注册中心,producer是生产者,此次开发,将增加一个消费者consumer,获得producer提供的服务:

Ribbon是一个用来实现负载均衡的组件。
当我们有多个producer处于运行状态时,consumer可以根据负载情况,获得其中一个producer,然后进行调用。
使用Ribbon,我们可以根据系统当前的负载情况,决定增加或者减少producer数量。
通过Ribbon,我们轻松地实现了集群的能力。
在producer中增加一个CallController类,用于提供服务,该类的代码如下:
package com.flying.producer.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class CallController { @Value("${server.port}") private int thePort; @RequestMapping("/call_me") private String callMe(@RequestParam(value = "name") String caller){ StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Service of port ").append(thePort).append(" is called by ").append(caller); return stringBuffer.toString(); } }新增consumer模块的过程如下:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.7.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>package com.flying.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}package com.flying.consumer.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class RestTemplateCreator { @LoadBalanced @Bean RestTemplate getRestTemplate(){ return new RestTemplate(); } }package com.flying.consumer.biz;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class CallerService { @Autowired RestTemplate restTemplate; public String callProducer(){ return restTemplate.getForObject("http://SERVICE-PRODUCER/call_me?name=consumer", String.class); } }package com.flying.consumer.controller;import com.flying.consumer.biz.CallerService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController { @Autowired private CallerService callerService; @GetMapping("/call_test") public String testByExplorer(){ return callerService.callProducer(); } }server.port=10000eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/spring.application.name=service-consumer代码编写完成了,现在准备进行测试。


producer1:监听9901producer2:监听9902producer3:监听9903


这是第一次测试时显示的web页面:

刷新页面,显示的web页面为:

刷新两次页面后,显示了下面的web页面:

不敢想象,没有编写多少代码,借助于Spring Cloud,竟然轻松地实现了集群功能,实现了负载均衡功能!

以上内容就是为大家推荐的负载均衡集群如何实现(实现负载均衡集的技巧)最佳回答,如果还想搜索其他问题,请收藏本网站或点击搜索更多问题
内容来源于网络仅供参考版权声明:所有来源标注为小樱知识网www.xiaoyin02.com的内容版权均为本站所有,若您需要引用、转载,只需要注明来源及原文链接即可。
本文标题:负载均衡集群如何实现(实现负载均衡集的技巧)
本文地址:https://www.xiaoyin02.com/shcs/121468.html
相关文章
手机音效怎么调均衡器,iqoo8pro均衡器怎样调? iqoo8pro均衡器调节方法如下 iQOO手机支持Super Audio音效,就是它的声音均衡器,在扬声器模式下默认开启,可...
2023-04-01
手机耳机均衡器怎么调,五段均衡器怎么调喜欢人声? POWERAMP播放器是款非常强悍的手机播放器,很多人爱不释手,POWERAMP最大的魅力在于动手性很强,每...
2023-04-01
手机声音均衡器怎么调,供需平衡是什么原理? 供需平衡就是一种实物的供给与人们对此种实物的需求相一致。比如手机厂商生产出的手机数量与市场营销...
2023-03-31
手机cpu负载过高怎么办,手机处理器哪一种运行最快? 手机处理器哪一种运行最快? 关于这个问题说来就话长了,因为不同手机处理器品牌都有自己旗舰处...
2023-03-29
热点文章
2021年独生子女补贴新政策是真的吗(独生子女证有有效期吗)
2021年国庆节阅兵仪式几点开始几点结束(2021年国庆节还有阅兵吗)
鼠目寸光一点红是什么生肖动物(鼠目寸光一点红)指什么生肖,紧密
k0到k9的玩法大全(强制gc的玩法和注意事项)
入土为安是什么生肖《入土为安》打一个生肖动物,词语解释
浙江12月底全面停工是真的吗(浙江什么时候放假停工)
如何做t(t怎么把p做哭)
北京口碑最差的三甲医院(北京301医院最擅长什么)