Springboot集成Netflix-ribbon、Consul实现负载均衡调用-09

Consul简介

Consul是一个开源的服务发现和配置管理工具,具有跨平台、运行高效等特点。它由HashiCorp公司开发,并使用Go语言编写。Consul主要用于实现分布式系统中的服务发现、健康检查、键值存储等功能。

核心功能

  1. 服务发现:Consul通过DNS或HTTP接口实现服务发现,允许开发人员轻松地注册、发现和注销服务,从而实现高可用和负载均衡。
  2. 健康检查:Consul具备强大的健康检查功能,可以监控服务的状态并根据设定的规则自动剔除故障节点。它支持多种健康检查方式,如TCP、HTTP、Docker容器等,确保服务的稳定性和可用性。
  3. 分布式一致性:Consul使用Raft算法作为其分布式一致性协议,确保在分布式环境下数据的一致性和可靠性。它实现了强一致性模型,能够处理网络分区、故障恢复等场景,保证系统的可靠性。
  4. 键值存储:Consul提供了一个简单但功能强大的键值存储接口,开发人员可以使用这个接口来存储和检索配置数据、共享状态信息等。它支持事务操作和ACL权限控制,为分布式系统的配置管理提供了便利。

使用HomeBrew安装Consl

根据系统不同会有不同的安装方法,笔者是在macos下进行的的安装,所以安装方法如下,Consl软件可从地址: Consl软件包下载 下载。

安装

brew tap hashicorp/tap
brew install hashicorp/tap/consul

运行

consul --version
consul agent -dev

访问

打开网址 : http://localhost:8500
在这里插入图片描述

涉及的模块

  • springcloud-consul-server, provider服务提供者, 端口号:18093,18094
  • springcloud-consul-client, consumer服务消费者, 端口号:18093
    在这里插入图片描述

springcloud-consul-server 服务provider模块

在这里插入图片描述

pom.xml

依赖版本可参考 springbootSeries 模块中pom.xml文件中的版本定义

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

application.properties配置

spring.profiles.active = dev
spring.application.name=springbootConsulServer
server.port=18093

spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}

spring.cloud.consul.discovery.service-name值为在Consl中显示的服务标识名称,全局唯一。

application-server2.properties配置

实现另一个模块配置,实现集群效果。

#springboot Server
spring.application.name=springbootConsulServer
spring.aop.auto=true
spring.aop.proxy-target-class=true

# log4j
logging.config=classpath:log4j2.xml
logging.level.root=INFO
logging.level.org.springframework.web=ERROR

#restful Server
server.port=18094
server.compression.enabled=true
server.compression.mime-types=application/json,application/octet-stream

#swagger
springdoc.api-docs.enabled=true
springdoc.api-docs.path=/api-schema

