基于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)--- ...
随机推荐
- 20201124-web方向-命令执行-RCE
参考链接:https://www.cnblogs.com/wangtanzhi/p/12311239.html RCE: 英文全称:remote command / code execcute 分别为 ...
- ABBYY FineReader 14如何查看PDF文档
使用 ABBYY FineReader,您可以轻松查看和编辑任何类型的 PDF文档,就像是一款功能强大的PDF编辑转换器,不仅如此,它还能够允许您复制其中的文本.图片和表格.本文我们来看看如何从&qu ...
- DC靶机1-9合集
DC1 文章前提概述 本文介绍DC-1靶机的渗透测试流程 涉及知识点(比较基础): nmap扫描网段端口服务 msf的漏洞搜索 drupal7的命令执行利用 netcat反向shell mysql的基 ...
- workerman windows环境下无法启动问题
问题描述 使用laravel框架composer加载workerman/gateway-worker扩展使用workerman做客服系统.通过laravel的command命令: php artisa ...
- IDEA创建web工程(超简单)
Idea创建Web工程 以新建模块为例. 新建Maven项目 勾选[Create from artchetype] 选择[org.apache.maven.archetypes:maven-arche ...
- JUC详解--【Foam番茄】
1.什么是JUC java.util 工具包 业务:普通的线程代码 Thread Runnable 没有返回值,效率相比于 Callable 相对较低! 2.线程和进程 进程:一个程序,QQ.exe ...
- LeetCode 047 Permutations II
题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...
- 了不起的 Deno:带你极速获取各大平台今日热榜
摘要:Deno 是一个 JavaScript/TypeScript 的运行时,默认使用安全环境执行代码,有着卓越的开发体验. 有人的地方就有江湖,有江湖的地方就有争论.前些天,继<[译]为什么如 ...
- Asp.NetCore之AutoMapper基础篇
应用场景 现在由于前后端技术的分离,后端程序员在使用ORM框架开发后台API接口的时候,往往会将数据库的"数据模型"直接提供给前端.而大多数时候,可能这些数据并不能够满足前端展示的 ...
- JZOJ 2020.10.6 【NOIP2017提高A组模拟9.7】简单无向图
简单无向图 题目 Description Input Output Sample Input 输入1: 4 2 1 1 2 输入2: 10 2 2 2 2 1 1 2 1 1 2 Sample Out ...