SpringCloud整合Ribbon负载均衡器

server/2024/9/23 22:30:09/

目录

一、模块一:提供数据

1.1 首先将第一个实例打包

1.2 使用命令行设置不同权重

1.3 打开图形化界面看看权重是否配置成功。

二、模块二:调用模块一

 三、修改默认负载均衡策略

 四、自定义规则

​编辑 五、完整代码

5.1 目录结构

5.2 配置文件

5.3 RestTemplateConfig.java

5.4 MyRule.java

5.5 RibbonConfig.java

5.6 GetStudentController.java

六、完整代码


一、模块一:提供数据

1.1 首先将第一个实例打包

1.2 使用命令行设置不同权重

先设置权重,再设置-jar,另外模块一的配置文件不要填权重配置,配了再用命令行不生效。

java -Dspring.cloud.nacos.discovery.weight=5 -jar --server.port=8081
Unrecognized option: --server.port=8081

同理 再开一个设置权重为2

java -Dspring.cloud.nacos.discovery.weight=2 -jar nacos-nacos-discovery1-0.0.1-SNAPSHOT.jar --server.port=8082

1.3 打开图形化界面看看权重是否配置成功。

二、模块二:调用模块一

其中为了方便,我用的上次代码 age代表权重。

 再次点击

可以看出默认采用轮播的形式

 三、修改默认负载均衡策略

/*** 修改默认负载均衡策略*/@Configuration
public class RibbonConfig {@Beanpublic IRule rule(){/** 生成随机策略**/return new RandomRule();}}

 再次访问模块一后 随机访问

采用nacos策略,会根据权重占比,来负载均衡

 四、自定义规则

1.1 MyRule.java

package com.beiyou.config;import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;public class MyRule extends AbstractLoadBalancerRule {@Autowiredprivate NacosDiscoveryProperties nacosDiscoveryProperties;//JUC包下,线程安装的Integerprivate static AtomicInteger count = new AtomicInteger(0);private Server server = null;@Override@SneakyThrowspublic Server choose(Object key) {BaseLoadBalancer loadBalancer = (BaseLoadBalancer) this.getLoadBalancer();//获取服务名称String serverName = loadBalancer.getName();NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();List<Instance> allInstances = namingService.getAllInstances(serverName, true);//6062Instance maxInstance = allInstances.stream().max(Comparator.comparing(Instance::getWeight)).get();//6061Instance minInstance = allInstances.stream().min(Comparator.comparing(Instance::getWeight)).get();int count2 = count.addAndGet(1);System.out.println(count2);int mod = count2 % 5; //取模运算  5,10,15,20,25 ...if(mod == 0){  //当次数是5的倍数的时候,mod就是0,server = new Server(minInstance.getIp(),minInstance.getPort());} else  {//mod 是 1,2,3,4 的时候打到权重高的这个机器上server = new Server(maxInstance.getIp(),maxInstance.getPort());}return server;}@Overridepublic void initWithNiwsConfig(IClientConfig iClientConfig) {}
}

返回自定义的规则

我们自定义的规则是含能除尽5就负载到权重为2的机器,否则负载到权重为5的机器。

① 使用自己的规则(两种配置方式)

 使用注解配置

@Configuration@RibbonClients(defaultConfiguration = MyRule.class) //全局设置负载规则,默认是轮询的
@RibbonClient(name = "nacos-a", configuration = MyRule.class) //针对某个服务,特殊配置
public class RibbonConfig {/*** 全局配置* 指定负载均衡策略*/@Beanpublic IRule iRule() {        //使用自己的规则        return  new MyRule();}}

Java

 基于配置文件配置,调用指定微服务提供的服务时,使用对应的负载均衡算法


#配置具体某一个服务个性化规则
nacos-b.ribbon.NFLoadBalancerRuleClassName=com.beiyou.config.MyRule##配置全局的负载均衡规则 (不生效)
default.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

Erlang

如果同时应用了以上两种方式去配置负载均衡,注解的优先级更高,则以注解为准.

因为配置文件的加载顺序在注解之前,后加载的配置会覆盖先前配置。

推荐使用配置文件,可以放进nacos,比较灵活。

