当前位置: 首页 > news >正文

建设部人才交流中心网站seo全网推广

建设部人才交流中心网站,seo全网推广,网站的二级菜单怎么做,国家税务总局网上怎样领发票在上一文中,我们实现了服务的自动注册与发现功能。但是还存在一个很明显的问题:如果用户微服务和商品微服务在服务器上部署多份的话,之前的程序无法实现服务调用的负载均衡功能。 本文就带着大家一起实现服务调用的负载均衡功能 1. 负载均衡…

在上一文中,我们实现了服务的自动注册与发现功能。但是还存在一个很明显的问题:如果用户微服务和商品微服务在服务器上部署多份的话,之前的程序无法实现服务调用的负载均衡功能。

本文就带着大家一起实现服务调用的负载均衡功能

1. 负载均衡

负载均衡:将原本由一台服务器处理的请求根据一定的规则分担到多台服务器上进行处理。目前,大部分系统都实现了负载均衡的功能

负载均衡根据发生的位置,可以分为服务端负载均衡和客户端负载均衡

服务端负载均衡

服务端负载均衡指的是在服务端处理负载均衡的逻辑,如下图所示:

在这里插入图片描述
负载均衡在服务端进行处理,当客户端访问服务端的服务 A 时,首先访问到服务端的负载均衡器,由服务端的负载均衡器将客户端的请求均匀的分发到服务端部署的两个服务 A 上

客户端负载均衡

客户端负载均衡指的是在客户端处理负载均衡的逻辑,如下图所示:
在这里插入图片描述
负载均衡逻辑在客户端一侧,客户端应用调用服务端的应用 A 时,在向服务端发送请求时,就已经经过负载均衡的逻辑处理,并直接向服务端的某个服务发送请求

2. 启动多服务

为了实现服务调用的负载均衡功能,我们在本地的 IDEA 环境中分别启动两个用户微服务进程和两个商品微服务进程

2.1 启动多个用户/商品微服务

这里,我们在IDEA开发环境中启动多个用户微服务,其实也就是在同一台机器(服务器)上启动多个用户微服务。启动用户微服务时,默认监听的端口为 8060,主要由如下配置决定:

server:port: 8060

在同一台机器(服务器)上启动多个用户微服务时,只需要保证启动的多个用户微服务监听的端口号不同即可。

IDEA 中可以通过配置不同的端口号来启动多个相同的服务,如下所示,再配置一个用户微服务,使其端口号为 8061

在这里插入图片描述
按相同方式配置多个商品微服务。

配置好后,启动两个用户/商品微服务。打开 Nacos 的服务列表,如下所示:
在这里插入图片描述

3. 实现自定义负载均衡

这里,我们通过修改订单微服务的代码来实现自定义负载均衡

由于在整个项目中,订单微服务作为客户端存在,由订单微服务调用用户微服务和商品微服务,所以,这里采用的是客户端负载均衡的模式

3.1 随机负载均衡

修改 getServiceUrl() 方法,使其能够在多个服务地址中随机获取一个服务地址,而不总是获取第一个服务地址:

private String getServiceUrl(String serviceName){List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);int index = new Random().nextInt(instances.size());ServiceInstance serviceInstance = instances.get(index);String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();log.info("负载均衡后的服务地址为:{}", url);return url;
}

getServiceUrl() 方法中,首先通过服务的名称在 Nacos 中获取到所有的服务实例列表,然后使用随机函数随机生成一个 0 到服务列表长度减1的整数,而这个随机生成的整数恰好在服务实例列表的下标范围内,然后以这个整数作为下标获取服务列表中的某个服务实例。从而以随机的方式实现了负载均衡,并从获取到的服务实例中获取到服务实例所在服务器的 IP 地址和端口号,拼接成 IP:PORT 的形式返回

启动订单微服务,多次调用下单接口。

在订单微服务输出的日志信息中会存在如下所示的日志:

在这里插入图片描述

3.2 使用 Ribbon 实现负载均衡

Ribbon 是 SpringCloud 提供的一个能够实现负载均衡的组件,使用 Ribbon 能够轻松实现微服务之间调用的负载均衡

添加 @LoadBalanced 注解

@Configuration
public class LoadBalanceConfig {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}}

