赞
踩
最近自己开始研究下微服务,总结下服务注册中心
主要是服务经常会超时,或者访问不了,跟服务注册中心配置有关系的。
注册服务中心
application.properties
- spring.application.name=eureka
-
- server.port=10001
-
- #强制不rver.port注册到注册服务器
- eureka.client.register-with-eureka=false
- eureka.client.fetch-registry=false
-
- #eureka.instance.hostname=localhost
-
-
- #注册中心地址
- eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
-
- #驱逐下线的服务,间隔,5秒,默认是60,建议开发和测试环境配置
- #org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean.evictionIntervalTimerInMs
- #eureka.server.evictionIntervalTimerInMs=60000
-
- #关闭eureka自我保护机制,修改检查失效服务的时间
- eureka.server.enable-self-preservation=false
- eureka.server.eviction-interval-timer-in-ms=10000
- #减少心跳时间
- #eureka.instance.lease-expiration-duration-in-seconds=90
- #eureka.instance.lease-renewal-interval-in-seconds=4
-
- #忽略权限拦截
- management.security.enabled=false
Eureka Client
application.properties
- server.port=8082
- #root日志以WARN级别输出
- logging.level.root=INFO
- #springframework.web日志以DEBUG级别输出
- logging.level.org.springframework.web=DEBUG
-
- eureka.client.serviceUrl.defaultZone = http://localhost:10001/eureka/
-
- spring.application.name = talk-service
-
- eureka.instance.lease-renewal-interval-in-seconds= 5
- # 心跳时间,即服务续约间隔时间(缺省为30s)
- eureka.instance.lease-expiration-duration-in-seconds= 5
- # 发呆时间,即服务续约到期时间(缺省为90s)
- #client.healthcheck.enabled=true
-
- ribbon.ReadTimeout=60000
- ribbon.ConnectTimeout=60000
-
- feign.hystrix.enabled=false
-
- eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
- eureka.instance.prefer-ip-address=true
- #eureka.instance.preferIpAddress=true
-
- #eureka.client.registry-fetch-interval-seconds=30
-
-
其次在Eureka重写服务状态的监听器
- package com.example.demo;
-
- import com.netflix.appinfo.InstanceInfo;
- import org.springframework.cloud.netflix.eureka.server.event.*;
- import org.springframework.context.event.EventListener;
- import org.springframework.stereotype.Component;
-
- /**
- * 服务上下线监控
- */
- @Component
- public class EurekaStateChangeListener {
-
- @EventListener
- public void listen(EurekaInstanceCanceledEvent event){
- System.err.println(event.getServerId()+"\t"+event.getAppName()+"服务下线");
- }
-
- @EventListener
- public void listen(EurekaInstanceRegisteredEvent event){
- InstanceInfo instanceInfo=event.getInstanceInfo();
- System.err.println(instanceInfo.getAppName()+"进行注册");
- }
-
- @EventListener
- public void listen(EurekaInstanceRenewedEvent event){
- System.err.println(event.getServerId()+"\t"+event.getAppName()+"服务进行续约");
- }
-
- @EventListener
- public void listen(EurekaRegistryAvailableEvent event){
- System.err.println("注册中心启动");
- }
-
- @EventListener
- public void listen(EurekaServerStartedEvent event){
- System.err.println("Eureka Server 启动");
- }
-
- }
输出如下
- 2018-12-05 00:02:14.614 INFO 1616 --- [io-10001-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 94 ms
- TALK-SERVICE进行注册
- 2018-12-05 00:02:16.236 INFO 1616 --- [io-10001-exec-2] c.n.e.registry.AbstractInstanceRegistry : Registered instance TALK-SERVICE/127.0.0.1:8082 with status UP (replication=false)
- TALK-SERVICE进行注册
- 2018-12-05 00:02:17.118 INFO 1616 --- [io-10001-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance TALK-SERVICE/127.0.0.1:8082 with status UP (replication=true)
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 2018-12-05 00:02:24.554 INFO 1616 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 2018-12-05 00:02:34.555 INFO 1616 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 2018-12-05 00:02:44.555 INFO 1616 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 127.0.0.1:8082 TALK-SERVICE服务进行续约
- 2018-12-05 00:02:54.555 INFO 1616 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
- 2018-12-05 00:03:04.555 INFO 1616 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
- 2018-12-05 00:03:04.555 INFO 1616 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Evicting 1 items (expired=1, evictionLimit=1)
- 2018-12-05 00:03:04.556 WARN 1616 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : DS: Registry: expired lease for TALK-SERVICE/127.0.0.1:8082
- 127.0.0.1:8082 TALK-SERVICE服务下线
参数配置具体参考网络,这里提供大概的配置参数
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。