1.Eureka服务端配置
- 新建module eureka-7001
- pom文件引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- yaml配置
server:
port: 7001
spring:
application:
name: eureka-7001
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
- 启动类加
@EnableEurekaServer
注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain {
public static void main(String[] args) {
SpringApplication.run(EurekaMain.class,args);
}
}
2.Eureka客户端配置
- 在需要注册的module的pom文件引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yaml配置
server:
port: 8081
spring:
application:
name: cloud-client
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true
instance-id: client8001
- 启动类加
@EnableEurekaClient
注解
@SpringBootApplication
@EnableEurekaClient
public class ClientMain {
public static void main(String[] args) {
SpringApplication.run(ClientMain.class,args);
}
}
- 访问http://localhost:7001/看到cloud-client已成功注册进eureka
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/04c16637559a455c7c0594d7cfcea85d.png)