swagger-config.group = default-group
swagger-config.description= The following is a restful-api list of {} application, and you can browse or test them to determine if they are working as you expect.
swagger-config.version=V1.0
swagger-config.urlPattern=/**
swagger-config.base-package=com.korgs
swagger-config.authorization-key-name=token
swagger-config.wiki = https://korgs.blog.csdn.net/

spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}

spring.cloud.consul.discovery.service-name值为在Consl中显示的服务标识名称,全局唯一。

SpringbootApplication启动类

添加 @EnableDiscoveryClient 注解

@Slf4j
@SpringBootApplication(scanBasePackages = {"com.korgs",  "cn.hutool.extra.spring"})
@Configuration
@EnableConfigurationProperties
@ServletComponentScan
@RestController
@EnableDiscoveryClient
public class SpringbootConsulServerApplication {

	@Value("${server.port}")
	private String serverPort;

	public static void main(String[] args) {
		SpringApplication.run(SpringbootConsulServerApplication.class, args);
	}

	@GetMapping("/helloworld")
	public BaseResponse helloWorld(){
		log.info(LogGenerator.trackLog()
				+ "msg="+ "I am busy to handle this request."
				+ "port=" + serverPort
		);
		return BaseResponse.success("hello world:" + serverPort);
	}
}

springcloud-consul-client 服务Consumer 模块

pom.xml

依赖版本可参考 springbootSeries 模块中pom.xml文件中的版本定义

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    </dependencies>

application.properties配置

service-url.ribbon-service= springbootConsulServer名称为provider注册到consul上面的服务名称。

spring.profiles.active = dev
spring.application.name=springbootConsulClient
server.port=18095

spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}

#自定义配置服务端
service-url.ribbon-service = http://springbootConsulServer

SpringbootApplication启动类

@Slf4j
@SpringBootApplication(scanBasePackages = {"com.korgs",  "cn.hutool.extra.spring"})
@Configuration
@EnableConfigurationProperties
@ServletComponentScan
@RestController
@EnableDiscoveryClient
public class SpringbootConsulClientRibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootConsulClientRibbonApplication.class, args);
	}

	@GetMapping("/helloworld")
	public BaseResponse helloWorld(){
		log.info( LogGenerator.trackLog()
				+ "msg=" + "I am busy to handle this request.");
		return BaseResponse.success("hello world");
	}
}

RestTemplate Bean 实现

注意添加@LoadBalanced注解,这个是负载均衡实现的关键,代码如下:

@Configuration
public class RibbonConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

测试Controller实现

@Value("${service-url.ribbon-service}")指向application.properties中的配置

@RestController
@RequestMapping("/api/load")
public class LoadBalanceController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${service-url.ribbon-service}")
    private String ribbonServiceUrl;

    @GetMapping("/v1/hello-content")
    public BaseResponse<String> loadHelloContent(String uuid){
        String result =  restTemplate.getForObject(ribbonServiceUrl + "/helloworld", String.class, uuid);

        return BaseResponse.success(result);
    }
}

源码下载

涉及模块:

  • springcloud-consul-server, provider服务提供者, 端口号:18093,18094
  • springcloud-consul-client, consumer服务消费者, 端口号:18093

源码下载:

  • 基础框架源码下载
  • Springboot集成Netflix-ribbon、Consul实现负载均衡调用

源码运行方法:

  • 模块详细功能说明和运行测试方法

springcloud-consul-server ,分别用18093,18094启动

测试界面如下,可观察后台日志,也可查看返回结构的端口号。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/608660.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

JavaScript百炼成仙自学笔记——13

函数七重关之六&#xff08;“new”一个函数&#xff09; 看个代码&#xff1a; function hello(){console.log(this); } 1、this&#xff1a;也是JavaScript中的一个关键字&#xff0c;永远指向当前函数的调用者 解释一下,有两层意思&#xff1a; ①this要嘛不出现&#…

基于SSM的“环卫工管理平台”的设计与实现(源码+数据库+文档+PPT)

基于SSM的“环卫工管理平台”的设计与实现&#xff08;源码数据库文档PPT) 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SSM 工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 系统总体设计图 注册 首页 后台登录 后台页面 环卫工管理 摘…

mac安装 php7.1/apache

1. 安装php 7.1 brew tap shivammathur/php 查看可安装版本 brew search php 安装指定版本&#xff08;禅道适用PHP运行环境(7.0/7.1/7.2版本)&#xff09; brew install php7.1 环境配置 vim ~/.zshrc export PATH"/usr/local/opt/php7.1/bin:$PATH" export …

使用Postman进行接口测试---解析postman页面

一、Postman 是一款流行的 API 测试工具&#xff0c;它提供了丰富的功能来帮助开发者测试和调试 API。以下是 Postman 页面上的主要功能及其含义和作用&#xff1a; 1. 请求详情&#xff08;Request Details&#xff09; &#xff1a; - 方法&#xff08;Method&#xff0…

【读论文】Gaussian Grouping: Segment and Edit Anything in 3D Scenes

Gaussian Grouping: Segment and Edit Anything in 3D Scenes 文章目录 Gaussian Grouping: Segment and Edit Anything in 3D Scenes1. What2. Why3. How3.1 Anything Mask Input and Consistency3.2 3D Gaussian Rendering and Grouping3.3 Downstream: Local Gaussian Editi…

第二篇【AI与传奇开心果系列】Python的AI技术点库案例示例:详解AI工业应用算法原理

AI与传奇开心果系列博文 系列博文目录Python的AI技术点库案例示例系列 博文目录前言一、AI工业应用算法原理介绍二、机器学习在工业领域的应用算法示例代码三、深度学习算法在工业领域应用示例代码四、强化学习在工业领域应用示例代码五、自然语言处理在工业领域应用示例代码六…

C语言 变量的作用域

今天 我们来说变量的作用域和存储类型 每种事物 都有自己作用的范围限制 例如 汽车只能在路上跑 轮船只能在海洋 飞机只能通行于天空 函数的参数 也只有在函数被调用过程中分配内存资源 函数执行结束 空间也会被立即释放 这也说明了 行参变量只有在函数内才有效 离开了该函数 …

【JavaEE】博客系统(前端页面设计)

文章目录 一、预期效果二、实现博客列表页 一、预期效果 二、实现博客列表页 实现导航栏 编辑 blog_list.html, 创建导航栏的 html 代码. 导航栏里面包含 logo, 标题, 以及一些按钮(跳转链接). 为了实现左右排列, 在 logo 和 按钮 之间加一个 spacer 作为占位器. <!-- 导航…

初次查询大数据信用报告,需要注意哪些问题?

随着大数据的普及&#xff0c;基于大数据技术的大数据信用也变得越来越重要&#xff0c;比如在申贷之前&#xff0c;不少地方都会查询申贷人的大数据信用&#xff0c;作为风险控制的必要手段&#xff0c;那对于初次查询大数据信用报告的人来说&#xff0c;需要注意哪些问题呢?…

引领AI数据标注新纪元:景联文科技为智能未来筑基

在人工智能蓬勃发展的今天&#xff0c;数据如同燃料&#xff0c;驱动着每一次技术飞跃。在这场智能革命的浪潮中&#xff0c;景联文科技凭借其深厚的专业实力与前瞻性的战略眼光&#xff0c;正站在行业前沿&#xff0c;为全球的人工智能企业提供坚实的数据支撑。 全国布局&…

vscode的git插件使用教程

虽然git的命令我没有滚瓜烂熟&#xff0c;但vscode的git插件是尊嘟很好用啊&#xff0c;都被我用烂了。在网上看见一个讲的很不错的插件教程。借鉴一下。并在一些地方用块引用进行了补充说明&#xff01; 跳过了vscode安装过程。 克隆GitHub中的存储库&#xff1a; 1、复制Gi…

python3有serial库吗

一、概述 pyserial模块封装了对串口的访问。 二、特性 在支持的平台上有统一的接口。 通过python属性访问串口设置。 支持不同的字节大小、停止位、校验位和流控设置。 可以有或者没有接收超时。 类似文件的API&#xff0c;例如read和write&#xff0c;也支持readline等…

探案录 | KingbaseES+SqlSugar为医疗用户排忧解难

在2024年的初春&#xff0c;某大型三甲医院的CT预约系统上线测试&#xff0c;如同新芽破土&#xff0c;充满了希望与活力。然而&#xff0c;仅仅两天后&#xff0c;一个技术难题如同迷雾中的幽灵&#xff0c;悄然出现&#xff1a;The connection pool has been exhausted…… 福…

5.07 Pneumonia Detection in Chest X-Rays using Neural Networks

肺炎诊断是一个耗时的过程&#xff0c;需要高技能的专业人员分析胸部X光片chest X-ray (CXR)&#xff0c;并通过临床病史、生命体征和实验室检查确认诊断。 它可以帮助医生确定肺部感染的程度和位置。呼吸道疾病在 X 光片上表现为一处膨胀的不透明区域。然而&#xff0c;由于不…

STM32 ADC学习

ADC Analog-to-Digital Converter&#xff0c;即模拟/数字转换器 常见ADC类型 分辨率和采样速度相互矛盾&#xff0c;分辨率越高&#xff0c;采样速率越低。 ADC的特性参数 分辨率&#xff1a;表示ADC能辨别的最小模拟量&#xff0c;用二进制位数表示&#xff0c;比如8,10…

OpenAI 正在开发一种可以防止版权诉讼的工具

OpenAI 正在开发一种名为 Media Manager 的工具&#xff0c;该工具将使内容创建者和所有者能够确定他们是否愿意将自己的内容用于 ML 研究和 AI 模型训练。 Media Manager 将做什么&#xff1f; 明年推出的 Media Manager 将使内容创作者和所有者能够更好地控制他们的工作是否…

C语言初阶(6) - 指针

目录 1.指针是什么&#xff1f; 2. 指针和指针类型 2.1 指针 - 整数 2.2 指针的解引用 3. 野指针 3.1 野指针成因 3.2 如何规避野指针 4. 常量指针和指针常量 (const) 4.1.常量指针 4.2.指针常量 5. 指针运算 5.1 指针-整数 5.2 指针-指针 5.3指针的关系运算 6.…

Vitis HLS 学习笔记--理解串流Stream(2)

目录 1. 简介 2. 极简的对比 3. 硬件模块的多次触发 4. 进一步探讨 do-while 5. 总结 1. 简介 在这篇博文中《Vitis HLS 学习笔记--AXI_STREAM_TO_MASTER-CSDN博客》&#xff0c;我分享了关于 AXI Stream 接口的实际应用案例。然而&#xff0c;尽管文章中提供了代码示例&…

如何向Linux内核提交开源补丁?

2021年&#xff0c;我曾经在openEuler社区上看到一项改进Linux内核工具的需求&#xff0c;因此参与过Linux内核社区的开源贡献。贡献开源社区的流程都可以在内核社区文档中找到&#xff0c;但是&#xff0c;单独学习需要一个较长的过程&#xff0c;新手难以入门&#xff0c;因此…

分享四种免费获取SSL的方式

SSL证书目前需要部署安装的网站很多&#xff0c;主要还是基于国内目前对证书的需求度在不断的升高&#xff0c;网站多了、服务器多了之后。网络安全问题就成为了大家不得不面对的一个重要的问题了。SSL证书的作用有很多&#xff0c;这里就不一一详述了&#xff0c;本期作品主要…