spring cloud netflix eureka高可用注册中心(找不到服务)
今天在学习spring cloud高可用注册中心时遇到了一个难缠的问题,而且困扰了我半天。根据官方文档和一些书籍搭建了一个注册中心,但是将集群内各个服务启动后报错了,注册中心集群内的各个服务都注册不上去,百度搜,无果。先上代码。
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
spring:
application:
name: service-registry
---
server:
port: 8771
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2:8772/eureka/
---
server:
port: 8772
spring:
profiles: peer2
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8771/eureka/
Application.Java
package name.newbie;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
这里我们准备了两个相互注册的服务,在完成注册服务搭建后开始逐个启动,结果就是(报错!报错!报错!)
寻思了半天,是自己大意了。
原因及解决方法
需要在host文件中指定127.0.0.1和hostname的映射
host文件地址:C:\Windows\System32\drivers\etc
因为在yml文件里我指定了hostname为peer1/peer2
来源:https://blog.csdn.net/qq_43959822/article/details/98764783