 五、完整代码

5.1 目录结构

5.2 配置文件


spring.application.name = d3
server.port = 8085spring.cloud.nacos.discovery.server-addr = 192.168.11.82:8848logging.level.root = error
logging.level.com.beiyou = debug

5.3 RestTemplateConfig.java


package com.beiyou.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;/***  使用RestTemplate进行服务调用*/@Configuration
public class RestTemplateConfig {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}}

5.4 MyRule.java

package com.beiyou.config;import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;public class MyRule extends AbstractLoadBalancerRule {@Autowiredprivate NacosDiscoveryProperties nacosDiscoveryProperties;//JUC包下,线程安装的Integerprivate static AtomicInteger count = new AtomicInteger(0);private Server server = null;@Override@SneakyThrowspublic Server choose(Object key) {BaseLoadBalancer loadBalancer = (BaseLoadBalancer) this.getLoadBalancer();//获取服务名称String serverName = loadBalancer.getName();NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();List<Instance> allInstances = namingService.getAllInstances(serverName, true);//6062Instance maxInstance = allInstances.stream().max(Comparator.comparing(Instance::getWeight)).get();//6061Instance minInstance = allInstances.stream().min(Comparator.comparing(Instance::getWeight)).get();int count2 = count.addAndGet(1);System.out.println(count2);int mod = count2 % 5; //取模运算  5,10,15,20,25 ...if(mod == 0){  //当次数是5的倍数的时候,mod就是0,server = new Server(minInstance.getIp(),minInstance.getPort());} else  {//mod 是 1,2,3,4 的时候打到权重高的这个机器上server = new Server(maxInstance.getIp(),maxInstance.getPort());}return server;}@Overridepublic void initWithNiwsConfig(IClientConfig iClientConfig) {}
}

5.5 RibbonConfig.java


package com.beiyou.config;import com.alibaba.cloud.nacos.ribbon.NacosRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 修改默认负载均衡策略*/@Configuration
public class RibbonConfig {@Beanpublic IRule rule(){/** 生成随机策略**/return new MyRule();}}

5.6 GetStudentController.java


package com.beiyou.controller;import com.beiyou.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.List;@RestController
@RefreshScope
public class GetStudentController {/*** 用B应用调用A应用*/@AutowiredRestTemplate restTemplate;@GetMapping("/api/b")public List<Student> getAllStudents() {List listStudents = restTemplate.getForObject("http://d1/api/d1", List.class);return listStudents;}}

六、完整代码

云效完整代码

https://codeup.aliyun.com/62858d45487c500c27f5aab5/spring-cloud-alibaba-root.git


http://www.ppmy.cn/server/20001.html

相关文章

uniapp步骤条 组件

//子组件 <template><view class"container-step"><view class"uni-padding-wrap"><view class"progress-box"><progress :percent"3" active"true" stroke-width"2" /></vie…

JVM的运行时数据区

之前的文章有讲过&#xff0c;Java 源代码文件经过编译器编译后会生成字节码文件&#xff0c;并且经过类加载器加载之后会交给执行引擎执行&#xff0c;并在执行过程中&#xff0c;Java会划出一片空间去存储执行期间所用到的数据&#xff0c;这片空间成为运行时数据区。 根据 …

C语言 | Leetcode C语言题解之第48题旋转图像

题目&#xff1a; 题解&#xff1a; void swap(int* a, int* b) {int t *a;*a *b, *b t; }void rotate(int** matrix, int matrixSize, int* matrixColSize) {// 水平翻转for (int i 0; i < matrixSize / 2; i) {for (int j 0; j < matrixSize; j) {swap(&matr…

vue3 ——笔记 (表单输入,监听器)

表单输入 在Vue 3中&#xff0c;v-model指令的用法稍有不同于Vue 2。在Vue 3中&#xff0c;v-model指令实际上是一个语法糖&#xff0c;它会自动将value属性和input事件绑定到组件或元素上&#xff0c;以实现双向数据绑定。 在自定义组件中使用v-model时&#xff0c;需要在组…

【Pytorch】(十三)模型部署: TorchScript

文章目录 &#xff08;十三&#xff09;模型部署: TorchScriptPytorch动态图的优缺点TorchScriptPytorch模型转换为TorchScripttorch.jit.tracetorch.jit.scripttrace和script的区别总结trace 和script 混合使用保存和加载模型 &#xff08;十三&#xff09;模型部署: TorchScr…

iOS(Object C) 插入排序

插入排序的思想: 可以想象你在打牌,手里有一张牌2, 第一次摸到一张牌5; 5 比1 大,所以摸到的牌5放在1的右边; (此时手里的牌为 2->5) 第二次摸到一张牌3; 3比5小,所以3和5互换位置,再拿3和2比,3比2大,3不动(此时手里的牌为 2-> 3 -> 5) 第三次摸到一张牌1,1比5小,…

前端学习<四>JavaScript——54-原型链

常见概念 构造函数 构造函数-扩展 原型规则和示例 原型链 instanceof 构造函数 任何一个函数都可以被 new&#xff0c;new 了之后&#xff0c;就成了构造方法。 如下&#xff1a; function Foo(name, age) {this.name name;this.age age;//retrun this; //默认有这…

react 遇到的问题1 ——( 数据更新视图没更新)已解决

问题&#xff1a; 使用react开发项目时&#xff0c;使用useState 定义数据&#xff0c;通过定义的set方法修改数据&#xff0c;视图没有更新 原因&#xff1a; 在 React 中使用 useState 定义数据时&#xff0c;useState 返回一个数组&#xff0c;包含当前状态和更新的函数。…