基于nacos注册中心的ribbon定制规则
前面说到基于nacos的注册发现有可以扩展实现我们自己的负载均衡算法(Nacos数据模型),来实现同集群调用,是基于spring.cloud.nacos.discovery.cluster-name参数。另外基于spring.cloud.nacos.discovery.metadata参数也可以实现金丝雀发布调用。这里就使用ribbon来实现这两种负载均衡算法。
直接使用feign调用,因为feign里面就是用的ribbon,引入feign依赖也就引入了ribbon
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
1.继承com.netflix.loadbalancer.AbstractLoadBalancerRule 实现自己的负载均衡算法,从NacosDiscoveryProperties中获取实例信息,然后筛选出来cluster-name和metadata中version相同的实例,进行调用。
package com.nijunyang.order.ribbon.rule; import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.Server;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List; /**
* Description: 集群和版本规则
* Created by nijunyang on 2020/12/14 21:36
*/
@Slf4j
public class ClusterWithVersionRule extends AbstractLoadBalancerRule { @Autowired
private NacosDiscoveryProperties nacosDiscoveryProperties; /**
* 重写choose方法
* @param key
* @return
*/
@SneakyThrows
@Override
public Server choose(Object key) {
//获取服务配置的集群名
String clusterName = nacosDiscoveryProperties.getClusterName();
//当前的版本号 配置文件配置的metadata信息
String currentVersion = nacosDiscoveryProperties.getMetadata().get("version");
//获取负载均衡器
BaseLoadBalancer baseLoadBalancer = (BaseLoadBalancer) getLoadBalancer();
//调用服务的名字
String invokedServerName = baseLoadBalancer.getName();
//获取namingServer(包含nacos注册发现相关api)
NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
//获取被调用的服务的所有实例
List<Instance> invokedAllInstanceList = namingService.getAllInstances(invokedServerName);
//同集群同版本
List<Instance> theSameClusterAndVersionList = new ArrayList<>();
//跨集群同版本
List<Instance> theSameVersionList = new ArrayList<>();
for (Instance instance : invokedAllInstanceList) {
if (clusterName.equalsIgnoreCase(instance.getClusterName())
&& currentVersion.equalsIgnoreCase(instance.getMetadata().get("version"))) {
theSameClusterAndVersionList.add(instance);
} else if (currentVersion.equalsIgnoreCase(instance.getMetadata().get("version"))) {
theSameVersionList.add(instance);
}
}
Instance invokedInstance;
if (theSameClusterAndVersionList.isEmpty()) {
//跨集群同版本调用, 随机选一个
if (theSameVersionList.isEmpty()) {
throw new RuntimeException("无对应版本服务");
}
SecureRandom random = new SecureRandom();
int i = random.nextInt(theSameVersionList.size());
invokedInstance = theSameVersionList.get(i);
} else {
//同集群同版本调用 随机选一个
SecureRandom random = new SecureRandom();
int i = random.nextInt(theSameClusterAndVersionList.size());
invokedInstance = theSameClusterAndVersionList.get(i);
}
return new NacosServer(invokedInstance);
} @Override
public void initWithNiwsConfig(IClientConfig iClientConfig) { }
}
2.配置自定义的负载均衡算法
spring:
application:
name: order
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #不用写协议
# namespace: 688bf906-8b48-4ee2-a433-828f042ec860 #test id
# group: pay
cluster-name: CD
metadata:
version: v2 ribbon:
NFLoadBalancerRuleClassName: com.nijunyang.order.ribbon.rule.ClusterWithVersionRule #指定全局的负载均衡算法
eager-load:
enabled: true #饥饿加载(ribbon客户端不是在服务启动的时候加载的,所以可能第一次调用会很慢,甚至超时)
clients: stock #指定哪些服务使用饥饿加载
#按服务指定负载均衡算法
stock:
ribbon:
NFLoadBalancerRuleClassName: com.nijunyang.order.ribbon.rule.ClusterWithVersionRule
eager-load:
enabled: true
当需要指定某一服务使用某一负载均衡算法的时候,不要让spring容器扫描到该规则。在配置文件中如上配置即可。
完整代码:https://github.com/bluedarkni/study/tree/master/cloud-alibaba
基于nacos注册中心的ribbon定制规则的更多相关文章
- Spring Cloud 系列之 Alibaba Nacos 注册中心(一)
前言 从本章节开始,我们学习 Spring Cloud Alibaba 相关微服务组件. Spring Cloud Alibaba 介绍 Spring Cloud Alibaba 致力于提供微服务开发 ...
- 手动造轮子——为Ocelot集成Nacos注册中心
前言 近期在看博客的时候或者在群里看聊天的时候,发现很多都提到了Ocelot网关的问题.我之前也研究过一点,网关本身是一种通用的解决方案,主要的工作就是拦截请求统一处理,比如认证.授权.熔断. ...
- Spring Cloud 系列之 Alibaba Nacos 注册中心(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Alibaba Nacos 注册中心(一) 本篇文章讲解 Nacos 注册中心集群环境搭建. Nacos 集群环境搭建 ...
- SpringCloud Alibaba实战(7:nacos注册中心管理微服务)
源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一节我们已经完成了Nacos Server的本地部署,这一节我们学习如何将Nac ...
- Nacos注册中心之概要设计
本文已收录 https://github.com/lkxiaolou/lkxiaolou 欢迎star. 前言 在之前的文章中分析了Nacos配置中心,配置中心的核心是配置的创建.读取.推送. 注册中 ...
- Nacos注册中心和配置中心流程原理
一.Nacos注册中心 1.服务启动后---->服务注册原理 springCloud集成Nacos实现原理: 服务启动时,在spring-cloud-commons包下 spring.facto ...
- 5-2 Nacos注册中心
Nacos注册中心 什么Nacos Nacos是Spring Cloud Alibaba提供的一个软件 这个软件主要具有注册中心和配置中心的功能 我们先学习它注册中心的功能 微服务中所有项目都必须注册 ...
- Spring Cloud Alibaba 使用nacos 注册中心
### 背景 上一文我们讲到了如何去搭建注册中心,这一次我们讲述如何使用nacos作为注册中心 ### spring-cloud-alibaba-basis 创建基础依赖 首先我们创建一个spring ...
- Spring Cloud Alibaba(4)---Nacos(注册中心)
Nacos(注册中心) 有关Spring Cloud Alibaba之前写过三篇文章. Spring Cloud Alibaba(1)---入门篇 Spring Cloud Alibaba(2)--- ...
随机推荐
- MySQL第01课- CentOS + 单实例MySql编译安装总结
2016年2月,从oracle转向MySql ,碰上几个坑,特此记录 总结 1.注意环境变量.配置文件,操作过程不能出错 2.相比rpm方式安装,编译安装方式可以指定安装路径,再说安装是简单活,将来安 ...
- Hadoop大数据平台节点的动态增删
环境:CentOS 7.4 (1708 DVD) 工具:MobaXterm 一. 节点的动态增加 1. 为新增加的节点(主机)配置免密码登录.使用ssh-keygen和ssh-copy-id命令(详 ...
- 深度分析:理解Java中的多态机制,一篇直接帮你掌握!
Java中的多态 1 多态是什么 多态(Polymorphism)按字面的意思就是"多种状态".在面向对象语言中,接口的多种不同的实现方式即为多态.用白话来说,就是多个对象调用同一 ...
- Model class apps.goods.models.GoodsType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
在admin.py注册这个model时,报了个错: RuntimeError: Model class apps.goods.models.GoodsType doesn't declare an e ...
- Visual Studio使用Git忽略不想上传到远程仓库的文件
前言: 作为一个.NET开发者而已,有着宇宙最强IDE:Visual Studio加持,让我们的开发效率得到了更好的提升.我们不需要担心环境变量的配置和其他代码管理工具,因为VS有丰富的拓展工具.废话 ...
- 上位机学习技巧——c#(原创)
(一直更新.......) 一.扫描可用串口 二.捕获鼠标移到控件(上升沿)/离开控件(下降沿) 在窗体生成代码中,找到对应按钮(这里使用butten1)区域,在区域内添加两个事件,分别是: 鼠标移到 ...
- C++stl简单使用
1 //1.sort函数排序 2 /* 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; ...
- 流量控制--6.Classful Queuing Disciplines (qdiscs)
Classful Queuing Disciplines 可以使用classful qdisc的代理来解锁Linux流量控制的灵活性和控制力.classful qdisc可以附加过滤器,允许将报文重定 ...
- sqli-labs-master less01
注:如未接触过sql注入,建议观看前期知识点文章 https://www.cnblogs.com/yyd-sun/p/12256407.html 第一关步骤 一.判断注入类型(数字/字符) (1).h ...
- 在VMware下创建windows server 2008虚拟机
1.创建新的虚拟机 打开VMware软件,点击主页内创建新的虚拟机 2.进入新建虚拟机向导 点击典型,点击下一步 3.在下一步中单击稍后安装操作系统 点击下一步 4.选择操作系统类型 客户机操作系统选 ...