上篇以一个 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 实例的 各个属性一一对应。
参考资料