Spring Cloud's Hystrix fuse

Posted by jjacquay712 on Thu, 26 Dec 2019 17:38:48 +0100

The fuse mechanism is a self-protection mechanism. When there are a large number of requests piled up and the service is unable to handle them immediately, the fuse mechanism can decide to discard the requests to ensure that its resources are not exhausted and downtime. The fuse is usually used on the service consumers. When the service callers consume the service in a single time and the number of errors reaches the set limit, the service consumers will not be called, Instead, call the set callback function. In Feign, Hystrix is integrated and easy to use. Create a new Maven Module, pom.xml as follows:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wang</groupId>
    <artifactId>aServiceConsumerFeignHystrix</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--Version number control-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

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

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.6.2</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

application.properties are as follows:

server.port=8883
spring.application.name=service-consumer-feign-hystrix
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
feign.hystrix.enabled=true

Controller class:

package com.wang.controller;

import com.wang.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by cheng on 2018/9/23.
 */
@RestController
public class FeignController {

    @Autowired
    FeignService feignService;

    @GetMapping(value = "/feign")
    public String feign(@RequestParam String name){
        return feignService.feignFun(name);
    }
}

Service interface:

@FeignClient(value="service-a",fallback = FeignServiceImpl.class)
public interface FeignService {

    @RequestMapping(value="/aservice", method = RequestMethod.GET)
    String feignFun(@RequestParam(value = "name") String name);
}

Service implementation class:

@Component
public class FeignServiceImpl implements FeignService {

    @Override
    public String feignFun(String name) {
        return "hystrix do " + name;
    }
}

Startup class:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerFeignHystrixApp {

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

 

Topics: Spring Maven Apache xml