Eurek Ribbon Feign常见问题及解决

Eureka的首页我们可以看到System Status,Environment,test,那我们怎么去修改这些信息呢,

在Spring Cloud文档里面是没有提到这一块的,但是在Eureka的文档里面是有看到

Configuring Eureka Client

https://github.com/Netflix/eureka/wiki/Configuring-Eureka

If you are running in the cloud environment, you will need to pass in the java commandline 

property -Deureka.datacenter=cloud so that the Eureka Client/Server knows to initialize 

the information specific to AWS cloud.

你启动的时候用-D的一个参数,你可以指定-Deureka.datacenter=cloud,Eureka Client/Server他就知道,

去初始化这个信息,AWS要的一些信息

eureka.datacenter=cloud

eureka.environment=product

localhost:8761/
server.port=8761
#eureka.instance.hostname=eureka1

#spring.application.name=microservice-discovery-eureka
#eureka.server.evictionIntervalTimerInMs=60000
eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/eureka
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false


#eureka.server.enableSelfPreservation=true

security.basic.enabled=true
security.user.name=admin
security.user.password=1234

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

logging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%n

eureka.datacenter=cloud
eureka.environment=product

Eureka他有的时候会出现一个红字,相信大家都有遇到过,那这个红字是什么呢,他的红字内容是这个东西

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS 

ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

表示Eureka进入了自动保护模式,那什么是自我保护模式,自我保护模式有什么特性呢

When the Eureka server comes up, it tries to get all of the instance registry information from 

a neighboring node. If there is a problem getting the information from a node, the server tries 

all of the peers before it gives up. If the server is able to successfully get all of the instances,

 it sets the renewal threshold that it should be receiving based on that information. If any time,

 the renewals falls below the percent configured for that value (below 85% within 15 mins), 
 
 the server stops expiring instances to protect the current instance registry information.

In Netflix, the above safeguard is called as self-preservation mode and is primarily used as

 a protection in scenarios where there is a network partition between a group of clients 
 
 and the Eureka Server. In these scenarios, the server tries to protect the information 
 
 it already has. There may be scenarios in case of a mass outage that this may cause 
 
 the clients to get the instances that do not exist anymore. The clients must make sure they are 

resilient to eureka server returning an instance that is non-existent or un-responsive. 

The best protection in these scenarios is to timeout quickly and try other servers.

https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication

他说当Eureka服务器启动时,他尝试从相邻节点获取所有实例注册表信息,在高可用的Eureka里面,他不是会相互复制吗,

如果从节点获取的信息有问题,服务器在放弃之前尝试所有对等体,如果服务器能够成功获取实例,他根据该信息设置其

接收的更新阈值,在任何时间,续订低于该值配置的百分比,服务器将停止,本应到期的实例,按理来说过一段时间没有接收

到心跳,他就应该去expiring,但是在保护模式他stop expiring,这是一个Eureka Server,这是一个Eureka Client,client每

30秒给server搞心跳信息,server如果没有90秒没有接收到心跳信息,把这个client卡擦掉,但是假设server进入了自我保护模式,

self-preservation,现在进入了自我保护模式,虽然他一直没有接收到心跳,他怀疑是eureka server本身,他可能有问题,所以这个

时候他就不把client的节点给踢掉了,这里有一个服务注册节点,服务注册列表把client给删掉,现在就不删了,他怀疑是eureka本身

出了问题,在Netflix中,上述保护措施称为自我保护模式,主要用户在一组客户端和Eureka服务器之间存在网络分区的保护,因为他有

保护模式呢,所以有的时候还比较麻烦

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. 

RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE. 

看到这一段,就说明进入保护模式了,只要进入保护模式,但是在开发的时候,我们往往只是做测试,现在我想把USER给踢掉,他不踢,

STS会调用SpringBoot的shutdown hook,停止的一个钩子,钩子会主动地下线,你帮我下掉吧,是这个意思,

netstat -ano | findstr 3306

eureka他有一个配置

eureka.server.enable-self-preservation	

他可以把自我模式关掉,那这个时候我就可以做测试

eureka.client.healthcheck.enabled = true	开启健康检查(需要spring-boot-starter-actuator依赖)
eureka.instance.lease-renewal-interval-in-seconds =10		租期更新时间间隔(默认30秒)
eureka.instance.lease-expiration-duration-in-seconds =30  租期到期时间(默认90秒)

eureka:
    server:
        enableSelfPreservation: false
        evictionIntervalTimerInMs: 4000

Interval是一个定时器,但是在生产环境中,千万不要把自我保护模式关掉

eureka:
    instance:
        leaseRenewalIntervalInSeconds: 10
        leaseExpirationDurationInSeconds: 30

注意:
更改Eureka更新频率将打破服务器的自我保护功能
https://github.com/spring-cloud/spring-cloud-netflix/issues/373

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}

https://github.com/spring-cloud/spring-cloud-netflix/issues/203	

Documentation: changing Eureka renewal frequency *WILL* break the self-preservation 

feature of the server #373

他说这个东西你不要去修改,它会打破自我保护的特性,我们在这边是应用名称,应用的端口,那你这个时候就得关闭自我保护的模式,

我这边想显示IP:端口怎么办,

instance-id: ${spring.cloud.client.ipAddress}:${server.port}

我们可以用这个配置

microservice-simple-provider-user:10.40.8.144:7900

我立马就知道应用的IP是多少,端口是多少,这只是一个拓展,

1.7. Eureka配置最佳实践总结
https://github.com/spring-cloud/spring-cloud-netflix/issues/203

配置谈不上什么最佳实践,因为任何一种配置他不可能满足所有的要求,那就要看你这个配置能否满足要求,

能满足你的要求就是最佳实践,只是做一个参考,像github标注为documention的,一般都是质量还不错的文档,

可以做一些参考
第二个就是Ribbon,@Configuration和@ComponentScan这个包不能够重叠,RestTemplate有一个坑,

其实它不是Ribbon的坑,它是RestTemplate的坑

  @GetMapping("list-all")
  public List<User> listAll() {
    ArrayList<User> list = Lists.newArrayList();
    User user = new User(1L, "zhangsan");
    User user2 = new User(2L, "zhangsan");
    User user3 = new User(3L, "zhangsan");
    list.add(user);
    list.add(user2);
    list.add(user3);
    return list;

  }
  
localhost:7900/list-all

[
	{"id":1,"username":null,"name":"zhangsan","age":null,"balance":null},
	{"id":2,"username":null,"name":"zhangsan","age":null,"balance":null},
	{"id":3,"username":null,"name":"zhangsan","age":null,"balance":null}
]

在ribbon项目里面

  @GetMapping("/list-all")
  public List<User> listAll() {
    // wrong
    //    List<User> list = this.restTemplate.getForObject("http://microservice-provider-user/list-all", List.class);
    //    for (User user : list) {
    //      System.out.println(user.getId());
    //    }

    // right
    User[] users = this.restTemplate.getForObject("http://microservice-simple-provider-user/list-all", User[].class);
    List<User> list2 = Arrays.asList(users);
    for (User user : list2) {
      System.out.println(user.getId());
    }

    return list2;
  }

localhost:8010/list-all

[
	{"id":1,"username":null,"name":"zhangsan","age":null,"balance":null},
	{"id":2,"username":null,"name":"zhangsan","age":null,"balance":null},
	{"id":3,"username":null,"name":"zhangsan","age":null,"balance":null}
]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值