天天看點

eureka-client無法注冊到服務中心的問題

今天跟着學習視訊敲了一個eureka服務注冊的demo,卻死活無法把服務注冊到注冊中心,反反複複核對了一下代碼,發現沒有什麼不一樣

依賴相同:

服務端依賴:

<!-- eureka server   -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
           

用戶端依賴:

<!-- eureka client   -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
           

yml配置:

服務端:

eureka:
  instance:
    # eureka 服務端執行個體名稱
    hostname: localhost
  client:
    #false表示不向注冊中心注冊自己
    register-with-eureka: false
    #false表示自己端就是注冊中心,我的職責就是維護服務執行個體,并不需要去檢索服務
    fetch-registry: false
    service-url:
      #設定與eureka Server互動的位址,查詢服務和注冊服務都需要依賴這個位址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
           

用戶端yml:

eureka:
  client:
    #表示是否将自己注冊進EurekaServer 預設為true
    register-with-eureka: true
    #是否從EurekaServer抓取已有的注冊資訊,預設為true。單節點無所謂,叢集必須設定為true才能配合ribbon使用負載均衡
    fetch-registry: true
    service-url:
      #設定與eureka Server互動的位址,查詢服務和注冊服務都需要依賴這個位址
      defaultZone: http://localhost:7001/eureka/
           

服務端啟動類:

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaMain7001.class,args);
    }
}
           

用戶端啟動類:

@EnableEurekaClient
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class, args);
    }
}
           

百度一番,發現這是一個老問題了,按照别人的說法還是無法解決。

後面重建立了一個項目,重新寫了一遍就可以了,之前無法注冊的原因是client端的@EnableEurekaClient注解不生效,根本沒有往注冊中心裡注冊,後面注冊成功的時候可以看到在Client端的啟動資訊裡有向eurekaServer端注冊的資訊。

eureka-client無法注冊到服務中心的問題