java version of spring cloud microservice architecture b2b2c e-commerce platform -- basic configuration of Hystrix

Posted by LeslieHart on Tue, 22 Oct 2019 22:48:59 +0200

1. [microcloud provider Dept Hystrix-8001] modify pom.xml configuration file, and add Hystrix configuration class:

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

2. [microcloud provider Dept hystrix-8001] modify the DeptRest program

package cn.study.microcloud.rest;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import cn.study.microcloud.service.IDeptService;
import cn.study.vo.Dept;

@RestController
public class DeptRest {
    @Resource
    private IDeptService deptService;
    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod="getFallback")    // If there is an error in the get() method that is currently invoked, execute fallback.
    public Object get(@PathVariable("id") long id) {
        Dept vo = this.deptService.get(id) ;    // Receive query results of database
        if (vo == null) {    // Data doesn't exist, let's say it throws an error
            throw new RuntimeException("Department information does not exist!") ;
        }
        return vo ;
    }
    public Object getFallback(@PathVariable("id") long id) {    // At this time, the parameters of the method are the same as those of get().
        Dept vo = new Dept() ;
        vo.setDeptno(999999L);
        vo.setDname("[ERROR]Microcloud-Dept-Hystrix");    // Wrong prompt
        vo.setLoc("DEPT-Provider");
        return vo ;
    }
    
    
}

Once the get() method throws an error message, it is considered that there is a problem with the service. By default, the fallbackMethod configured in the "@ HystrixCommand" annotation is used to call the specified method in the class and return the corresponding data.

3. [microcloud provider Dept hystrix-8001] start fusing in the main class.

package cn.study.microcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
public class Dept_8001_StartSpringCloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(Dept_8001_StartSpringCloudApplication.class, args);
    }
}

The current processing situation is: if the server has an error (but does not mean that the provider is closed), then the fallback processing of the specified method will be called.

Topics: xml Spring REST Database