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