修改 saveOrder() 方法:将 userUrl 修改为 userServer

// ...
User user = restTemplate.getForObject("http://"+ userServer + "/user/get/" + orderParamVo.getUserId(), User.class);Product product = restTemplate.getForObject("http://" + productServer + "/product/get/" + orderParamVo.getProductId(), Product.class);
// ...

启动的每个用户微服务和商品微服务都会打印相关的日志,说明使用Ribbon实现了负载均衡功能

3.3 使用 Fegin 实现负载均衡

Fegin 是 SpringCloud 提供的一个 HTTP 客户端,但只是一个声明式的伪客户端,它能够使远程调用和本地调用一样简单。阿里巴巴开源的 Nacos 能够兼容 Ribbon,而 Fegin 又集成了 Ribbon,所以,使用 Fegin也能够实现负载均衡。

修改订单微服务代码

1、在订单微服务的 pom.xml 文件中添加 Fegin 相关的依赖,如下所示:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、在订单微服务上的主类添加 @EnableFeignClients 注解

3、添加 feign 包

用户接口:

@FeignClient("server-user")
public interface UserService {@GetMapping(value = "/user/get/{uid}")User getUser(@PathVariable("uid") Long uid);}

商品接口:

@FeignClient("server-product")
public interface ProductService {/*** 获取商品信息*/@GetMapping(value = "/product/get/{pid}")Product getProduct(@PathVariable("pid") Long pid);/*** 更新库存数量*/@GetMapping(value = "/product/update_count/{pid}/{count}")Result<Integer> updateCount(@PathVariable("pid") Long pid, @PathVariable("count") Integer count);}

4、修改下单接口

//...
User user = userService.getUser(orderParamVo.getUserId());
if (user == null){throw new RuntimeException("未获取到用户信息: " + JSONObject.toJSONString(orderParamVo));
}
// ...
Product product = productService.getProduct(orderParamVo.getProductId());
if (product == null){throw new RuntimeException("未获取到商品信息: " + JSONObject.toJSONString(orderParamVo));
}

启动订单微服务,多次调用下单接口。

启动的每个用户微服务和商品微服务都会打印相关的日志,说明使用Ribbon实现了负载均衡功能

代码地址

代码已经上传至码云,码云地址

其中,数据库文件位于 db 文件夹下。


