通过springboot项目的形式搭建Eureka注册中心,具体包含以下几个步骤:
1、pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.5</version>
</parent>
<groupId>com.chengya</groupId>
<artifactId>eureka-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service</name>
<description>Demo project for Spring Cloud</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、配置文件application.yml
server: port: 3301 eureka: instance: hostname: localhost client: # 是否将自己注入到eureka register-with-eureka: false # 是否从eureka中获取注册信息 fetch-registry: false #指定暴露给eureka client的请求地址 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
3、启动类
package com.chengya.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
}
}
4、测试
如果项目启动时遇到以下报错,可能是springboot版本和springcloud版本不匹配导致的
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
SpringCloud和SpringBoot版本对照表:
https://start.spring.io/actuator/info
"spring-cloud": { "Hoxton.SR12": "Spring Boot >=2.2.0.RELEASE and <2.4.0.M1", "2020.0.5": "Spring Boot >=2.4.0.M1 and <2.6.0-M1", "2021.0.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-M3", "2021.0.0-M3": "Spring Boot >=2.6.0-M3 and <2.6.0-RC1", "2021.0.0-RC1": "Spring Boot >=2.6.0-RC1 and <2.6.1", "2021.0.1": "Spring Boot >=2.6.1 and <2.6.6-SNAPSHOT", "2021.0.2-SNAPSHOT": "Spring Boot >=2.6.6-SNAPSHOT and <3.0.0-M1", "2022.0.0-M1": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" }
修改SpringCloud和SpringBoot版本,重启项目
访问端口地址,可以看到Spring Eureka,这就表示Eureka注册中心已经搭建成功。