Eureka 的 Application Service 客户端的注册以及运行示例

        Eureka 服务器架起来了(关于架设步骤参考博客《 Linux 下 Eureka 服务器的部署》),现在如何把我们要负载均衡的服务器(也就是从 Application Client 接收请求并返回一个响应的 Application Service)注册到 Eureka?本文以一个示例介绍 Eureka Application Service 客户端的 Eureka 生命周期(包括启动时的注册、侍服示例、关闭时的取消注册)情况,相信读完本文之后,读者可以对 Eureka 的 Application Service 角色有了一个进一步了解,而且完全可以把自己的服务加进 Eureka。
        1. Eureka 服务器启动
        本文 demo 要求 Eureka Server 已经部署好,并已经启动。关于架设步骤参考博客《 Linux 下 Eureka 服务器的部署》。
        Eureka 服务器启动以后,可以通过 http://serverIP:8080/eureka/ 或者 http://serverIP:8080/eureka/v2/apps/ 浏览已注册到 Eureka 的 Application Service。比如作者在开发环境 PC 访问服务器端的 http://serverIP:8080/eureka/v2/apps/,页面返回结果如下:
<applications>
	<versions__delta>1</versions__delta>
	<apps__hashcode>UP_1_</apps__hashcode>
	<application>
		<name>EUREKA</name>
		<instance>
			<hostName>localhost.localdomain</hostName>
			<app>EUREKA</app>
			<ipAddr>127.0.0.1</ipAddr>
			<status>UP</status>
			<overriddenstatus>UNKNOWN</overriddenstatus>
			<port enabled="true">8080</port>
			<securePort enabled="false">443</securePort>
			<countryId>1</countryId>
			<dataCenterInfo
				class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
				<name>MyOwn</name>
			</dataCenterInfo>
			<leaseInfo>
				<renewalIntervalInSecs>30</renewalIntervalInSecs>
				<durationInSecs>90</durationInSecs>
				<registrationTimestamp>1404789864122</registrationTimestamp>
				<lastRenewalTimestamp>1404798147096</lastRenewalTimestamp>
				<evictionTimestamp>0</evictionTimestamp>
				<serviceUpTimestamp>1404789841811</serviceUpTimestamp>
			</leaseInfo>
			<metadata class="java.util.Collections$EmptyMap" />
			<appGroupName>UNKNOWN</appGroupName>
			<homePageUrl>http://localhost.localdomain:8080/</homePageUrl>
			<statusPageUrl>http://localhost.localdomain:8080/Status</statusPageUrl>
			<healthCheckUrl>http://localhost.localdomain:8080/healthcheck</healthCheckUrl>
			<vipAddress>eureka.mydomain.net</vipAddress>
			<isCoordinatingDiscoveryServer>true</isCoordinatingDiscoveryServer>
			<lastUpdatedTimestamp>1404789864122</lastUpdatedTimestamp>
			<lastDirtyTimestamp>1404789841863</lastDirtyTimestamp>
			<actionType>ADDED</actionType>
		</instance>
	</application>
</applications>

        apps__hashcode 是 1,可以看出目前只有一个 Application Service 注册到了 Eureka Server,这个名为 Eureka 的 Application Service 只有一个实例,就是这台 Eureka Server 它自己 - localhost.localdomain。为何 Eureka Server 自己也是一个 Application Service 呢?这也是 Eureka 架构健壮性 feature 之一,Eureka Server 之间也会相互注册,而且还会将注册到自己的 Application Service 注册给其他 Eureka Server,避免了一台 Eureka Server 宕机所造成的区域性服务瘫痪。
        2. Application Service 配置文件的编写
        我们的负载均衡服务器就用官方提供 demo 里的 sampleservice 下的 sample-eureka-service.properties 即可,当然还要根据实际情况修正一下,比如把 eureka.name 改为我们自定义的 Application 名以区分开其他服务,把 eureka.port(本服务将会运行并侍服请求的端口)改为我们的 Service 服务器开放的侍服端口 1935,eureka.serviceUrl.default(默认情况下本服务将要注册到的 Eureka 服务器): http://serverIP:8080/eureka/v2/
###Eureka Client configuration for Sample Eureka Service


#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=sampleservice


#Virtual host name by which the clients identifies this service
eureka.vipAddress=sampleservice.mydomain.net


#The port where the service will be running and serving requests
eureka.port=1935


#For eureka clients running in eureka server, it needs to connect to servers in other zones
eureka.preferSameZone=false


#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/

        3. 日志配置
        就用官方提供 demo 里的 sampleservice 下的 log4j.properties 即可,当然还要根据实际需要修正一下,比如给 com.defonds.wms.module.server 包的输出级别设置为 DEBUG(log4j.properties 追加 log4j.logger.com.defonds.wms.module.server=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

        4. Application Service 启动时注册 Eureka
        我们希望每台负载均衡服务器在启动时就注册给 Eureka,以及时承担负载。注册代码如下所示:
    private static final DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory.getInstance();


    private static final Logger logger = LoggerFactory.getLogger(SampleEurekaService.class);


    public void registerWithEureka() {
        // Register with Eureka
        DiscoveryManager.getInstance().initComponent(
                new MyDataCenterInstanceConfig(),
                new DefaultEurekaClientConfig());
        ApplicationInfoManager.getInstance().setInstanceStatus(
                InstanceStatus.UP);
	}

        第一句加载本地配置文件,initComponent 句根据配置初始化这台 Eureka Application Service,并且注册到 Eureka Server,setInstanceStatus 句指示本台 Application Service 已启
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值