依赖管理
项目搭建比较简单,首先创建一个空的springboot项目,修改pom.xml文件springcloud相关的依赖管理。
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud.version>Hoxton.SR10</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<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>
SpringBoot与SpringCloud之间的版本依赖可访问https://start.spring.io/actuator/info获取。
"Hoxton.SR10": "Spring Boot >=2.2.0.RELEASE and <2.3.10.BUILD-SNAPSHOT",
"Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.3.10.BUILD-SNAPSHOT and <2.4.0.M1",
"2020.0.0-M3": "Spring Boot >=2.4.0.M1 and <=2.4.0.M1",
"2020.0.0-M4": "Spring Boot >=2.4.0.M2 and <=2.4.0-M3",
"2020.0.0": "Spring Boot >=2.4.0.M4 and <=2.4.0",
"2020.0.1": "Spring Boot >=2.4.1 and <2.5.0-M1",
"2020.0.2-SNAPSHOT": "Spring Boot >=2.4.5-SNAPSHOT"
配置文件
server:
port: 7000 #为了以后访问项目不用写端口号
context-path: / #为了以后访问项目不用写项目名
spring:
application:
name: eureka-server
eureka:
instance:
hostname: 127.0.0.1 # 指定Eureka主机
client:
register-with-eureka: false # 是否向服务中心注册自己
fetch-registry: false # 是否能够获取Eureka注册信息
service-url: # 暴露自己的服务中心地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
server:
enable-self-preservation: false # 关闭自我保护机制,防止应用停止后仍是up状态
项目启动
项目启动要在启动类加@EnableEurekaServer注解,表示此项目为eureka server服务
package ichpeng.cloud.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author: ichpeng@qq.com
* @date: 2021/3/18 14:14
* @description:
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
访问http://127.0.0.1:7000 打开eureka服务页面