欢迎大家来到IT世界,在知识的湖畔探索吧!
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
欢迎大家来到IT世界,在知识的湖畔探索吧!
访问http://localhost:8500/ui/dc1/services
欢迎大家来到IT世界,在知识的湖畔探索吧!
3.代码工程
实验目标
通过实践来理解和掌握微服务架构中服务注册与发现的基本概念和实现方法
调用关系图
order-service
pom.xml
欢迎大家来到IT世界,在知识的湖畔探索吧!
spring-cloud-consul
com.et
1.0-SNAPSHOT
4.0.0
order-service
17
17
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-consul-discovery
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
欢迎大家来到IT世界,在知识的湖畔探索吧!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
欢迎大家来到IT世界,在知识的湖畔探索吧!
spring-cloud-consul
com.et
1.0-SNAPSHOT
4.0.0
payment-service
17
17
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-consul-discovery
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
欢迎大家来到IT世界,在知识的湖畔探索吧!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
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/88295.html