Springboot 集成 Consul 实现服务注册中心-05

因为后续很多模块都要用到注册中心,所以此处先实现此模块。

Consul简介

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

Consul的核心功能

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

Consul的主要应用场景

  • 服务发现:作为注册中心,服务地址被注册到Consul中以后,可以使用Consul提供的DNS、HTTP接口查询。
  • 服务隔离:Consul支持以服务为单位设置访问策略,能同时支持经典的平台和新兴的平台,支持TLS证书分发,service-to-service加密。
  • 服务配置:Consul提供key-value数据存储功能,并且能将变动迅速地通知出去,通过工具consul-template可以更方便地实时渲染配置文件。

Consul还提供了丰富的配置选项和API接口,以满足不同的使用需求。它易于安装和部署,并且支持多种操作系统和平台。由于其强大的功能和灵活的配置选项,Consul已经成为许多分布式系统中不可或缺的一部分。

使用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模块

此模块实现向服务中心注册。
在这里插入图片描述

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中显示的服务标识名称,全局唯一。

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.");
		return BaseResponse.success("hello world");
	}
}

Consul测试

启动Springboot服务,会在 http://localhost:8500中看到如下界面:
在这里插入图片描述

源码下载

涉及模块:

  • springcloud-consul-server:18093, 待注册的服务模块

源码下载:

源码运行方法:

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 04:30:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 04:30:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 04:30:03       82 阅读
  4. Python语言-面向对象

    2024-05-13 04:30:03       91 阅读

热门阅读

  1. XMl发展前景及相关领域

    2024-05-13 04:30:03       31 阅读
  2. 第1章 信息系统综合知识 1.4 IT战略

    2024-05-13 04:30:03       31 阅读
  3. HTML学习笔记汇总

    2024-05-13 04:30:03       34 阅读
  4. skynet - spinlock 简单的自旋锁

    2024-05-13 04:30:03       37 阅读
  5. 【C++风云录】跨界融合:纺织工程与材料科学

    2024-05-13 04:30:03       23 阅读
  6. Node 学习-1

    2024-05-13 04:30:03       29 阅读
  7. 高并发场景

    2024-05-13 04:30:03       27 阅读
  8. 理解Python的装饰器 decorator

    2024-05-13 04:30:03       32 阅读