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

3.springcloud微服务架构搭建 之 《springboot自动装配ribbon》

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工作原理自己网上百度&#xff0c;说的都很详细 目录 1.项目引入openfeign和ribbon配置 2.新建lilock-ribbon-spring-boot-starter 3…...

【一】进程到底是个啥?

1. 什么是进程 进程&#xff08;process&#xff09;&#xff1a;一个运行起来的程序&#xff0c;就是进程&#xff01;&#xff0c;我们可以在任务管理中看到进程。 进程是操作系统进行资源分配的基本单位 2. 进程的管理 所谓的进程管理&#xff0c;其实就是分为两步&…...

[蓝桥杯] 双指针、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 题解关键思路与…...

编译原理陈火旺版第四章课后题答案

下面答案仅供参考&#xff01; 1.考虑下面文法G1: (1) 消去 Q 的左递归。然后&#xff0c;对每个非终结符&#xff0c;写岀不带回溯的递归子程序。 (2) 经改写后的文法是否是LL(1)的&#xff1f;给出它的预测分析表。 2.对下面的文法G: P→(E)lalblΛ (1)计算这个文法的每个非…...

【LeetCode】剑指 Offer(25)

目录 题目&#xff1a;剑指 Offer 49. 丑数 - 力扣&#xff08;Leetcode&#xff09; 题目的接口&#xff1a; 解题思路&#xff1a; 代码&#xff1a; 过啦&#xff01;&#xff01;&#xff01; 写在最后&#xff1a; 题目&#xff1a;剑指 Offer 49. 丑数 - 力扣&…...

【数据结构】链表OJ

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

电子工程师必须掌握的硬件测试仪器,你确定你都掌握了?

目录示波器示例1&#xff1a;测量示波器自带的标准方波信号输出表笔认识屏幕刻度认识波形上下/左右移动上下/左右刻度参数调整通道1的功能界面捕获信号设置Menu菜单触发方式触发电平Cursor按钮捕捉波形HLEP按钮参考资料频谱分析仪器信号发生器示波器 示例1&#xff1a;测量示波…...

高速PCB设计指南系列(四)

第二篇 抗干扰3&#xff08;部分&#xff09; 3 提高敏感器件的抗干扰性能 提高敏感器件的抗干扰性能是指从敏感器件这边考虑尽量减少对干扰噪声 的拾取&#xff0c;以及从不正常状态尽快恢复的方法。 提高敏感器件抗干扰性能的常用措施如下&#xff1a; &#xff08;1&…...

ODrive入门配置

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

快速测试两台服务器间的网速(ChatGPT回复)

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

彻底搞懂nodejs事件循环

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

Linux基础命令大全(下)

♥️作者&#xff1a;小刘在C站 ♥️个人主页&#xff1a;小刘主页 ♥️每天分享云计算网络运维课堂笔记&#xff0c;努力不一定有收获&#xff0c;但一定会有收获加油&#xff01;一起努力&#xff0c;共赴美好人生&#xff01; ♥️夕阳下&#xff0c;是最美的绽放&#xff0…...

Matplotlib从入门到精通05-样式色彩秀芳华

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

< CSS小技巧:那些不常用,却很惊艳的CSS属性 >

文章目录&#x1f449; 前言&#x1f449; 一. background-clip: text - 限制背景显示&#xff08;裁剪&#xff09;&#x1f449; 二. user-select - 控制用户能否选中文本&#x1f449; 三. :focus-within 伪类&#x1f449; 四. gap - 网格 / 弹性布局间隔设置&#x1f449;…...

GPT-4 重磅发布,用户直呼:强得离谱

ChatGPT沉寂了一会&#xff0c;OpenAI 的新“核弹”又来了&#xff0c;GPT-4&#xff0c;并且它还非常擅长编码。闲话不提&#xff0c;直捣黄龙。 OpenAI 宣布发布 GPT-4 ChatGPT-4这是 OpenAI 努力扩展深度学习的最新里程碑&#xff0c;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关键字 聚合函数 聚合函数作用于一组数据&#xff0c;并对一组数据返回一个…...

