Spring Boot Spring Cloud B2B2C o2o distributed micro-service Part IV: Breaker (Hystrix) (Finchley version) -B2B2C small e-commerce program

I. Introduction breaker

A Library has the Created Called Netflix Hystrix that the implements at The Circuit Breaker pattern an In A microService.
Architecture IT IS to have have the Common Multiple Layers of Service Calls ---- taken from the official website.
There are spring cloud b2b2c e-commerce needs of friends can add penguin beg: 3536247259

Second, prepare for work
This article is based on an article in the engineering, construction started on the first article, start the eureka-server project; project start service-hi, the port is 8762.

Third, the use of circuit breakers in the ribbon
code for the transformation of serice-ribbon project, first started adding dependent spring-cloud-starter-netflix- hystrix in pox.xml file:

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

In the startup class program ServiceRibbonApplication plus @EnableHystrix comment open Hystrix:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {

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

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

}

Transformation HelloService class, on hiService method plus @HystrixCommand comment. The comment created a fuse function of the method and specify the method fallbackMethod fuse, fuse method returns a string, the string is "hi," + name +, the following code ", sorry, error!":

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}

Start: service-ribbon project, when we visit http: // localhost:? 8764 / hi name = forezp, the browser displays:

hi forezp,i am from port:8762

Now closed service-hi project, when we visit http: // localhost:? 8764 / hi name = forezp, the browser will display:

hi ,forezp,orry,error!

This shows that when the service-hi project is not available, when the service-ribbon service-hi call API interface performs fast failure to return a set of strings directly, rather than waiting for a response time out, which is good control of the vessel the thread blocks.

Four, Feign used in circuit breakers
Feign are self-breaker, after the D version of the Spring Cloud, it is not turned on by default. You need to configure to open it in the configuration file, the configuration file add the following code:

feign.hystrix.enabled=true

Based on service-feign transformation project, just in FeignClient of SchedualServiceHi notes plus the interface of the specified class fallback on the line:

@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

SchedualServiceHiHystric SchedualServiceHi need to implement the interface, and Ioc is injected into the container, as follows:

@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}

Start four servcie-feign project browser to open http: // localhost:? 8765 / hi name = forezp, note that at this time the project does not start service-hi, web page display:

sorry forezp

Open service-hi engineering, accessed again, the browser displays:

hi forezp,i am from port:8762

This proves that the circuit breaker played a role.

Published 12 original articles · won praise 35 · views 433

Guess you like

Origin blog.csdn.net/m0_46413054/article/details/104769804