微信h5手机网站/网上教育培训机构
1.springcloud微服务架构搭建 之 《springboot自动装配Redis》
2.springcloud微服务架构搭建 之 《springboot集成nacos注册中心》
ribbon工作原理自己网上百度,说的都很详细
目录
1.项目引入openfeign和ribbon配置
2.新建lilock-ribbon-spring-boot-starter
3.增加restTemplate配置类
4.配置Ribbon配置类
5.配置ribbon自动装配spring.factories
6.新建一个cms业务服务,开始测试
7.配置ribbon参数
8.新建测试类进行测试
9.在user服务调用
1.项目引入openfeign和ribbon配置
openfeign集成了ribbon,需要单独给ribbon配置参数信息
<!-- openfeign 集成了ribbon,openfeign坐标引入 版本号:2.1.3.RELEASE-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>${spring.cloud.version}</version>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId><version>${spring.cloud.version}</version>
</dependency><!--配置httpclient 版本号:4.5.9-->
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>${httpclient.version}</version>
</dependency>
2.新建lilock-ribbon-spring-boot-starter
引入ribbon坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>lilock-commons</artifactId><groupId>lilock.cn</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><version>1.0-SNAPSHOT</version><artifactId>lilock-feign-spring-boot-starter</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>
3.增加restTemplate配置类
package lilock.cn.common.ribbon.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "lilock.rest-template")
@Data
public class RestTemplateProperties {/*** 最大链接数*/private int maxTotal = 300;/*** 同路由最大并发数*/private int maxPerRoute = 200;/*** 读取超时时间 ms*/private int readTimeout = 30000;/*** 链接超时时间 ms*/private int connectTimeout = 15000;/**** 最大重试次数*/private int maxAutoRetries = 0;/*** 是否支持重试*/private boolean enableRetry = false;
}
4.配置Ribbon配置类
package lilock.cn.common.ribbon.config;import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import lilock.cn.common.ribbon.properties.RestTemplateProperties;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;@EnableConfigurationProperties
public class RibbonConfig {@Autowiredprivate RestTemplateProperties restTemplateProperties;@Bean@LoadBalancedpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){RestTemplate restTemplate = new RestTemplate();//设置resttemplate http工厂restTemplate.setRequestFactory(factory);return restTemplate;}/*** 配置默认负载均衡策略-轮训* @return*/@Bean@Primarypublic IRule ribbonRule(){IRule rule = new RoundRobinRule();return rule;}@Bean@Primarypublic ClientHttpRequestFactory httpRequestFactory(HttpClient httpclient){//设置httpclientreturn new HttpComponentsClientHttpRequestFactory(httpclient);}/*** 配置httpclient 参数* @return*/@Beanpublic HttpClient httpClient(){Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);// 最大链接数connectionManager.setMaxTotal(restTemplateProperties.getMaxTotal());// 同路由并发数20connectionManager.setDefaultMaxPerRoute(restTemplateProperties.getMaxPerRoute());RequestConfig requestConfig = RequestConfig.custom()// 读超时.setSocketTimeout(restTemplateProperties.getReadTimeout())// 链接超时.setConnectTimeout(restTemplateProperties.getConnectTimeout())// 链接不够用的等待时间.setConnectionRequestTimeout(restTemplateProperties.getReadTimeout()).build();return HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).setRetryHandler(new DefaultHttpRequestRetryHandler(restTemplateProperties.getMaxAutoRetries(),restTemplateProperties.isEnableRetry())).build();}
}
5.配置ribbon自动装配spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
lilock.cn.common.ribbon.properties.RestTemplateProperties,\
lilock.cn.common.ribbon.config.RibbonConfig
6.新建一个cms业务服务,开始测试
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>lilock-modules</artifactId><groupId>lilock.cn</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><version>1.0-SNAPSHOT</version><artifactId>lilock-service-cms</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>lilock.cn</groupId><artifactId>lilock-feign-spring-boot-starter</artifactId><version>${project.version}</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
7.配置ribbon参数
server.port 可以配置不同的端口,cms服务启动2次模拟负载,我这里端口分别配置的是8990,8991
lilock:rest-template:max-total: 300 #最大连接数max-per-route: 200 #路由最大数max-auto-retries: 0 #最大重试次数enable-retry: false #是否开启重试connect-timeout: 15000 #链接超时时间read-timeout: 30000 #请求超时时间
spring:cloud:nacos:discovery:server-addr: 127.0.0.1:8848register-enabled: truenamespace: devapplication:name: lilock-service-cms
server:port: 8990
8.新建测试类进行测试
package lilock.cn.cms.controller;import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/api/cms") @Api(value = "cms接口管理",tags = {"CMS接口管理"}) @Slf4j public class CmsController {@Value("${server.port}")private Integer port;@Value("${spring.application.name}")private String service;@GetMapping("/hello")public String getHello(){return "["+ service +"]点前服务端口号是:["+ port +"]返回内容 HELLO" ;} }
9.在user服务调用
package lilock.cn.user.controller;import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping(path = "/user")
@Api(value = "系统用户",tags = {"系统用户"})
@Slf4j
public class UserController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/testTemplate")public String testTemplate(){String value = restTemplate.getForObject("http://lilock-service-cms/api/cms/hello",String.class);log.info("获取当前请求结果:{}",value);return value;}
}
可以看到8990,8991 2个端口对应的服务轮训返回,说明配置的轮训策略生效
相关文章:

3.springcloud微服务架构搭建 之 《springboot自动装配ribbon》
1.springcloud微服务架构搭建 之 《springboot自动装配Redis》 2.springcloud微服务架构搭建 之 《springboot集成nacos注册中心》 ribbon工作原理自己网上百度,说的都很详细 目录 1.项目引入openfeign和ribbon配置 2.新建lilock-ribbon-spring-boot-starter 3…...

【一】进程到底是个啥?
1. 什么是进程 进程(process):一个运行起来的程序,就是进程!,我们可以在任务管理中看到进程。 进程是操作系统进行资源分配的基本单位 2. 进程的管理 所谓的进程管理,其实就是分为两步&…...

[蓝桥杯] 双指针、BFS和DFS与图论问题
文章目录 一、日志统计 1、1 题目描述 1、2 题解关键思路与解答 二、献给阿尔吉侬的花束 2、1 题目描述 2、2 题解关键思路与解答 三、红与黑 3、1 题目描述 3、2 题解关键思路与解答 3、2、1 dfs题解代码 3、2、2 bfs题解答案 四、交换瓶子 4、1 题目描述 4、2 题解关键思路与…...

编译原理陈火旺版第四章课后题答案
下面答案仅供参考! 1.考虑下面文法G1: (1) 消去 Q 的左递归。然后,对每个非终结符,写岀不带回溯的递归子程序。 (2) 经改写后的文法是否是LL(1)的?给出它的预测分析表。 2.对下面的文法G: P→(E)lalblΛ (1)计算这个文法的每个非…...

【LeetCode】剑指 Offer(25)
目录 题目:剑指 Offer 49. 丑数 - 力扣(Leetcode) 题目的接口: 解题思路: 代码: 过啦!!! 写在最后: 题目:剑指 Offer 49. 丑数 - 力扣&…...

