实现负载均衡集的技巧 负载均衡集群如何实现


实现负载均衡集的技巧 负载均衡集群如何实现

文章插图
1、引言刚学习Java时,我们编写的程序,运行时只有一个进程,这种程序就是我们常说的“单体架构”,最典型的单体架构图如下:
单体架构的软件,我们可以认为是没有架构的 。
软件的架构,从单体架构,发展到垂直多程序架构、分布式架构、SOA架构,一路狂奔,到了现在的微服务架构 。
虽然我们推崇微服务架构,但客观地说,单体架构依然盛行,包括很多比较大的软件,就是用单体架构来开发的,发布时也只有一个可执行程序 。
单体架构之所以盛行,是因为采用其他架构时,我们除了实现业务功能,还要实现架构,例如模块之间的相互调用,这些都很复杂 。
当我们使用SpringCloud开发微服务架构的软件时,会发现事情变得很轻松 。我们可以方便地对软件的处理能力扩容,可以用Eureka和Ribbon,实现模块之间基于RESTful的相互调用 。
说了这么多理论,估计读者都有点烦了,还是直接上代码吧 。
2、建立Eureka服务器和Eureka客户端在上一讲《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提供的服务:
3、Ribbon功能介绍Ribbon是一个用来实现负载均衡的组件 。
当我们有多个producer处于运行状态时,consumer可以根据负载情况,获得其中一个producer,然后进行调用 。
使用Ribbon,我们可以根据系统当前的负载情况,决定增加或者减少producer数量 。
通过Ribbon,我们轻松地实现了集群的能力 。
4、在producer中增加一个CallController类在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 = "https://www.520longzhigu.com/diannao/name") String caller){StringBuffer stringBuffer = new StringBuffer();stringBuffer.append("Service of port ").append(thePort).append(" is called by ").append(caller);return stringBuffer.toString();}}5、新增一个模块consumer【实现负载均衡集的技巧 负载均衡集群如何实现】新增consumer模块的过程如下:
第1步:建立consumer模块,使用了Lombok、Spring Web、Eureka Discovery Client依赖:第2步:修改consumer模块的pom.xml文件,添加对Ribbon的依赖:<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>第3步:编辑入口类ConsumerApplication,添加对Eureka的使用: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);}}第4步:创建一个生成RestTemplate对象的类RestTemplateCreator,加上对Ribbon的使用: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@BeanRestTemplate getRestTemplate(){return new RestTemplate();}}第5步:创建一个业务类CallerService,使用RestTemplate实现对producer的调用: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 {@AutowiredRestTemplate restTemplate;public String callProducer(){return restTemplate.getForObject("http://SERVICE-PRODUCER/call_me?name=consumer", String.class);}}第6步:为了便于测试,添加一个测试类TestController: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 {@Autowiredprivate CallerService callerService;@GetMapping("/call_test")public String testByExplorer(){return callerService.callProducer();}}第7步:修改application.properties文件,配置监听端口为10000,以及Erueka服务器的url:server.port=10000eureka.client.serviceUrl.defaultZone=http://localhost:8800/eureka/spring.application.name=service-consumer代码编写完成了,现在准备进行测试 。


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: