Spring Boot 服务注册与发现:让你的微服务架构更强大!
【摘要】 🏆本文收录于「滚雪球学SpringBoot」专栏,手把手带你零基础入门Spring Boot,从入门到就业,助你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!@TOC环境说明:Windows 10 + IntelliJ IDEA 2021.3.2 + Jdk 1.8 📜 前言:微服务架构中的服务注册与发现在微服务架构中,服务注册与发现是非常...

🏆本文收录于「滚雪球学SpringBoot」专栏,手把手带你零基础入门Spring Boot,从入门到就业,助你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
@TOC
环境说明:Windows 10 + IntelliJ IDEA 2021.3.2 + Jdk 1.8
📜 前言:微服务架构中的服务注册与发现
在微服务架构中,服务注册与发现是非常关键的一部分。当应用的微服务数量越来越多时,手动配置每个服务的访问路径变得越来越不现实。这时候,服务注册与发现的机制就显得至关重要。
服务注册与发现帮助我们动态管理微服务之间的关系,解决了微服务实例的自动发现和访问问题。比如,当一个微服务实例上线或下线时,其他服务无需重新配置即可发现它并与其通信。
在Spring Boot应用中,我们通常使用Spring Cloud来实现这一机制,其中Eureka是最常用的服务注册与发现框架。今天,我们就来一起探索如何在Spring Boot中实现服务注册与发现,提升微服务架构的灵活性和可维护性!🚀
🛠️ 搭建Spring Boot服务注册与发现
🌱 步骤1:引入Spring Cloud Eureka依赖
首先,我们需要在Spring Boot项目中引入Spring Cloud Eureka的依赖。打开pom.xml
文件,添加如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
这条依赖会引入Eureka Server,用于服务的注册与发现。
💡 步骤2:配置Eureka Server
在项目的application.properties
中配置Eureka Server的相关参数。打开src/main/resources/application.properties
文件,添加以下配置:
# Eureka Server配置
server.port=8761
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.enable-self-preservation=false
server.port=8761
:设置Eureka Server的端口。spring.application.name=eureka-server
:指定Eureka Server的应用名称。eureka.client.register-with-eureka=false
:Eureka Server本身不需要注册到Eureka。eureka.client.fetch-registry=false
:Eureka Server不需要从其他服务获取注册信息。eureka.server.enable-self-preservation=false
:禁用自我保护模式,以便更好地调试。
🚀 步骤3:启用Eureka Server
在主类中使用@EnableEurekaServer
注解来启用Eureka Server:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
@EnableEurekaServer
注解用于启动Eureka Server,它允许你把Spring Boot应用变成一个Eureka服务注册中心。
🔥 步骤4:启动Eureka Server
运行EurekaServerApplication
,启动Eureka Server。此时,你可以访问http://localhost:8761
,查看Eureka Server的管理控制台。在控制台中,你可以看到已注册的所有微服务实例。
🛠️ 配置服务注册到Eureka
🌱 步骤1:引入Spring Cloud Eureka Client依赖
接下来,我们要在微服务客户端中配置Eureka Client,使服务能够注册到Eureka Server。首先,在pom.xml
中引入Eureka Client的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
这条依赖会引入Eureka Client,允许微服务将自己注册到Eureka Server。
💡 步骤2:配置Eureka Client
在application.properties
中,配置Eureka Client相关的参数,让微服务能够向Eureka Server注册:
# Eureka Client配置
spring.application.name=service-client
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.application.name=service-client
:指定当前微服务的名称。eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
:配置Eureka Server的地址,微服务将会向该地址注册。
🚀 步骤3:启用Eureka Client
在微服务应用的主类中,添加@EnableEurekaClient
注解来启用Eureka Client:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceClientApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceClientApplication.class, args);
}
}
@EnableDiscoveryClient
注解让该微服务成为Eureka的客户端,能够向Eureka Server注册自己,并发现其他服务。
🔥 步骤4:启动微服务并查看注册
运行ServiceClientApplication
,此时该微服务会向Eureka Server注册。在Eureka Server的管理控制台(http://localhost:8761
)中,你可以看到该微服务已经被注册,并且可以看到微服务的健康状况。
🔐 服务发现:如何通过Eureka发现其他服务?
🌱 步骤1:使用@EnableDiscoveryClient
注解
如果你需要在一个微服务中发现其他微服务,可以通过@EnableDiscoveryClient
注解启用服务发现功能。通过DiscoveryClient
,你可以在运行时动态获取其他微服务的信息。
💡 步骤2:使用RestTemplate调用其他服务
你可以使用RestTemplate
和@LoadBalanced
注解来实现服务间的调用。通过@LoadBalanced
,Spring Cloud会自动进行负载均衡,选择一个合适的微服务实例来进行调用。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.stereotype.Service;
@Service
public class ServiceDiscovery {
@Autowired
private DiscoveryClient discoveryClient;
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public String callService() {
// 通过服务名称访问其他服务
String url = "http://service-client/hello";
return restTemplate().getForObject(url, String.class);
}
}
在上述代码中,RestTemplate
通过@LoadBalanced
注解实现负载均衡,使得客户端可以通过服务名称来调用注册到Eureka Server的其他服务。
🚀 步骤3:通过服务名称访问
你可以通过服务名称来访问其他微服务。例如,http://service-client/hello
将被映射到已经注册到Eureka的service-client
服务实例。
💡 小技巧:Eureka的高级特性
-
服务健康检查
Eureka Server支持服务健康检查,可以确保只允许健康的服务实例进行注册。你可以通过spring.cloud.netflix.eureka.instance.health-check-url
来配置健康检查的URL。spring.cloud.netflix.eureka.instance.health-check-url=http://localhost:8080/actuator/health
-
自我保护模式
Eureka默认启用了自我保护模式,当Eureka Server没有接收到足够的心跳时,Eureka会认为服务实例仍然是健康的。这一功能有助于防止服务注册信息的丢失。你可以通过eureka.server.enable-self-preservation
来禁用或启用这一功能。 -
服务注销
当一个服务下线时,Eureka会自动注销它。在服务端控制台中,你可以看到服务的状态变为DOWN
,并且相关服务的实例也会被注销。
🏁 结语:让微服务架构更灵活
通过Spring Cloud Eureka,我们可以轻松实现微服务之间的服务注册与发现,大大提升了微服务架构的灵活性和可扩展性!🚀
服务注册与发现不仅能够让我们避免硬编码服务地址,还能实现负载均衡,增强系统的可靠性。结合Eureka、Spring Boot及Spring Cloud的强大功能,你可以轻松构建出一个高效、可扩展的微服务架构。💡
快来实践吧,Spring Boot与Spring Cloud Eureka,带你进入微服务的世界!🌍
🧧福利赠与你🧧
无论你是计算机专业的学生,还是对编程有兴趣的小伙伴,都建议直接毫无顾忌的学习此专栏「滚雪球学SpringBoot」,bug菌郑重承诺,凡是学习此专栏的同学,均能获取到所需的知识和技能,全网最快速入门SpringBoot,就像滚雪球一样,越滚越大, 无边无际,指数级提升。
最后,如果这篇文章对你有所帮助,帮忙给作者来个一键三连,关注、点赞、收藏,您的支持就是我坚持写作最大的动力。
同时欢迎大家关注公众号:「猿圈奇妙屋」 ,以便学习更多同类型的技术文章,免费白嫖最新BAT互联网公司面试题、4000G pdf电子书籍、简历模板、技术文章Markdown文档等海量资料。
✨️ Who am I?
我是bug菌,CSDN | 掘金 | InfoQ | 51CTO | 华为云 | 阿里云 | 腾讯云 等社区博客专家,C站博客之星Top30,华为云多年度十佳博主/价值贡献奖,掘金多年度人气作者Top40,掘金等各大社区平台签约作者,51CTO年度博主Top12,掘金/InfoQ/51CTO等社区优质创作者;全网粉丝合计 30w+;更多精彩福利点击这里;硬核微信公众号「猿圈奇妙屋」,欢迎你的加入!免费白嫖最新BAT互联网公司面试真题、4000G PDF电子书籍、简历模板等海量资料,你想要的我都有,关键是你不来拿。

-End-
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)