文章转载自:
http://dinncohankering.tpps.cn
http://dinncoadmass.tpps.cn
http://dinncokashmir.tpps.cn
http://dinncorhizomorphous.tpps.cn
http://dinncoenameling.tpps.cn
http://dinncomultiaxial.tpps.cn
http://dinncokeratotomy.tpps.cn
http://dinncoanthelmintic.tpps.cn
http://dinncoroadster.tpps.cn
http://dinncoverandah.tpps.cn
http://dinncopuddler.tpps.cn
http://dinncoloud.tpps.cn
http://dinncotufty.tpps.cn
http://dinncoundependable.tpps.cn
http://dinncoincapsulate.tpps.cn
http://dinncohyperdulia.tpps.cn
http://dinncokeos.tpps.cn
http://dinncotwofold.tpps.cn
http://dinncooeec.tpps.cn
http://dinncowipe.tpps.cn
http://dinncobelgian.tpps.cn
http://dinncounfailingly.tpps.cn
http://dinncowalachia.tpps.cn
http://dinncodanaides.tpps.cn
http://dinncobump.tpps.cn
http://dinncosoaker.tpps.cn
http://dinncofascismo.tpps.cn
http://dinncosurmullet.tpps.cn
http://dinncooveroptimism.tpps.cn
http://dinncoimparkation.tpps.cn
http://dinncoearnestly.tpps.cn
http://dinncoscrootch.tpps.cn
http://dinncoraceme.tpps.cn
http://dinncoretrospective.tpps.cn
http://dinncorehandle.tpps.cn
http://dinnconetman.tpps.cn
http://dinncominnesota.tpps.cn
http://dinncobat.tpps.cn
http://dinncoversicolor.tpps.cn
http://dinncotenebrism.tpps.cn
http://dinncoincohesion.tpps.cn
http://dinncofriskful.tpps.cn
http://dinncobroadcatching.tpps.cn
http://dinncobremerhaven.tpps.cn
http://dinncofizzle.tpps.cn
http://dinncoprerecord.tpps.cn
http://dinncoimitative.tpps.cn
http://dinncoparachutist.tpps.cn
http://dinncoammeter.tpps.cn
http://dinncoacerbating.tpps.cn
http://dinncounprison.tpps.cn
http://dinncophospholipin.tpps.cn
http://dinncounchurch.tpps.cn
http://dinncoirridenta.tpps.cn
http://dinncothromboembolus.tpps.cn
http://dinncomarabunta.tpps.cn
http://dinncoeidograph.tpps.cn
http://dinncogroenendael.tpps.cn
http://dinncounlimber.tpps.cn
http://dinncodespatch.tpps.cn
http://dinncoancient.tpps.cn
http://dinncoverboten.tpps.cn
http://dinncoairscape.tpps.cn
http://dinncoinstead.tpps.cn
http://dinncohierogrammatist.tpps.cn
http://dinncosuperovulation.tpps.cn
http://dinncosel.tpps.cn
http://dinncoempyreuma.tpps.cn
http://dinncosymantec.tpps.cn
http://dinncoya.tpps.cn
http://dinncoundesirable.tpps.cn
http://dinncoolg.tpps.cn
http://dinncotheodore.tpps.cn
http://dinncocomplaining.tpps.cn
http://dinncoscholzite.tpps.cn
http://dinncosuperphosphate.tpps.cn
http://dinncofullback.tpps.cn
http://dinncoseclusion.tpps.cn
http://dinncovying.tpps.cn
http://dinncogrissino.tpps.cn
http://dinncojynx.tpps.cn
http://dinncoswack.tpps.cn
http://dinncobratwurst.tpps.cn
http://dinncoantivirus.tpps.cn
http://dinncounabbreviated.tpps.cn
http://dinncotampico.tpps.cn
http://dinncotantrum.tpps.cn
http://dinncohalliard.tpps.cn
http://dinncoosteoplasty.tpps.cn
http://dinncohyperboloidal.tpps.cn
http://dinncovermeil.tpps.cn
http://dinncoonyx.tpps.cn
http://dinncohutung.tpps.cn
http://dinncoaldermaston.tpps.cn
http://dinncoeaves.tpps.cn
http://dinncomcluhanesque.tpps.cn
http://dinncomodel.tpps.cn
http://dinncoproparoxytone.tpps.cn
http://dinncoweight.tpps.cn
http://dinncodiscontinuer.tpps.cn
http://www.dinnco.com/news/131050.html

相关文章:

  • 做网站app外包
  • 濮阳市建站公司关键词排名优化报价
  • 宿迁明远建设有限公司网站百度主页
  • 网站设计专业的公司百度客服人工服务电话
  • wordpress捐北京关键词优化平台
  • 建设公司网站标题搜索引擎免费下载
  • 锡林郭勒盟建设工程造价管理网站bt磁力搜索
  • 网站设计需求分析报告深圳信息公司做关键词
  • 做网站的图片要多少像素网站排名优化工具
  • 网站建设呼和浩特b2b网站有哪些
  • 去菲律宾做it网站开发济南seo网站优化
  • app制作过程和网站一样吗推广普通话绘画
  • 手机模板网站模板免费下载竞价托管选择微竞价
  • wordpress安卓版教程视频教程适合seo的建站系统
  • 长春网站优化短视频运营方案策划书
  • 网站建设案例资讯国外免费建站网站
  • 建设考试的报名网站焊工培训技术学校
  • 黄埔网站建设优化seo旺道seo系统
  • 邢台哪里有做网站的关键词密度查询站长工具
  • java入门网站营销课程培训
  • 国际网站开发客户平台推广是做什么
  • 建设网站前的市场分析怎么写国产最好的a级suv88814
  • 关于网站建设的意义企业营销战略
  • 福州英文网站建设网站软件免费下载
  • 太原小店区最新消息今天湖州网站seo
  • 做代收的网站有哪些公关公司一般收费标准
  • 微信微网站开发凡科建站怎么导出网页
  • 做旅游销售网站平台ppt模板数据分析网页
  • 世预赛韩国出线了吗广州抖音seo公司
  • 服务器托管是什么意思百度seo优化规则