1、空工程创建完成-创建注册中心模块

image.png
image.png
image.png

2、项目的pom依赖如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.fcant</groupId>
  12. <artifactId>register_center</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>register_center</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. <exclusions>
  30. <exclusion>
  31. <groupId>org.junit.vintage</groupId>
  32. <artifactId>junit-vintage-engine</artifactId>
  33. </exclusion>
  34. </exclusions>
  35. </dependency>
  36. </dependencies>
  37. <dependencyManagement>
  38. <dependencies>
  39. <dependency>
  40. <groupId>org.springframework.cloud</groupId>
  41. <artifactId>spring-cloud-dependencies</artifactId>
  42. <version>${spring-cloud.version}</version>
  43. <type>pom</type>
  44. <scope>import</scope>
  45. </dependency>
  46. </dependencies>
  47. </dependencyManagement>
  48. <build>
  49. <plugins>
  50. <plugin>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-maven-plugin</artifactId>
  53. </plugin>
  54. </plugins>
  55. </build>
  56. </project>

3、在配置文件配置注册中心

  1. server:
  2. port: 8761
  3. tomcat:
  4. uri-encoding: UTF-8
  5. servlet:
  6. application-display-name: RegisterCenter
  7. eureka:
  8. instance:
  9. #eureka实例的主机名
  10. hostname: eureka-server
  11. client:
  12. register-with-eureka: false #是否把服务注册中心注册到eureka
  13. fetch-registry: false #是否从服务注册中心注册eureka获取注册信息
  14. service-url:
  15. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  16. spring:
  17. application:
  18. name: RegisterCenter
  19. cloud:
  20. loadbalancer:
  21. ribbon:
  22. enabled: true

4、当前版本下需要新建config.properties配置文件作为动态配置(空文件即可)

否则会报如下warn
image.png

5、在启动类添加开启EurekaServer的注解 @EnableEurekaServer

  1. package com.fcant.register_center;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @EnableEurekaServer
  6. @SpringBootApplication
  7. public class RegisterCenterApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(RegisterCenterApplication.class, args);
  10. }
  11. }

6、启动注册中心,并访问管理页面

http://localhost:8080/
image.png