Spring Cloud Ribbon Integration Hystrix

Source: Internet
Author: User

Based on the Ribbon engineering of the ribbon of Spring Cloud , the previous essay

1.pom.xml

Join the dependency

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

That is, Pom.xml is

<?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.dzpykj</groupId> <artifactId>ribbon</artifactId> <v Ersion>0.0.1-snapshot</version> <packaging>jar</packaging> <name>ribbon</name> & Lt;description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> <relativepath/> <!--lookup parent from repository--</parent> <properties> &LT;PR Oject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties&    Gt <dependencyManagement> <dependencies> <dependency> <groupid>org.s                Pringframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scop E>import  </scope> </dependency> </dependencies> </dependencyManagement> <depende ncies> <dependency> <groupId>org.springframework.boot</groupId> <arti factid>spring-boot-starter-web</artifactid> </dependency> <dependency> <g Roupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid&gt            ; <scope>test</scope> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGF Ramework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> &LT;/DEP endency> <dependency> <groupId>org.springframework.cloud</groupId> <a rtifactid>spring-cloud-starter-hystrix</artifactid> </dependency> </dependencies> <bu    Ild>    <plugins> <plugin> <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>            </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https://repo.spring.io/milestone</url><snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>

2.APPLICATION.YML file configuration is not changed

Spring:  application:    name:ribbonserver:  8765Eureka:  client:    serviceurl:      Defaultzone:http://localhost:8761/eureka/

3. Start class join @enablecircuitbreaker annotation Open Hystrix

 Packagecom.dzpykj;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.web.client.RestTemplateBuilder;ImportOrg.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;Importorg.springframework.cloud.client.loadbalancer.LoadBalanced;Importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.web.client.RestTemplate, @SpringBootApplication @enableeurekaclient@enablecircuitbreaker //@EnableCircuitBreaker notes indicate the use of circuit breakers Public classribbonapplication {@AutowiredPrivateResttemplatebuilder Builder; @Bean @LoadBalanced Publicresttemplate resttemplate () {returnBuilder.build (); }//@Bean//@LoadBalanced//resttemplate resttemplate () {//return new Resttemplate ();//    }     Public Static voidMain (string[] args) {Springapplication.run (ribbonapplication.class, args); }}

4. New HelloService class

Emphasis is on @hystrixcommand annotations, callback methods when Fallbackmethod is an exception

 PackageCom.dzpykj.hystrixService;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.stereotype.Service;Importorg.springframework.web.client.RestTemplate;ImportCom.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service Public classHelloService {@Value ("${server.port}") String Port;        @Autowired resttemplate resttemplate; @HystrixCommand (Fallbackmethod= "Helloerrorfallback")     Publicstring Hello (string name) {returnResttemplate.getforobject ("Http://eurekaclient/hi?name=" +name, String.class); }         Publicstring Helloerrorfallback (string name) {return"Sorry" +name+ "When you were visting ribbon project,port:" +port+ ", you meet an error"; }}

5. Retrofit Hellocontroller, call HelloService's Hello method

 PackageCom.dzpykj.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;Importorg.springframework.web.client.RestTemplate;ImportCom.dzpykj.hystrixService.HelloService; @RestController Public classHellocontroller {@Autowired resttemplate resttemplate;        @Autowired HelloService HelloService; @RequestMapping ("/hello/{name}")     Publicstring Hello (@PathVariable string name) {returnResttemplate.getforobject ("Http://eurekaclient/hi?name=" +name, String.class); } @RequestMapping ("/hi/{name}")     Publicstring Hi (@PathVariable string name) {returnHelloservice.hello (name); }}

6. Prepare to complete and start testing. Start Eureka Service cluster, Eureka Single client, ribbon engineering

6.1 Start the Eureka service Peer1,peer2 cluster by following the steps of the Spring Cloud Eureka server cluster Demo-level setup

6.2 Launch 8763 Eureka client based on Spring Cloud Eureka Service Demo level

6.3 Starting the Ribbon Project

7. Visit Http://localhost:8765/hi/chaixy

8. Impersonate the Eurekaclient service exception: Manually shut down the Eurekaclient service and revisit the Http://localhost:8765/hi/chaixy

You can see that when the Eurekaclient service shuts down, access encounters an exception, callbacks the exception when the callback method Helloerrorfallback method (method within the HelloService class)

Spring Cloud Ribbon Integration 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.