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

网站空间控制面板珠海网站制作推广公司

网站空间控制面板,珠海网站制作推广公司,电商网站设计价格,以公司做网站其实在项目中直接使用ribbon时不多#xff0c;大多是使用feign的#xff0c;其实feign底层也是通过ribbon构建的#xff0c;主要记忆一下计算规则#xff0c;ribbon的源码还是很不错的#xff0c;还是值得学习的。 1、添加pom dependencygroupIdorg.spr…其实在项目中直接使用ribbon时不多大多是使用feign的其实feign底层也是通过ribbon构建的主要记忆一下计算规则ribbon的源码还是很不错的还是值得学习的。 1、添加pom dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-ribbon/artifactId /dependency2、启动类注解 我没有在启动类添加注解直接另外一个配置类Configuration添加 3、添加配置 # 默认是轮训都有随机、加权响应时间、重试看具体业务和服务器搭配 #eureka-client.ribbon.NFLoadBalancerRuleClassNamecom.netflix.loadbalancer.RoundRobinRule4、参照源码规则自己编写规则 这里是通过hashcode在hash闭环定义的当每台服务器hashcode在hash闭环的对应位置后每次请求直接定位到hash中顺时针寻找最近的服务器 package com.xl.ribbon.consumer.rules;import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractLoadBalancerRule; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.Server; import lombok.NoArgsConstructor; import org.springframework.util.CollectionUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.SortedMap; import java.util.TreeMap;NoArgsConstructor public class MyRule extends AbstractLoadBalancerRule implements IRule {Overridepublic void initWithNiwsConfig(IClientConfig iClientConfig) {}Overridepublic Server choose(Object key) {HttpServletRequest request ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();String uri request.getServletPath() ? request.getQueryString();return route(uri.hashCode(), getLoadBalancer().getAllServers());}public Server route(int hashId, ListServer servers) {if (CollectionUtils.isEmpty(servers)) {return null;}TreeMapLong, Server serverMap new TreeMap();servers.forEach(server - {// 虚化若干个服务节点到环上for (int i 0; i 8; i) {long hash hash(server.getId() i);serverMap.put(hash, server);}});long hash hash(String.valueOf(hashId));SortedMapLong, Server last serverMap.tailMap(hash);// 当request URL的hash值大于任意一个服务器对应的hashKey// 取serverMap中的第一个节点if (last.isEmpty()) {Server value serverMap.firstEntry().getValue();last.put(hash, value);}return last.get(last.firstKey());}public long hash(String key) {MessageDigest md5;try {md5 MessageDigest.getInstance(MD5);} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);}byte[] keyBytes null;try {keyBytes key.getBytes(UTF-8);} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}md5.update(keyBytes);byte[] digest md5.digest();long hash (digest[2] 0xFF 16) | (digest[1] 0xFF) 8 | (digest[0] 0xFF);return hash 0xffffffffL;} } 5、规则配置 其实直接配置在application.properties也可以这里我是在一个配置文件 package com.xl.ribbon.consumer;import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import com.xl.ribbon.consumer.rules.MyRule; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** Created by lig on 2024/10/12.*/ Configuration //RibbonClient(name eureka-client, configuration com.netflix.loadbalancer.RandomRule.class) RibbonClient(name eureka-client, configuration MyRule.class) public class RibbonConfiguration { // // Bean // public IRule defaultLBStrategy() { // return new RandomRule(); // }} 6、接口 package com.xl.ribbon.consumer;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;/*** Created by lig on 2024/10/12.*/ RestController public class RibbonController {Autowiredprivate RestTemplate restTemplate;GetMapping(/sayHi)public String sayHi() {return restTemplate.getForObject(http://eureka-client/sayHi, String.class);}} 总结 其实在实际项目中ribbon编写符合自己业务的规则还是挺复杂的用途还是挺广的.
http://www.tj-hxxt.cn/news/218593.html

相关文章:

  • 怎么查询网站是否收录营销策划书范文案例
  • 阿里云建设网站好不好电商平台模板
  • 企业自建站注册域名费用
  • 怎样更换网站cms深圳网站搭建专业公司
  • 凡科建网站十大网络平台有哪些
  • wordpress中搜索页面模板关键词优化分析工具
  • 企智网站建设死链接对网站的影响
  • 石家庄市住房城乡建设局网站天津网站app建设
  • 上海网网站建设长沙哪个公司做网站好
  • 怎样做商城网站的推广logo图片设计
  • 合肥建站免费模板dw建设网站的代码模板
  • 法律问题咨询哪个网站做的好网页设计作品聚散
  • 300网站建设开发一个网站一般需要多少钱
  • 网站服务器如何管理关键词seo服务
  • 电脑商业网站怎的做做企业网站大概多少钱
  • 做网站济南西wordpress 文章 相册
  • 沈阳 建设工程 招标中心网站《企业宣传片制作》
  • 兰州市住房和城乡建设局网站手机网站一键分享
  • 网站没有内容 能做优化吗php购物网站开发uml图
  • 邵阳做网站哪个公司好敏捷软件开发流程
  • 网页游戏直接玩爱站网seo工具查询
  • 黑龙江省深圳网站优化公司哪家好
  • 网站建设类的手机软件荣耀手机商城官方网
  • 三亚网络网站建设传奇网页链接
  • 杭州网站建设hzfwwl宁波seo推广如何收费
  • 电脑上买wordpress雪军miui一键优化
  • 餐饮网站建设案例公司登记
  • 酒店网站建设功能一叶子电子商务网站建设策划书
  • 怎么做公司网站竞价预约网免费建站流程
  • 保定建设招聘信息网站wordpress 用户接口