SpringCloud框架初探(四): Eureka Client客戶端搭建

源碼地址:SpringCloud學習源碼

1、Eureka客戶端搭建

@SpringBootApplication
@EnableEurekaClient
public class ProviderUserApplication {

  public static void main(String[] args) {
    SpringApplication.run(ProviderUserApplication.class, args);
  }
}

使用@EnableEurekaClient,啓動Eureka客戶端。

然後再看客戶端的配置文件:
定義客戶端的端口號,還是使用server,port

server:
  port: 7901
spring:
  application:
    name: provider-user

需要指明spring.application.name,這個很重要,這在以後的服務與服務之間相互調用一般都是根據這個name 。

配置Eureka Client其他參數:

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
    metadata-map:
      zone: ABC      # eureka可以理解的元數據
      lilizhou: BBC  # 不會影響客戶端行爲
    lease-renewal-interval-in-seconds: 5

eureka.client.healthcheck.enabled = true 開啓Eureka客戶端的健康檢查
eureka.client.serviceUrl.defaultZone 指定客戶端對應的註冊中心服務器的地址
eureka.instance.prefer-ip-address = true 表示註冊到註冊中心的是IP
eureka.instance.instance-id 表示註冊到註冊中心實例的ID規則

eureka.instance.lease-renewal-interval-in-seconds 表示續約更新時間間隔,也就是客戶端健康檢查的心跳時間間隔。

2、啓動Eureka客戶端,並查看服務器狀態

當client向server註冊時,它會提供一些元數據,例如主機和端口,URL,主頁等。Eureka server 從每個client實例接收心跳消息。 如果心跳超時,則通常將該實例從註冊server中刪除。

在這裏插入圖片描述

可以看到PROVIDER-USER(客戶端的名稱),也已經註冊到了Eureka註冊中心。

provider-user:10.1.18.161:7900 也就是instance-id:

 instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

我在微信訂閱號等你!
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章