客户端注册
1. 添加依赖
首先需要做的是将 Spring Eureka 依赖项添加到组织服务和许可证服务的 pom.xml 文件中。
2. 增加配置属性(在 Config Server 中)
eureka.instance.preferIpAddress 属性告诉 Eureka,将服务的 IP 地址而不是服务的主机名注册到 Eureka。
eureka.client.registerWithEureka 属性是一个触发器,告知 Spring Eureka 客户端通过 Eureka 进行注册。
eureka.client.fetchRegistry 属性用于告知 Spring Eureka 客户端以获取注册表的本地副本。
将此属性设置为 true 将在本地缓存注册表,而不是每次查找服务都调用 Eureka 服务。
每隔 30 秒,客户端软件就会重新联系 Eureka 服务,以便查看注册表是否有任何变化。
eureka.client.serviceUrl.defaultZone 包含了客户端用于解析服务位置的 Eureka 服务的列表,该列表以逗号进行分隔。
每个通过 Eureka 注册的服务都会有两个与之相关的组件:应用程序 ID 和实例 ID。
应用程序 ID 用于表示一组服务实例。在 Spring Boot 微服务中,应用程序 ID 始终是由spring.application.name 属性设置的值。
实例 ID 是一个随机生成的数字,用于代表单个服务实例。
为什么偏向于 IP 地址?
在默认情况下,Eureka 注册通过主机名联系它的服务。这种方式在基于服务器的环境中运行良好,在这样的环境中,服务会被分配一个 DNS 支持的主机名。
但是,在基于容器的部署(如Docker)中,容器将以随机生成的主机名启动,并且该主机名不存在对应的DNS条目。如果你没有将 eureka.instance.preferIpAddress 设置为 true,那么你的客户端应用程序将无法正确地解析主机名的位置,因为该容器不存在对应的 DNS 条目。
设置 preferIpAddress 属性将通知 Eureka 服务,客户端想要通过 IP 地址进行通告。
基于云的微服务应该是短暂的和无状态的,它们可以随意启动和关闭,所以 IP 地址更适合这些类型的服务。
3. 查看注册的服务
Eureka 的 REST API
Eureka 服务返回的默认格式是 XML。
Eureka 可以将数据作为 JSON 净荷返回,但是必须将 HTTP 请求中的首部字段 Accept 设置为application/json。
Eureka 的仪表盘
一旦 Eureka 服务启动完毕,就可以通过访问 http://localhost:8070 来查看 Eureka 仪表板。Eureka 仪表板允许我们查看服务的注册状态。
当服务通过 Eureka 注册时,Eureka 将在 30 秒内等待 3 次连续的健康检查,然后服务才能变得可用。这一点在 Docker 环境中运行我们的代码示例时表现非常明显,因为 Eureka 服务和应用程序服务都是在同一时间启动的。
请注意,在启动应用程序后,尽管服务本身已经启动,我们仍然可能收到关于未找到服务的 404 错误。在这种情况下,请等待 30 秒,然后再尝试调用服务。
Last updated