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 {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/createOrder")public String createOrder() {// invoke Payment ServiceString 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@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}
}
application.yml
spring:application:name: order-servicecloud:consul:host: localhostport: 8500discovery:enabled: trueregister: 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-servicecloud:consul:host: localhostport: 8500discovery:enabled: trueregister: 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.引用
- Spring Cloud Consul
- Spring Cloud Consul快速入门Demo | Harries Blog™