四、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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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 [email protected]
 * @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();
    }
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章