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

微信公众号的h5网站开发asp学校网站源码

微信公众号的h5网站开发,asp学校网站源码,优质企业网站推广,wordpress 页面下载文件其实在项目中直接使用ribbon时不多&#xff0c;大多是使用feign的&#xff0c;其实feign底层也是通过ribbon构建的&#xff0c;主要记忆一下计算规则&#xff0c;ribbon的源码还是很不错的&#xff0c;还是值得学习的。 1、添加pom <dependency><groupId>org.spr…

其实在项目中直接使用ribbon时不多,大多是使用feign的,其实feign底层也是通过ribbon构建的,主要记忆一下计算规则,ribbon的源码还是很不错的,还是值得学习的。

1、添加pom

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

2、启动类注解

我没有在启动类添加注解,直接另外一个配置类@Configuration添加

3、添加配置

# 默认是轮训,都有随机、加权响应时间、重试,看具体业务和服务器搭配
#eureka-client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

4、参照源码规则,自己编写规则

这里是通过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, List<Server> servers) {if (CollectionUtils.isEmpty(servers)) {return null;}TreeMap<Long, 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));SortedMap<Long, 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.yayakq.cn/news/891161/

相关文章:

  • 做移动网站优化快wordpress 获取pageid
  • 广州市网站建设制作设计平台做网站分流
  • 网页设计素材网站推荐wordpress 文章 顺序
  • 西安网站建设-中国互联中国十大采购平台app
  • 手机网站开发怎么样手机网站开发一个多少钱
  • 方维网络的品牌网站建设产品线上推广渠道
  • 汕尾招聘 网站建设合伙人wordpress优化版源码
  • 自己想做个网站需要多少钱无锡装饰网站建设排名
  • 做损坏文档的网站天津市建设网站
  • 电商模板网站免费汕头市城乡与住房建设局
  • 博物馆网站制作长治网站制作教程
  • 广州网站建设报价单网站开发市场前景
  • 廊坊企业网站团队做app的公司有哪些
  • 深圳网站开发哪家公司好免费做网站的
  • 网站建设系统公司地址郑州互联网公司排行榜
  • 外语网站制作wordpress加载视频
  • 郑州建设厅网站建网站服务商
  • 建设银行报网站wordpress 加载速度优化
  • 网站服务器排名天津商城网站建设
  • 如何跟进网站建设的客户网站首页制作案例
  • 建设化妆品网站的成本有哪里可以做兼职翻译的网站
  • 云南文投建设投资有限公司网站比较好的国外网站建设公司
  • 网站建设页面美工学做网站 空间 域名
  • 网站logo图怎么做的企业营销网站建设费用
  • seo网站营销推广全程实例pdf做物流哪个网站货源多
  • wordpress站点演示三网合一建站价格
  • 做网站的语言有哪些wordpress多语言主页
  • 阿里巴巴国际站运营工作内容做网站在阿里云买什么软件
  • 情趣官方网站怎么做代理网站质量度
  • 加盟网站制作费用wordpress悬浮