1、创建microservicecloud-eureka-7001,pom.xml添加以下包
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
2、配置yaml
server:
port: 7001
eureka:
instance:
hostname: www.eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3、启动类
@SpringBootApplication
@EnableEurekaServer //这里是重点,需要引入这个注解
public class ApplicationEureka7001 {
public static void main(String[] args) {
SpringApplication.run(ApplicationEureka7001.class,args);
}
}
配置完以后可以启动,
红色框可以看到已注册的组件,Eureka现在并没有发现任何组件
4、Eureka注册组件,我在这里使用随便一个组件来注册
修改组件的pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
修改YAML配置
eureka:
client:
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka/ //eureka地址
在启动类上加上注解
@EnableEurekaClient
启动组件,然后再回去eureka地址页面上看,可以看出来,组件已经注册到eureka上了