上篇以一个 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 Se
上篇《 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/:
###Eureka Client configuration for Sample Eureka Client
#Properties based configuration for eureka client. The properties specified here is mostly what the users
#need to change. All of these can be specified as a java system property with -D option (eg)-Deureka.region=us-east-1
#For additional tuning options refer <url to go here>
#Region where eureka is deployed -For AWS specify one of the AWS regions, for other datacenters specify a arbitrary string
#indicating the region.This is normally specified as a -D option (eg) -Deureka.region=us-east-1
eureka.region=default
#Name of the application to be identified by other services
eureka.name=sampleEurekaClient
#Virtual host name by which the clients identifies this service
#eureka.vipAddress=eureka.mydomain.net
#The port where the service will be running and servicing requests
#eureka.port=80
#For eureka clients running in eureka server, it needs to connect to servers in other zones
eureka.preferSameZone=true
#Change this if you want to use a DNS based lookup for determining other eureka servers. For example
#of specifying the DNS entries, check the eureka-client-test.properties, eureka-client-prod.properties
eureka.shouldUseDns=false
eureka.us-east-1.availabilityZones=default
eureka.serviceUrl.default=http://serverIP:8080/eureka/v2/
4. 日志配置
就用官方提供 demo 里的 sampleclient 下的 log4j.properties 即可,当然还要根据实际需要修正一下,比如给 com.netflix.eureka 包的输出级别设置为 DEBUG(log4j.properties 追加 log4j.logger.com.netflix.eureka=DEBUG),以方便我们研发期跟踪调试。
log4j.rootCategory=INFO,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %C:%L [%t] [%M] %m%n
log4j.logger.com.netflix.eureka=DEBUG
5. 寻找本次请求的 Application Service 实例
客户端在向 Application Service 请求之前得先从 Eureka Server 拿到侍服本次请求的 Application Se