创建一个 Module 工程
采用 Spring Initializr 的方式构建
作为 服务注册中心 Eureka Server 工程
在它的 POM 中 引入相关依赖:
依赖版本要对应。不然会 jar 包冲突。
引入Eureka Server 的起步依赖 spring-cloud-starter-netflix-eureka-server
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.0.1.RELEASE</version> </dependency>
Spring Boot 测试的起步依赖 spring-boot-starter-test
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
引入 Spring Boot 的 maven 插件 spring-boot-maven-plugin 有了它就可以用 Maven 插件的方式启动 Spring Boot 工程
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.1.RELEASE</version> </plugin> </plugins> </build>
配置 application.yml
指定端口:
server: port: 8761 eureka: instance: prefer-ip-address: true hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
最后在 工程的启动类上加注解 @EnableEurekaServer 开启 Eureka Server 功能,引入包,导入.class 。
@EnableEurekaServer
处理 Test 报错,引入包,导入.class。
或删除 原引入,直接导入 test.class 。
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency>
启动程序 启动类 的 main 方法 启动程序。
在浏览器上访问 Eureka Server 主页面
http://localhost:8761
这样就可以等其他服务来 Eureka Server 注册中心注册实例了。