深度学习数据集处理基础内容——xml和json文件详解

文章目录一、xml文件1.1 什么是 XML&#xff1f;1.2XML 和 HTML 之间的差异1.3XML 不会做任何事情1.4通过 XML 您可以发明自己的标签1.5XML 不是对 HTML 的替代1.6XML 无所不在二、json文件基本的JSON结构体类型&#xff08;共享部分&#xff09;三、转COCO数据集3.1 info3.2 l…...

蓝桥杯基础技能训练

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

【Kubernetes】第二十八篇 - 实现自动构建部署

一&#xff0c;前言 上一篇&#xff0c;介绍了 Deployment、Service 的创建&#xff0c;完成了前端项目的构建部署&#xff1b; 希望实现&#xff1a;推送代码 -> 自动构建部署-> k8s 滚动更新&#xff1b; 本篇&#xff0c;实现自动构建部署 二&#xff0c;推送触发构…...

SkyWalking 10.2.0 SWCK 配置过程

SkyWalking 10.2.0 & SWCK 配置过程 skywalking oap-server & ui 使用Docker安装在K8S集群以外&#xff0c;K8S集群中的微服务使用initContainer按命名空间将skywalking-java-agent注入到业务容器中。 SWCK有整套的解决方案&#xff0c;全安装在K8S群集中。 具体可参…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销&#xff0c;平衡网络负载&#xff0c;延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

R语言AI模型部署方案:精准离线运行详解

R语言AI模型部署方案:精准离线运行详解 一、项目概述 本文将构建一个完整的R语言AI部署解决方案,实现鸢尾花分类模型的训练、保存、离线部署和预测功能。核心特点: 100%离线运行能力自包含环境依赖生产级错误处理跨平台兼容性模型版本管理# 文件结构说明 Iris_AI_Deployme…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

论文浅尝 | 基于判别指令微调生成式大语言模型的知识图谱补全方法(ISWC2024)

笔记整理&#xff1a;刘治强&#xff0c;浙江大学硕士生&#xff0c;研究方向为知识图谱表示学习&#xff0c;大语言模型 论文链接&#xff1a;http://arxiv.org/abs/2407.16127 发表会议&#xff1a;ISWC 2024 1. 动机 传统的知识图谱补全&#xff08;KGC&#xff09;模型通过…...

unix/linux,sudo,其发展历程详细时间线、由来、历史背景

sudo 的诞生和演化,本身就是一部 Unix/Linux 系统管理哲学变迁的微缩史。来,让我们拨开时间的迷雾,一同探寻 sudo 那波澜壮阔(也颇为实用主义)的发展历程。 历史背景:su的时代与困境 ( 20 世纪 70 年代 - 80 年代初) 在 sudo 出现之前,Unix 系统管理员和需要特权操作的…...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...

如何在最短时间内提升打ctf(web)的水平?

刚刚刷完2遍 bugku 的 web 题&#xff0c;前来答题。 每个人对刷题理解是不同&#xff0c;有的人是看了writeup就等于刷了&#xff0c;有的人是收藏了writeup就等于刷了&#xff0c;有的人是跟着writeup做了一遍就等于刷了&#xff0c;还有的人是独立思考做了一遍就等于刷了。…...

Yolov8 目标检测蒸馏学习记录

yolov8系列模型蒸馏基本流程&#xff0c;代码下载&#xff1a;这里本人提交了一个demo:djdll/Yolov8_Distillation: Yolov8轻量化_蒸馏代码实现 在轻量化模型设计中&#xff0c;**知识蒸馏&#xff08;Knowledge Distillation&#xff09;**被广泛应用&#xff0c;作为提升模型…...

论文阅读笔记——Muffin: Testing Deep Learning Libraries via Neural Architecture Fuzzing

Muffin 论文 现有方法 CRADLE 和 LEMON&#xff0c;依赖模型推理阶段输出进行差分测试&#xff0c;但在训练阶段是不可行的&#xff0c;因为训练阶段直到最后才有固定输出&#xff0c;中间过程是不断变化的。API 库覆盖低&#xff0c;因为各个 API 都是在各种具体场景下使用。…...