网站首页 > 技术文章 正文
1.什么是Spring Cloud Consul?
Spring Cloud Consul 是 Spring Cloud 生态系统中的一个组件,它用于将 Consul 集成到 Spring Boot 应用程序中。Consul 是一个服务发现和配置管理工具,提供了服务注册、服务发现、健康检查、键值存储等功能。
Spring Cloud Consul 的主要功能包括:
- 服务注册与发现:应用程序可以在启动时将自身注册到 Consul 中,并能够发现其他已注册的服务。这对于微服务架构尤为重要,因为它允许服务动态地查找和调用彼此。
- 分布式配置:通过 Consul 的键值存储功能,Spring Cloud Consul 可以实现分布式配置管理。应用程序可以从 Consul 中获取配置信息,并在配置发生变化时自动更新。
- 健康检查:Spring Cloud Consul 支持将应用程序的健康状态报告给 Consul,以便 Consul 可以监控服务的健康状况,并在服务不可用时采取相应措施。
使用场景包括:
- 微服务架构:在微服务架构中,服务的数量和实例可能会动态变化,使用 Spring Cloud Consul 可以简化服务的注册和发现过程。
- 动态配置管理:在需要频繁更改配置的环境中,使用 Consul 的分布式配置功能可以简化配置管理,并减少应用程序重启的需求。
- 高可用性和故障恢复:通过健康检查和服务发现,Spring Cloud Consul 可以帮助实现服务的高可用性和自动故障恢复。
2.环境搭建
启动 Consul agent
docker run -d --name=dev-consul -p 8500:8500 consul
访问http://localhost:8500/ui/dc1/services
3.代码工程
实验目标
通过实践来理解和掌握微服务架构中服务注册与发现的基本概念和实现方法
调用关系图
order-service
pom.xml
<?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>spring-cloud-consul</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-service</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Cloud Starter Consul Discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
</project>
controller
package com.et.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/createOrder")
public String createOrder() {
// invoke Payment Service
String paymentResponse = restTemplate.getForObject("http://payment-service/pay", String.class);
return "Order created and " + paymentResponse;
}
}
config
package com.et.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
application.yml
spring:
application:
name: order-service
cloud:
consul:
host: localhost
port: 8500
discovery:
enabled: true
register: true
server:
port: 8080
payment-service
pom.xml
<?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>spring-cloud-consul</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>payment-service</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Cloud Starter Consul Discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
</project>
controller
package com.et.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PaymentController {
@GetMapping("/pay")
public String processPayment() {
return "Payment processed successfully!";
}
}
application.yml
spring:
application:
name: payment-service
cloud:
consul:
host: localhost
port: 8500
discovery:
enabled: true
register: true
server:
port: 8081
以上只是一些关键代码,所有代码请参见下面代码仓库
代码仓库
- https://github.com/Harries/springcloud-demo(Spring Cloud Consul )
4.测试
- 启动Order Service
- 启动Payment Service
- 访问http://127.0.0.1:8080/createOrder
- 返回调用结果Order created and Payment processed successfully!
5.引用
- https://cloud.spring.io/spring-cloud-consul/reference/html
猜你喜欢
- 2024-11-24 使用Knative部署基于Spring Native的微服务
- 2024-11-24 阿里p7大佬首次分享Spring Cloud学习笔记,带你从0搭建微服务
- 2024-11-24 ElasticSearch进阶篇之搞定在SpringBoot项目中的实战应用
- 2024-11-24 SpringCloud微服务架构实战:类目管理微服务开发
- 2024-11-24 SpringBoot+SpringCloud题目整理
- 2024-11-24 《github精选系列》——SpringBoot 全家桶
- 2024-11-24 Springboot2.0学习2 超详细创建restful服务步骤
- 2024-11-24 SpringCloud系列:多模块聚合工程基本环境搭建「1」
- 2024-11-24 Spring Cloud Contract快速入门Demo
- 2024-11-24 15年大牛用140多个实战案例深入讲解Java微服务架构文档
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 506℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 486℃MySQL service启动脚本浅析(r12笔记第59天)
- 466℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 464℃启用MySQL查询缓存(mysql8.0查询缓存)
- 444℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 423℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 419℃MySQL server PID file could not be found!失败
- 最近发表
-
- netty系列之:搭建HTTP上传文件服务器
- 让deepseek教我将deepseek接入word
- 前端大文件分片上传断点续传(前端大文件分片上传断点续传怎么操作)
- POST 为什么会发送两次请求?(post+为什么会发送两次请求?怎么回答)
- Jmeter之HTTP请求与响应(jmeter运行http请求没反应)
- WAF-Bypass之SQL注入绕过思路总结
- 用户疯狂点击上传按钮,如何确保只有一个上传任务在执行?
- 二 计算机网络 前端学习 物理层 链路层 网络层 传输层 应用层 HTTP
- HTTP请求的完全过程(http请求的基本过程)
- dart系列之:浏览器中的舞者,用dart发送HTTP请求
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)