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

网站建设项目招标标书重庆网站建设 优化

网站建设项目招标标书,重庆网站建设 优化,线上推广策划方案范文,wordpress用旧的编辑器本文已收录于专栏 《中间件合集》 目录 概念说明什么是RibbonRibbon和Nginx负载均衡的区别 工作流程代码实现RibbonSDK发送请求端引入RibbonSDK和Nacos的依赖配置文件中填写负载均衡策略调用代码 接收请求端执行效果发送请求端接收请求端 总结提升 概念说明 什么是Ribbon Ribb…本文已收录于专栏 《中间件合集》 目录 概念说明什么是RibbonRibbon和Nginx负载均衡的区别 工作流程代码实现RibbonSDK发送请求端引入RibbonSDK和Nacos的依赖配置文件中填写负载均衡策略调用代码 接收请求端执行效果发送请求端接收请求端 总结提升 概念说明 什么是Ribbon Ribbon 是一个客户端负载均衡器它是Spring Cloud Netflix开源的一个组件用于在分布式系统中实现对服务实例的负载均衡。它可以作为一个独立的组件使用也可以与 Spring Cloud 等微服务框架集成使用。   Ribbon 的主要功能是根据一定的负载均衡策略将客户端请求分配到可用的服务实例上以提高系统的可用性和性能。它通过周期性地从服务注册中心如 Eureka获取可用的服务实例列表并根据配置的负载均衡策略选择合适的实例来处理请求。Ribbon 支持多种负载均衡策略如轮询、随机、加权随机、加权轮询等。 Ribbon和Nginx负载均衡的区别 工作流程 客户端发起请求到 Ribbon。Ribbon 从服务注册中心获取可用的服务实例列表。根据配置的负载均衡策略选择一个合适的服务实例。将请求转发给选中的服务实例进行处理。如果请求失败或超时Ribbon 会尝试选择其他的服务实例进行重试。 代码实现 RibbonSDK sdk是每个使用ribbon的服务中需要引入的jar包需要借助jar包中的功能来完成ribbon的使用。 package com.example.ribbonsdk.config.test;import com.example.client.Controller.SDKController; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import org.springframework.http.*; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder;import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random;/*** BelongsProject: ribbonDemo* BelongsPackage: com.example.ribbonsdk.config* Author: Wuzilong* Description: RibbonSDK* CreateTime: 2023-07-31 22:47* Version: 1.0*/ Component public class RequestInterceptor implements ClientHttpRequestInterceptor, ApplicationContextAware {public static ApplicationContext applicationContext;int index 0;// 目前是写死的应该放到注册中心中去动态的添加注册服务和权重public MapString,Integer serverList new HashMap(){{put(localhost:9002,7); // 权重值为7put(localhost:9005,3); // 权重值为3}};Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {if (this.applicationContext null) {this.applicationContext applicationContext;}}/*** Author:Wuzilong* Description: 手动注入AnnotationConfigApplicationContext用于判断* CreateTime: 2023/6/19 17:36* param:* return:**/Beanpublic AnnotationConfigApplicationContext annotationConfigApplicationContext() {return new AnnotationConfigApplicationContext();}Overridepublic ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {System.out.println(拦截器拦截进来了拦截的地址是request.getURI());RestTemplate restTemplate new RestTemplate();//获取服务名String serveName request.getURI().getAuthority();String newAuthority null;Environment environment applicationContext.getBean(Environment.class);String loadBalanceName environment.getProperty(ribbon.loadBalanceName);if (loadBalanceName.equals(polling)){newAuthority this.polling(serveName);System.out.println(采用的是负载均衡策略————轮询);}else if (loadBalanceName.equals(weight)){newAuthority this.weight();System.out.println(采用的是负载均衡策略————权重);}String newHost newAuthority.split(:)[0];String newPort newAuthority.split(:)[1];URI newUri UriComponentsBuilder.fromUri(request.getURI()).host(newHost).port(newPort).build().toUri();RequestEntity tRequestEntity new RequestEntity(HttpMethod.GET, newUri);ResponseEntityString exchange restTemplate.exchange(tRequestEntity, String.class);System.out.println(请求的服务是exchange.getBody());// 创建一个ClientHttpResponse对象并将实际的响应内容传递给它ClientHttpResponse response new ClientHttpResponse() {Overridepublic HttpStatus getStatusCode() {return exchange.getStatusCode();}Overridepublic int getRawStatusCode() {return exchange.getStatusCodeValue();}Overridepublic String getStatusText() {return exchange.getBody();}Overridepublic void close() {}Overridepublic InputStream getBody() {return new ByteArrayInputStream(exchange.getBody().getBytes());}Overridepublic HttpHeaders getHeaders() {return exchange.getHeaders();}};return response;}//轮询获取服务的IP地址public String polling(String serverName){ListString pollingList applicationContext.getBean(SDKController.class).getList(serverName);String ipContext pollingList.get(index);index(index1)%pollingList.size();return ipContext;}//权重获取服务的IP地址public String weight() {int totalWeight serverList.values().stream().mapToInt(Integer::intValue).sum();int randomWeight new Random().nextInt(totalWeight); // 生成一个随机权重值int cumulativeWeight 0; // 累计权重值for (Map.EntryString,Integer server : serverList.entrySet()) {cumulativeWeight server.getValue();if (randomWeight cumulativeWeight) {return server.getKey();}}return null; // 没有找到合适的服务器}} RequestInterceptor 类实现了两个接口一个是ClientHttpRequestInterceptor用来重写intercept方法也就是说重写了拦截器中的业务逻辑我们可以把拦截到的请求进行处理处理的过程可以写到intercept方法中另一个是ApplicationContextAware这个接口是用来获取bean容器中对象的。 发送请求端 引入RibbonSDK和Nacos的依赖 dependencygroupIdcom.example/groupIdartifactIdRibbonSDK/artifactIdversion1.0-SNAPSHOT/version/dependency!-- 手写nacos的sdk用来获取注册列表--dependencygroupIdcom.example/groupIdartifactIdClient/artifactIdversion2.5-20230615.123611-1/version/dependencyNacos的其他配置可参考手写Naocs注册中心基本原理  手写Nacos配置中心基本原理 配置文件中填写负载均衡策略 ribbon:loadBalanceName: polling调用代码 import com.example.ribbonsdk.config.test.RequestInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;/*** BelongsProject: ribbonDemo* BelongsPackage: com.example.ribbonsdk.service* Author: Wuzilong* Description: 请求端* CreateTime: 2023-08-28 08:20* Version: 1.0*/ Service public class ServiceA {Autowiredprivate RequestInterceptor requestInterceptor;public void getServiceInfo(){String url http://localhost/B/receiveMessage/;RestTemplate restTemplatenew RestTemplateBuilder().build();restTemplate.getInterceptors().add(requestInterceptor);ResponseEntityString forEntity restTemplate.getForEntity(url, String.class);if (forEntity.getStatusCode() HttpStatus.OK) {System.out.println(调用B服务成功);}} }import com.example.ribbonsdk.service.ServiceA; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;/*** BelongsProject: ribbonDemo* BelongsPackage: com.example.ribbonsdk.Controller* Author: Wuzilong* Description: 描述什么人干什么事儿* CreateTime: 2023-07-31 22:54* Version: 1.0*/ RestController RequestMapping(/ribbonsdk) public class ServiceAController {Autowiredprivate ServiceA serviceA;RequestMapping(valuegetInfo,method RequestMethod.GET)public void getInfo(){serviceA.getServiceInfo();} }接收请求端 import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.net.InetAddress; import java.net.UnknownHostException;/*** BelongsProject: ServiceB* BelongsPackage: com.example.serviceb.Controller* Author: Wuzilong* Description: B服务* CreateTime: 2023-06-07 19:08* Version: 1.0*/ RestController RequestMapping(/B) public class ServiceBController {Value(${server.port})private String serverPort;GetMapping(/receiveMessage)public String receiveMessage() throws UnknownHostException {System.out.println(B我被调用了);//返回的内容是ip地址和端口号return InetAddress.getLocalHost().getHostAddress():serverPort;} }执行效果 发送请求端 接收请求端 总结提升 Ribbon 是一个强大的客户端负载均衡器可以帮助构建可靠和高性能的分布式系统。它通过负载均衡策略将请求分发到多个服务实例上提供了灵活的配置选项和额外的功能。 此文章对你有用的话记得留言点赞收藏哦
http://www.huolong8.cn/news/17342/

相关文章:

  • 重庆市建设考试报名网站百度做广告多少钱
  • 做空间的网站快手刷热度推广网站
  • 抖音代运营套餐价格表网站根目录文件 seo
  • 阿克苏市建设局网站易语言 做网站
  • 空间链接制作网站怎么创办自己的网站
  • 网站建设及维护招聘ui图标素材网站
  • 公司网站建设团队动画制作视频
  • 百度站长工具添加网站验证网络营销的实现方式有哪些?
  • 建设银行积分网站用illustrator做网站
  • 免费开放服务器seo整站优化外包公司
  • 做教育网站宣传策略策划公司网站设计
  • 正能量网站不用下载直接进入企业网站制作流程图
  • 天津企业设计网站建设音乐网站网页设计
  • 高校网站如何建设大江网站建设
  • 南京做公司网站公司wordpress 照片墙
  • 网站推广方案范例广告设计制作税率
  • 网站做跳转辽宁工程建设信息网官网入口
  • 网站推广基本方法是简单网页制作模板免费
  • 域名网站注册最划算重庆建设部网站
  • 湘潭网站建设方案案例怎样用dw做 网站首页
  • 做直播网站收费吗下载网站系统源码
  • 网站用户粘度重要的龙岗网站建设
  • 哪个公司做网站建设好hao123主页下载安装
  • 高邮网站建设永久云服务器免费领
  • 网站后台更新后主页没有变化muiteer主题 wordpress
  • 外贸网站哪家好建电子商务网站需要多少钱
  • 西安做网站广告的公司网页版梦幻西游金色伙伴推荐
  • 怎么做自己的销售网站app推广刷量
  • 专做商品折扣的网站wordpress google字体删除
  • 溧阳市建设局网站6婚纱摄影 网站关键词 优化