Eureka 的 Application Client 客戶端的運行示例
上篇以一個 demo 示例介紹了 Eureka 的 Application Service 客戶端角色。今天我們繼續瞭解 Eureka 的 Application Client 客戶端,以一個簡單的交互示例,介紹 Application Client 是如何使用 Eureka 獲取
Application Service 實例併發送請求的。
上篇《Eureka 的 Application Service 客戶端的註冊以及運行示例》使用了一個 Socket 服務做爲 demo,本文示例配合該文,將以一個 Socket 客戶端對該服務進行請求。
1. Eureka 服務器啓動
本文 demo 要求 Eureka Server 已經部署好,並已經啓動。關於架設步驟參考博客《Linux 下 Eureka 服務器的部署》。
2. Application Service 服務啓動
直接將《Eureka 的 Application Service 客戶端的註冊以及運行示例》中的小 demo 跑起來(Socket Server 啓動,開始侍服網絡請求)。
3. Application Client 配置文件的編寫
我們的 Application Client 就用官方提供 demo 裏的 sampleclient 下的 sample-eureka-client.properties 即可,當然還要根據實際情況修正一下,比如把 eureka.serviceUrl.default(默認情況下本客戶端將要註冊到的 Eureka 服務器):http://serverIP:8080/eureka/v2/:
4. 日誌配置
就用官方提供 demo 裏的 sampleclient 下的 log4j.properties 即可,當然還要根據實際需要修正一下,比如給 com.netflix.eureka 包的輸出級別設置爲 DEBUG(log4j.properties 追加 log4j.logger.com.netflix.eureka=DEBUG),以方便我們研發期跟蹤調試。
5. 尋找本次請求的 Application Service 實例
客戶端在向 Application Service 請求之前得先從 Eureka Server 拿到侍服本次請求的 Application Service 實例。尋找本次請求的 Application Service 實例的代碼如下:
第一句初始化本客戶端,第二句告訴 Eureka 本客戶端處於就緒狀態,接下來兩句是使用 Eureka 找到對本次請求進行侍服的 Application Service 實例。
6. Application Client 網絡請求
Application Client 拿到 Application Service 之後立馬進行網絡 Socket 請求,進行數據信息交互。
7. Application Client 關閉時取消註冊
Application Client 在關閉時要將自己在 Eureka 的註冊取消掉。
8. 運行 demo
新建一個項目(不要和 Application Service 的 demo 跑在同一個項目下),現在我們把完整的 Application Client 代碼整理一下。
如上篇博客所述一樣把各種依賴包加進本文 demo 的 CLASSPATH,然後把第三、四步寫好的 sample-eureka-client.properties、log4j.properties 也拷貝進 CLASSPATH,並將 sample-eureka-client.properties 重命名爲 config.properties(原因參考上篇博客),接下來就可以運行本文 demo 了。run SampleEurekaClient,日誌結果如下:
2014-07-10 11:02:51,860 INFO com.netflix.config.sources.URLConfigurationSource:125 [main] [<init>] URLs to be used as dynamic configuration source: [file:/D:/javaprojects/test2/bin/config.properties]
2014-07-10 11:02:51,927 INFO com.netflix.config.DynamicPropertyFactory:281 [main] [getInstance] DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@5ca46701
2014-07-10 11:02:52,060 WARN com.netflix.appinfo.PropertiesInstanceConfig:349 [main] [init] Cannot find the properties specified : eureka-client. This may be okay if there are other environment specific properties or the configuration is installed with a different mechanism.
2014-07-10 11:02:52,065 WARN com.netflix.discovery.DefaultEurekaClientConfig:95 [main] [init] Cannot find the properties specified : eureka-client. This may be okay if there are other environment specific properties or the configuration is installed with a different mechanism.
2014-07-10 11:02:52,103 INFO com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider:79 [main] [get] Setting initial instance status as: STARTING
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:646 [main] [fetchRegistry] Disable delta property : false
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:647 [main] [fetchRegistry] Single vip registry refresh property : null
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:648 [main] [fetchRegistry] Force full registry fetch : false
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:649 [main] [fetchRegistry] Application is null : false
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:650 [main] [fetchRegistry] Registered Applications size is zero : true
2014-07-10 11:02:53,668 INFO com.netflix.discovery.DiscoveryClient:652 [main] [fetchRegistry] Application version is -1: true
2014-07-10 11:02:54,061 INFO com.netflix.discovery.DiscoveryClient:992 [main] [makeRemoteCall] Finished a call to service url http://serverIP:8080/eureka/v2/ and url path apps/ with status code 200.
2014-07-10 11:02:54,061 INFO com.netflix.discovery.DiscoveryClient:758 [main] [getAndStoreFullRegistry] Getting all instance registry info from the eureka server
2014-07-10 11:02:54,568 INFO com.netflix.discovery.DiscoveryClient:765 [main] [getAndStoreFullRegistry] The response status is 200
2014-07-10 11:02:54,578 INFO com.netflix.discovery.DiscoveryClient:1056 [main] [initScheduledTasks] Starting heartbeat executor: renew interval is: 30
Connected to server. Sending a sample request
Waiting for server response..
Received response from server. Communication all fine using Eureka :
Exiting the client. Demo over..
證明 demo 獲取 Application Service 實例並請求成功。
可以看出,Application Service 實例的信息都封裝在了 com.netflix.appinfo.InstanceInfo 下,那麼我們還能從中獲取哪些信息?SampleEurekaClient 類加入以下代碼:
然後重新運行本文 demo,以上語句打印結果如下:
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:69 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getAppGroupName()-UNKNOWN
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:70 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getAppName()-SAMPLESERVICE
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:71 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getASGName()-null
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:72 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getHomePageUrl()-http://defonds-win7:1935/
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:73 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getHostName()-defonds-win7
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:74 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getId()-defonds-win7
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:75 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getIPAddr()-172.21.40.134
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:76 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getLastUpdatedTimestamp()-1404961202884
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:77 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getPort()-1935
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:78 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getSecureVipAddress()-null
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:79 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getStatusPageUrl()-http://defonds-win7:1935/Status
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:80 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getVIPAddress()-sampleservice.mydomain.net
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:81 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getLastDirtyTimestamp()-1404961230856
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:82 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getHealthCheckUrls()-[http://defonds-win7:1935/healthcheck]
2014-07-10 11:02:54,621 DEBUG com.netflix.eureka.SampleEurekaClient:83 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getStatus()-UP
這些信息都和 http://serverIP:8080/eureka/v2/apps/ 顯示的 SAMPLESERVICE 實例的 各個屬性一一對應。
參考資料
上篇《Eureka 的 Application Service 客戶端的註冊以及運行示例》使用了一個 Socket 服務做爲 demo,本文示例配合該文,將以一個 Socket 客戶端對該服務進行請求。
1. Eureka 服務器啓動
本文 demo 要求 Eureka Server 已經部署好,並已經啓動。關於架設步驟參考博客《Linux 下 Eureka 服務器的部署》。
2. Application Service 服務啓動
直接將《Eureka 的 Application Service 客戶端的註冊以及運行示例》中的小 demo 跑起來(Socket Server 啓動,開始侍服網絡請求)。
3. Application Client 配置文件的編寫
我們的 Application Client 就用官方提供 demo 裏的 sampleclient 下的 sample-eureka-client.properties 即可,當然還要根據實際情況修正一下,比如把 eureka.serviceUrl.default(默認情況下本客戶端將要註冊到的 Eureka 服務器):http://serverIP:8080/eureka/v2/:
4. 日誌配置
就用官方提供 demo 裏的 sampleclient 下的 log4j.properties 即可,當然還要根據實際需要修正一下,比如給 com.netflix.eureka 包的輸出級別設置爲 DEBUG(log4j.properties 追加 log4j.logger.com.netflix.eureka=DEBUG),以方便我們研發期跟蹤調試。
5. 尋找本次請求的 Application Service 實例
客戶端在向 Application Service 請求之前得先從 Eureka Server 拿到侍服本次請求的 Application Service 實例。尋找本次請求的 Application Service 實例的代碼如下:
第一句初始化本客戶端,第二句告訴 Eureka 本客戶端處於就緒狀態,接下來兩句是使用 Eureka 找到對本次請求進行侍服的 Application Service 實例。
6. Application Client 網絡請求
Application Client 拿到 Application Service 之後立馬進行網絡 Socket 請求,進行數據信息交互。
7. Application Client 關閉時取消註冊
Application Client 在關閉時要將自己在 Eureka 的註冊取消掉。
8. 運行 demo
新建一個項目(不要和 Application Service 的 demo 跑在同一個項目下),現在我們把完整的 Application Client 代碼整理一下。
如上篇博客所述一樣把各種依賴包加進本文 demo 的 CLASSPATH,然後把第三、四步寫好的 sample-eureka-client.properties、log4j.properties 也拷貝進 CLASSPATH,並將 sample-eureka-client.properties 重命名爲 config.properties(原因參考上篇博客),接下來就可以運行本文 demo 了。run SampleEurekaClient,日誌結果如下:
2014-07-10 11:02:51,860 INFO com.netflix.config.sources.URLConfigurationSource:125 [main] [<init>] URLs to be used as dynamic configuration source: [file:/D:/javaprojects/test2/bin/config.properties]
2014-07-10 11:02:51,927 INFO com.netflix.config.DynamicPropertyFactory:281 [main] [getInstance] DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@5ca46701
2014-07-10 11:02:52,060 WARN com.netflix.appinfo.PropertiesInstanceConfig:349 [main] [init] Cannot find the properties specified : eureka-client. This may be okay if there are other environment specific properties or the configuration is installed with a different mechanism.
2014-07-10 11:02:52,065 WARN com.netflix.discovery.DefaultEurekaClientConfig:95 [main] [init] Cannot find the properties specified : eureka-client. This may be okay if there are other environment specific properties or the configuration is installed with a different mechanism.
2014-07-10 11:02:52,103 INFO com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider:79 [main] [get] Setting initial instance status as: STARTING
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:646 [main] [fetchRegistry] Disable delta property : false
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:647 [main] [fetchRegistry] Single vip registry refresh property : null
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:648 [main] [fetchRegistry] Force full registry fetch : false
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:649 [main] [fetchRegistry] Application is null : false
2014-07-10 11:02:53,667 INFO com.netflix.discovery.DiscoveryClient:650 [main] [fetchRegistry] Registered Applications size is zero : true
2014-07-10 11:02:53,668 INFO com.netflix.discovery.DiscoveryClient:652 [main] [fetchRegistry] Application version is -1: true
2014-07-10 11:02:54,061 INFO com.netflix.discovery.DiscoveryClient:992 [main] [makeRemoteCall] Finished a call to service url http://serverIP:8080/eureka/v2/ and url path apps/ with status code 200.
2014-07-10 11:02:54,061 INFO com.netflix.discovery.DiscoveryClient:758 [main] [getAndStoreFullRegistry] Getting all instance registry info from the eureka server
2014-07-10 11:02:54,568 INFO com.netflix.discovery.DiscoveryClient:765 [main] [getAndStoreFullRegistry] The response status is 200
2014-07-10 11:02:54,578 INFO com.netflix.discovery.DiscoveryClient:1056 [main] [initScheduledTasks] Starting heartbeat executor: renew interval is: 30
Connected to server. Sending a sample request
Waiting for server response..
Received response from server. Communication all fine using Eureka :
Exiting the client. Demo over..
證明 demo 獲取 Application Service 實例並請求成功。
可以看出,Application Service 實例的信息都封裝在了 com.netflix.appinfo.InstanceInfo 下,那麼我們還能從中獲取哪些信息?SampleEurekaClient 類加入以下代碼:
然後重新運行本文 demo,以上語句打印結果如下:
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:69 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getAppGroupName()-UNKNOWN
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:70 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getAppName()-SAMPLESERVICE
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:71 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getASGName()-null
2014-07-10 11:02:54,619 DEBUG com.netflix.eureka.SampleEurekaClient:72 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getHomePageUrl()-http://defonds-win7:1935/
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:73 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getHostName()-defonds-win7
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:74 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getId()-defonds-win7
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:75 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getIPAddr()-172.21.40.134
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:76 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getLastUpdatedTimestamp()-1404961202884
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:77 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getPort()-1935
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:78 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getSecureVipAddress()-null
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:79 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getStatusPageUrl()-http://defonds-win7:1935/Status
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:80 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getVIPAddress()-sampleservice.mydomain.net
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:81 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getLastDirtyTimestamp()-1404961230856
2014-07-10 11:02:54,620 DEBUG com.netflix.eureka.SampleEurekaClient:82 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getHealthCheckUrls()-[http://defonds-win7:1935/healthcheck]
2014-07-10 11:02:54,621 DEBUG com.netflix.eureka.SampleEurekaClient:83 [main] [sendRequestToServiceUsingEureka] nextServerInfo.getStatus()-UP
這些信息都和 http://serverIP:8080/eureka/v2/apps/ 顯示的 SAMPLESERVICE 實例的 各個屬性一一對應。
參考資料
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.