小樱知识 > 生活常识负载均衡集群如何实现(实现负载均衡集的技巧)

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

提问时间:2022-03-16 14:26:53来源:小樱知识网


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 = "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    @Bean    RestTemplate 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 {      @Autowired    RestTemplate 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 {      @Autowired    private 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

代码编写完成了,现在准备进行测试。

6、执行测试

第1步:在Linux下建立consumer、producer1、producer2、producer3、register四个目录;

第2步:将软件传入到对应的目录,其中,producer软件同时传入producer1、producer2、producer3三个目录:

第3步:修改application.properties发文件,修改producer的监听端口:

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

第4步:启动consumer、producer1、producer2、producer3、register;

第5步:查询register的监控页面,可以看到四个客户端注册进来了:

第6步:多次查看consumer提供的Web功能,会发现每次调用的producer并不是同一个。

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

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

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

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

以上内容就是为大家推荐的负载均衡集群如何实现(实现负载均衡集的技巧)最佳回答,如果还想搜索其他问题,请收藏本网站或点击搜索更多问题

内容来源于网络仅供参考
二维码

扫一扫关注我们

版权声明:所有来源标注为小樱知识网www.xiaoyin02.com的内容版权均为本站所有,若您需要引用、转载,只需要注明来源及原文链接即可。

本文标题:负载均衡集群如何实现(实现负载均衡集的技巧)

本文地址:https://www.xiaoyin02.com/shcs/121468.html

相关文章