Use of springcloud-----fuse Hystrix

Source: Internet
Author: User

1. Resttemplate+ribbon using Hystrix

1.1 Introducing Dependencies

<!---<dependency>    <GroupId  >org.springframework.cloud</groupId>    <  artifactid>spring-cloud-starter-hystrix</artifactid > </ Dependency >

1.2 Adding annotations on methods that require fusing

@Service Public classHiservice {@Autowired resttemplate resttemplate; //a way to fuse@HystrixCommand (fallbackmethod= "Hierror")//method of execution after fusing     PublicString Sayhi () {returnResttemplate.getforobject ("Http://SERVICE-HI/info", String.class); }        //method of execution after fusing     PublicString Hierror () {return"Sorry hi Error"; }}

1.3 Declaring the use of Hystrix in the startup class

@SpringBootApplication @enablediscoveryclient//Register with the service center@RestController @enablehystrix//enabling the fusing mechanism Public classConsumerribbon {@AutowiredPrivateHiservice Hiservice;  Public Static voidMain (string[] args) {Springapplication.run (Consumerribbon.class, args); } @Bean @LoadBalanced//Use this resttemplate to turn on load balancingresttemplate initresttemplate () {return Newresttemplate (); } @RequestMapping ("Info")     Publicstring Hiconsumer () {string response=Hiservice.sayhi (); returnresponse; }}

1.4 Start Registry and Cloud-consumer-ribbon, Access http://localhost:8764/info return sorry hi error

Start Service-hi, access Http://localhost:8764/info return Hello Eureka client 8762

2 Feign using Hystrix

2.1 Feign comes with a fuse, no need to import hystrix dependencies, but need to import the following dependencies, otherwise return java.lang.noclassdeffounderror:com/netflix/hystrix/contrib/ Javanica/aop/aspectj/hystrixcommandaspect Error

< Dependency >    < groupId >com.netflix.hystrix</groupId>    <  Artifactid>hystrix-javanica</artifactid></  dependency>

2.2 Hystrix is enabled in the configuration file, which is turned off by default

Feign:  hystrix:   enabled:true

2.3 Specify the class to be executed after the fuse

@FeignClient (value= "Service-hi", Fallback=hiservicehystric.  Class)// specifies which service provider to invoke, specifies the type of execution after the fuse interface  ihiservice {        @ Requestmapping (Value= "/info", Method=requestmethod.get)// specifies which interface to invoke the service provider     String info ();        @RequestMapping (Value= "/info", Method=requestmethod.get)// Specifies which interface     to invoke the service provider String hi ();}

2.4 Specify the corresponding method after fusing

@Component  Public class Implements Ihiservice {    // fused after executing the appropriate method public      String info () {          return "Sorry info feign";    }      Public String hi () {        return "Sorry hi feign";    }}

2.5 Declaring the startup Hystrix in the Startup class

@EnableHystrix

2.6 Start Registry and cloud-consumer-feign, Access http://localhost:8765/info return Sorry info feign

Start Service-hi, access Http://localhost:8765/info return Hello Eureka client 8762

Use of springcloud-----fuse Hystrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.