注册中心项目: service-center
1.项目pom.xml添加eureka-server依赖
<?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.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.douyu.cloud</groupId>
<artifactId>service-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-center</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<!--eureka 注册中心依赖-->
<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>
<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>
</plugin>
</plugins>
</build>
</project>
2.项目启动类添加 @EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //启动支持Eureka注册服务
public class ServiceCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceCenterApplication.class, args);
}
}
3.配置文件配置注册中心信息
application.yml
spring:
profiles:
active: dev #开发的配置文件生效
application:
name: service-center #项目名称
application-dev.yml
server:
port: 8001 #注册中心端口是8001
eureka:
instance:
hostname: 127.0.0.1 #表示设置该服务注册中心的hostname
preferIpAddress: true #Eureka注册服务列表显示ip+端口
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
register-with-eureka: false #表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设为false。
fetch-registry: false #表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他 的Eureka Server节点的数据,故而设为false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #与Eureka Server交互的
地址,查询服务和注册服务都需要依赖这个地址,默认是http://localhost:8001/eureka;多个地址可用,分隔
server:
enable-self-preservation: false #关闭自我保护,解决服务已经关闭,但是注册中心上面还是有注册信息的问题 生产环境 不能关闭 AP高可用,分区容错性
4.访问Eureka注册中心 http://127.0.0.1:8001