【数据结构】链表OJ
Yan-英杰的主页 悟已往之不谏 知来者之可追 目录 编辑 编辑二、分享:OJ调试技巧 编辑三、链表的中间结点 编辑四、链表中倒数第k个结点 一、移除链表元素 示例 1: 输入:head [1,2,6,3,4,5,6], val 6 输出:[1,2,3,4,…...

电子工程师必须掌握的硬件测试仪器,你确定你都掌握了?
目录示波器示例1:测量示波器自带的标准方波信号输出表笔认识屏幕刻度认识波形上下/左右移动上下/左右刻度参数调整通道1的功能界面捕获信号设置Menu菜单触发方式触发电平Cursor按钮捕捉波形HLEP按钮参考资料频谱分析仪器信号发生器示波器 示例1:测量示波…...

高速PCB设计指南系列(四)
第二篇 抗干扰3(部分) 3 提高敏感器件的抗干扰性能 提高敏感器件的抗干扰性能是指从敏感器件这边考虑尽量减少对干扰噪声 的拾取,以及从不正常状态尽快恢复的方法。 提高敏感器件抗干扰性能的常用措施如下: (1&…...

ODrive入门配置
目录一、驱动板说明二、安装python三、安装odrivetool四、接线五、zadig设置SimpleFOC、ODrive和VESC教程链接汇总:请点击一、驱动板说明 ODrive 硬件版本:V3.6-56V, 工作电压:12V-56V, 工作电流:60A ODri…...

快速测试两台服务器间的网速(ChatGPT回复)
如何使用iperf3测试从远程服务器下载文件速度 在进行网络性能测试时,了解服务器之间的带宽和延迟是非常重要的。iperf3是一种用于测量网络性能的工具,可以帮助我们测试从远程服务器下载文件的速度。本文将介绍如何在本地计算机上使用iperf3测试从远程服…...

彻底搞懂nodejs事件循环
nodejs是单线程执行的,同时它又是基于事件驱动的非阻塞IO编程模型。这就使得我们不用等待异步操作结果返回,就可以继续往下执行代码。当异步事件触发之后,就会通知主线程,主线程执行相应事件的回调。 以上是众所周知的内容。今天…...

Linux基础命令大全(下)
♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维课堂笔记,努力不一定有收获,但一定会有收获加油!一起努力,共赴美好人生! ♥️夕阳下,是最美的绽放࿰…...

Matplotlib从入门到精通05-样式色彩秀芳华
Matplotlib从入门到精通05-样式色彩秀芳华总结Matplotlib从入门到精通05-样式色彩秀芳华导入依赖一、matplotlib的绘图样式(style)1.matplotlib预先定义样式2.用户自定义stylesheet3.设置rcparams二、matplotlib的色彩设置(color)…...

< CSS小技巧:那些不常用,却很惊艳的CSS属性 >
文章目录👉 前言👉 一. background-clip: text - 限制背景显示(裁剪)👉 二. user-select - 控制用户能否选中文本👉 三. :focus-within 伪类👉 四. gap - 网格 / 弹性布局间隔设置👉…...

GPT-4 重磅发布,用户直呼:强得离谱
ChatGPT沉寂了一会,OpenAI 的新“核弹”又来了,GPT-4,并且它还非常擅长编码。闲话不提,直捣黄龙。 OpenAI 宣布发布 GPT-4 ChatGPT-4这是 OpenAI 努力扩展深度学习的最新里程碑,GPT-4 是一个大型多模态模型。 据悉&a…...

【JavaSE】知识点总结(3)
目录 一、类定义和使用 1. 类的定义 2. 类的实例化 3. 构造方法 构造方法的重载 二、this关键字 三、 static 修饰属性 四、封装 2. getter与setter 五、继承 1. 继承的语法 2. 子类中访问父类 3. 关于继承原则 4. super关键字 5. super和this 6. protected 关键…...

MySQL基础(三)聚合函数、子查询
目录 聚合函数 AVG/SUM/MAX/MIN COUNT函数 GROUP BY HAVING having和where的区别 SELECT的执行过程 子查询 单行子查询vs多行子查询 单行子查询 多行子查询 关联子查询 EXISTS 与 NOT EXISTS关键字 聚合函数 聚合函数作用于一组数据,并对一组数据返回一个…...

深度学习数据集处理基础内容——xml和json文件详解
文章目录一、xml文件1.1 什么是 XML?1.2XML 和 HTML 之间的差异1.3XML 不会做任何事情1.4通过 XML 您可以发明自己的标签1.5XML 不是对 HTML 的替代1.6XML 无所不在二、json文件基本的JSON结构体类型(共享部分)三、转COCO数据集3.1 info3.2 l…...

蓝桥杯基础技能训练
51单片机系统浓缩图 1. HC138译码器 用3个输入引脚,实现8个输出引脚,而且这个八个输出引脚中只要一个低电平,所以我们只需要记住真值表就行 #include "reg52.h" sbit HC138_A P2^5; sbit HC138_B P2^6; sbit HC…...

【Kubernetes】第二十八篇 - 实现自动构建部署
一,前言 上一篇,介绍了 Deployment、Service 的创建,完成了前端项目的构建部署; 希望实现:推送代码 -> 自动构建部署-> k8s 滚动更新; 本篇,实现自动构建部署 二,推送触发构…...

蓝桥杯刷题第十天
第一题:裁纸刀问题描述本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。小蓝有一个裁纸刀,每次可以将一张纸沿一条直线裁成两半。小蓝用一张纸打印出两行三列共 6 个二维码,至少使用九次裁出来…...

网络安全缓冲区溢出与僵尸网络答题分析
一、缓冲区溢出攻击 缓冲区溢出是指当计算机向缓冲区内填充数据位数时超过了缓冲区本身的容量,溢出的数据覆盖在合法数据上。理想的情况是:程序会检查数据长度,而且并不允许输入超过缓冲区长度的字符。但是绝大多数程序都会假设数据长度总是…...

机器学习:逻辑回归模型算法原理(附案例实战)
机器学习:逻辑回归模型算法原理 作者:AOAIYI 作者简介:Python领域新星作者、多项比赛获奖者:AOAIYI首页 😊😊😊如果觉得文章不错或能帮助到你学习,可以点赞👍收藏&#x…...

IO流之 File 类和字节流
文章目录一、File 类1. 概述2. 创建功能3. 删除功能4. 判断和获取功能5. 递归策略5.1 递归求阶乘5.2 遍历目录二、字节流1. IO 流概述2. 字节流写数据2.1 三种方式2.2 换行及追加2.3 加异常处理3. 字节流读数据3.1 一次读一个字节3.2 一次读一个字节数组3.3 复制文本文件3.4 复…...

【华为机试真题 Python实现】2023年1、2月高频机试题
文章目录2023年1季度最新机试题机考注意事项1. 建议提前刷题2. 关于考试设备3. 关于语言环境3.1. 编译器信息3.2. ACM 模式使用sys使用input(推荐)3. 关于题目分值及得分计算方式4. 关于做题流程5. 关于作弊2023年1季度最新机试题 两个专栏现在有200博文…...

【拳打蓝桥杯】最基础的数组你真的掌握了吗?
文章目录一:数组理论基础二:数组这种数据结构的优点和缺点是什么?三:数组是如何实现随机访问的呢?四:低效的“插入”和“删除”原因在哪里?五:实战解题1. 移除元素暴力解法双指针法2…...

断崖式难度的春招,可以get这些点
前言 大家好,我是bigsai,好久不见,甚是想念。 开学就等评审结果,还好擦边过了,上周答辩完整理材料,还好都过了(终于可以顺利毕业了),然后后面就是一直安享学生时代的晚年。 最近金三银四黄金…...

一年经验年初被裁面试1月有余无果,还遭前阿里面试官狂问八股,人麻了
最近接到一粉丝投稿:年初被裁员,在家躺平了6个月,然后想着学习下再去面试,现在面试了1个月有余,无果,天天打游戏到半夜,根本无法静下心来学习。下面是他这些天面试经常会被问到的一些问题&#…...

我从功能测试到python接口自动化测试涨到22k,谁知道我经历了什么......
目录:导读前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结(尾部小惊喜)前言 常见的接口…...

SDG,ADAM,LookAhead,Lion等优化器的对比介绍
本文将介绍了最先进的深度学习优化方法,帮助神经网络训练得更快,表现得更好。有很多个不同形式的优化器,这里我们只找最基础、最常用、最有效和最新的来介绍。 优化器 首先,让我们定义优化。当我们训练我们的模型以使其表现更好…...