springcloud对于eureka对注册服务的剔除机制及hystrix集成ribbon实现熔断降级

eureka对注册在eureka服务平台的服务实例,默认是有自我保护机制的。也就是说对实际上已经不能对外提供服务的应用,不进行剔除,那么在实际的应用中,我们要及时剔除掉不能对外进行提供服务的应用,并进行及时修复重新让其能提供对外服务。这个时候需要eureka开启相关的健康检查配置,对于非健康的服务实例。及时下线。那么相关的配置已经经过测试配置如下:

1:需要引入springboot的健康检查依赖:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

2:需要配置相关的属性依赖:

(1):eureka在什么时候剔除掉已经不能用的服务实例?

(2) eureka怎么判断哪些服务实例需要剔除的?

大致的机制如下:

注册到eureka上的服务实例需要向eureka发送心跳(默认每30秒发送一次心跳)证明该服务实活着的。正常情况eureka每30s收到一次心跳,如果eureka一直没有收到某个服务发送的心跳(默认是90s内,连续三次没有收到心跳),则会任务该服务已经挂掉,等待剔除。那么当经过一定时间(默认1000*30s,可以自行设置)。就会剔除掉已经挂掉的服务。

配置信息如下:

服务端配置信息:
eureka:
  server:
    #每***s剔除续约到期的服务
    eviction-interval-timer-in-ms: 30000
    #关闭eureka自我保护机制
    enable-self-preservation: false
eureka:
  instance:
    #30s没有收到心跳就剔除服务
    lease-expiration-duration-in-seconds: 10
    #每10s发送一次心跳
    lease-renewal-interval-in-seconds: 5
    #开启健康检查
  client:
    healthcheck:
      enabled: true

经过测试,如果10s都没有收到服务实例发送的心跳就把它列为剔除对象。30s后将其踢掉。

第二部分是Hystrix集成ribbon实现服务熔断降级

ribbon实现负载很简单。@LoadBalance注解添加到创建RestTemplate对象即可实现

主要是实现熔断的相关配置代码如下:

1:配置Hystrix的超时时长。默认是1s这个,可以手动根据实际业务需求更改:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000

2:创建服务降级处理回调方法:

@EnableCircuitBreaker开启Hystrix熔断降级

  @HystrixCommand  @Fallback定义回调函数

package com.itmuch.cloud.microserviceconsumermovieribbonwithhystrix.service;

import com.itmuch.cloud.microserviceconsumermovieribbonwithhystrix.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

/**
 * : 描述信息
 *
 * @author liyy
 * @date 2018-07-28 15:53
 */
@Service
public class RibbonHystrixService {

    @Autowired
    private RestTemplate restTemplate;

    public static Logger logger = LoggerFactory.getLogger(RibbonHystrixService.class);

    @HystrixCommand(fallbackMethod = "fallback")
    public User findOne(Long id){
//        return restTemplate.getForObject("http://microservice-provider-user/1",User.class);
        User u = restTemplate.getForObject("http://localhost:8080/userController/1",User.class);
        return u;
    }

    public User fallback(Long id){
        logger.info("服务调用发生异常,进入熔断方法,方法参数:{}",id);
        User user = new User();
        user.setId(-1L);
        user.setUsername("default username");
        user.setAge("13");
        return user;
    }

}
测试服务设置超时返回时长!
@GetMapping("/{id}")
    @ResponseBody
    public User findById(@PathVariable Long id) throws InterruptedException, ParseException {
        User user = userRepository.findUser(id);
        Thread.sleep(2000);//延迟800ms相应
        return user;
    }

测试通过!实现服务长时间没有响应。直接返回指定的结果。防止应用产生雪崩效应

猜你喜欢

转载自blog.csdn.net/liyingying111111/article/details/84768754