1.eureka-server 的 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.eureka-server的启动主类,添加一个 @EnableEurekaServer注解即可
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3.eureka-server的application.properties配置文件如下:
server.port=10025
spring.application.name=eureka-service
eureka.instance.hostname=localhost
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}:${random.long(1,100)}
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
4.eureka-client 的 pom.xml内容如下,其他和eureka-server相同:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
5.eureka-client的启动主类,添加一个 @EnableDiscoveryClient注解即可
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
6.eureka-client的application.properties配置文件如下:
server.port=9500
#服务的实例名,服务间的调用通过此名字调用
spring.application.name=eureka-client
#填写eureka注册中心地址,把自己注册到注册中心去
eureka.client.serviceUrl.defaultZone=http://localhost:10025/eureka/
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}:${random.long(1,100)}
7.好了,分别启动 eureka-server、eureka-client,然后访问 http://localhost:10025/,可以看到eureka-client服务实例已经注册到注册中心了,后面还有该实例的 IP与端口