构建微服务应用的第一步是搭建服务注册中心,它本身也是一个特殊的微服务,负责维护所有其他微服务的注册信息。
整个过程分为六个步骤:
步骤一:创建服务注册中心
使用 spring-cloud-starter-netflix-eureka-server 依赖项构建服务注册中心微服务应用。 pom.xml 文件如下:
4.0.0 org.springframework.boot spring-boot-starter-parent3.4.1 com.sky service-registry1.0 service-registry registry for job portal application 21 2025.0.0 org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-netflix-eureka-serverorg.springframework.boot spring-boot-starter-testtest org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
步骤二:启用Eureka Server
在主应用程序类中添加 @EnableEurekaServer 注解:
package com.sky.service_registry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
步骤三:配置服务注册中心
在 application.properties 文件中配置以下属性,告知Spring不将该应用注册为微服务:
spring.application.name=service-registry server.port=8761 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
步骤四至六:注册新微服务
- 在新的微服务
pom.xml中添加spring-cloud-starter-netflix-eureka-client依赖项。 - 配置新微服务的
application.properties文件,指定 Eureka 服务器的 URL (指向步骤一至三创建的服务注册中心)。 - 启动服务注册中心和新微服务,访问 Eureka 服务器 URL (
h) 验证注册结果。
ttp://localhost:8761/
敬请期待微服务系列文章的后续内容!感谢阅读!









