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.2.6.RELEASE</version> <!--这里的版本号需要 和下面Hoxton.SR3对应上 否则服务启动报错-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>spring-boot-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-euraka</name>
<description>spring-euraka注册</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<!--用于服务可以被别人发现,但是自己用http://localhost:8081/ 看不到自己服务,只能看见别人的服务-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!--必须添加这块,否则出现 Error creating bean with name 'configurationPropertiesBeans报错-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId><!--当进行打包mvn package 会打包成一个可以直接运行的 JAR 文件使用“java -jar”命令就可以直接运行-->
<version>2.5.0</version>
</plugin>
</plugins>
</build>
</project>
在启动类上添加
package com.example.springeuraka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringEurakaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurakaApplication.class, args);
}
}
配置中心:
server.port=8081
spring.application.name=spring-euraka-lpp
eureka.instance.hostname=localhost
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaulZone=http://localhost:8081/eureka/
最后实现效果:Eureka