四、Hystrix

一、Hystrix简介

Hystrix是由Netflix开源的一个延迟和容错库,实现熔断器。防止服务请求不到,一直等待,而引起雪崩。

 

二、搭建服务消费端ribbon-hystrix

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ribbon-hystrix</artifactId>
    <description>ribbon实现熔断器</description>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <!-- 引入hystrix熔断器的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>
</project>
server:
  port: 0
spring:
  application:
    name: ribbon-hystrix
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${random.int}
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
package com.mo.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/6 22:52
 */
@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 使用HystrixCommand进行熔断
     * 当请求SERVICE-HELLO出现故障时,Hystrix会将请求进行熔断
     * 返回helloError方法的执行结果
     * 防止请求连接不通,累积大量请求,产生雪崩
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "helloError")
    public String hello() {
        return restTemplate.getForObject("http://SERVICE-HELLO/hello", String.class);
    }

    /**
     * 熔断器熔断后,返回执行的方法
     *
     * @return
     */
    public String helloError() {
        return "server is not error";
    }
}
package com.mo;

import com.mo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * EnableHystrix 开启熔断器功能
 *
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/6 22:42
 */
@RestController
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class HelloApplication {

    @Autowired
    private HelloService helloService;

    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @GetMapping("/hello")
    public String hello() {
        return helloService.hello();
    }
}

三、搭建服务消费端feign-hystrix 

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mo-cloud</artifactId>
        <groupId>com.mo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>feign-hystrix</artifactId>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- feign集成了熔断器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
</project>
server:
  port: 0
spring:
  application:
    name: feign-hystrix
eureka:
  client:
    service-url:
      default-zone: http://127.0.0.1:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${random.int}
# feign默认集成了hystrix,开启feign的熔断
feign:
  hystrix:
    enabled: true
package com.mo.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * FeignClient的fallback实现熔断机制
 * fallback调用实现类的对应实现方法
 *
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/6 23:53
 */
@FeignClient(value = "SERVICE-HELLO", fallback = HelloService.HelloServiceImpl.class)
public interface HelloService {

    /**
     * 通过feign调用指定服务
     *
     * @return
     */
    @GetMapping("/hello")
    String hello();

    /**
     * 使用内部类实现Feign的接口
     */
    @Component
    class HelloServiceImpl implements HelloService {
        @Override
        public String hello() {
            return "server error";
        }
    }
}
package com.mo;

import com.mo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author x.pan
 * @email px5215201314@163.com
 * @date 2020/4/6 23:52
 */
@RestController
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignHystrix {

    @Autowired
    private HelloService helloService;

    public static void main(String[] args) {
        SpringApplication.run(FeignHystrix.class,args);
    }

    @GetMapping("/hello")
    public String hello(){
        return helloService.hello();
    }
}

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值