当前位置: 首页 > 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;推送触发构…...

springboot 百货中心供应链管理系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;百货中心供应链管理系统被用户普遍使用&#xff0c;为方…...

进程地址空间(比特课总结)

一、进程地址空间 1. 环境变量 1 &#xff09;⽤户级环境变量与系统级环境变量 全局属性&#xff1a;环境变量具有全局属性&#xff0c;会被⼦进程继承。例如当bash启动⼦进程时&#xff0c;环 境变量会⾃动传递给⼦进程。 本地变量限制&#xff1a;本地变量只在当前进程(ba…...

rknn优化教程(二)

文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK&#xff0c;开始写第二篇的内容了。这篇博客主要能写一下&#xff1a; 如何给一些三方库按照xmake方式进行封装&#xff0c;供调用如何按…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

视频字幕质量评估的大规模细粒度基准

大家读完觉得有帮助记得关注和点赞&#xff01;&#xff01;&#xff01; 摘要 视频字幕在文本到视频生成任务中起着至关重要的作用&#xff0c;因为它们的质量直接影响所生成视频的语义连贯性和视觉保真度。尽管大型视觉-语言模型&#xff08;VLMs&#xff09;在字幕生成方面…...

C# SqlSugar:依赖注入与仓储模式实践

C# SqlSugar&#xff1a;依赖注入与仓储模式实践 在 C# 的应用开发中&#xff0c;数据库操作是必不可少的环节。为了让数据访问层更加简洁、高效且易于维护&#xff0c;许多开发者会选择成熟的 ORM&#xff08;对象关系映射&#xff09;框架&#xff0c;SqlSugar 就是其中备受…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

是否存在路径(FIFOBB算法)

题目描述 一个具有 n 个顶点e条边的无向图&#xff0c;该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序&#xff0c;确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数&#xff0c;分别表示n 和 e 的值&#xff08;1…...

MySQL账号权限管理指南:安全创建账户与精细授权技巧

在MySQL数据库管理中&#xff0c;合理创建用户账号并分配精确权限是保障数据安全的核心环节。直接使用root账号进行所有操作不仅危险且难以审计操作行为。今天我们来全面解析MySQL账号创建与权限分配的专业方法。 一、为何需要创建独立账号&#xff1f; 最小权限原则&#xf…...

快刀集(1): 一刀斩断视频片头广告

一刀流&#xff1a;用一个简单脚本&#xff0c;秒杀视频片头广告&#xff0c;还你清爽观影体验。 1. 引子 作为一个爱生活、爱学习、爱收藏高清资源的老码农&#xff0c;平时写代码之余看看电影、补补片&#xff0c;是再正常不过的事。 电影嘛&#xff0c;要沉浸&#xff0c;…...