Spring-cloud (vi) hystrix INTRODUCTION

Source: Internet
Author: User

Premise
    • An available Eureka registry (text to the previous blog in the two-node registry, not important)
    • A service provider who connects to this registry
Quick Start Project Setup

Build a new Maven project, Artifactid for Ribbon-consum-hystrix, rely on the Ribbon-customer project to add Hystrix dependency, here directly, pom.xml configuration is as follows:

<?xmlVersion= "1.0" encoding= "UTF-8"?><projectxmlns="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.cnblogs.hellxz</groupId>    <artifactId>Ribbon-consum-hystrix</artifactId>    <version>1.0-snapshot</version>    <parent>        <groupId>Org.springframework.boot</groupId>        <artifactId>Spring-boot-starter-parent</artifactId>        <version>1.5.9.RELEASE</version>        <relativePath/>    </parent>    <dependencies>        <dependency>            <groupId>Org.springframework.boot</groupId>            <artifactId>Spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>Org.springframework.cloud</groupId>            <artifactId>Spring-cloud-starter-eureka</artifactId>            <version>RELEASE</version>        </dependency>        <dependency>            <groupId>Org.springframework.cloud</groupId>            <artifactId>Spring-cloud-starter-ribbon</artifactId>            <version>RELEASE</version>        </dependency>        <!--exposure to a variety of indicators -        <dependency>            <groupId>Org.springframework.boot</groupId>            <artifactId>Spring-boot-starter-actuator</artifactId>        </dependency>        <!--hystrix fuses --        <dependency>            <groupId>Org.springframework.cloud</groupId>            <artifactId>Spring-cloud-starter-hystrix</artifactId>            <version>RELEASE</version>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>Org.springframework.cloud</groupId>                <artifactId>Spring-cloud-dependencies</artifactId>                <version>RELEASE</version>                <scope>Import</scope>                <type>Pom</type>            </dependency>        </dependencies>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupId>Org.springframework.boot</groupId>                <artifactId>Spring-boot-maven-plugin</artifactId>            </plugin>            <plugin>                <groupId>Org.apache.maven.plugins</groupId>                <artifactId>Maven-compiler-plugin</artifactId>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                </configuration>            </plugin>        </plugins>    </build></project>

New Package Com.cnblogs.hellxz, new app entry class Ribbonapplication:

Package Com.cnblogs.hellxz;import org.springframework.boot.SpringApplication;import org.springframework.cloud.client.SpringCloudApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import Org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;/*** @Author: Hellxz* @Description: hystrix fuses use HelloWorld* @Date: 2018/4/20 10:03 */@SpringCloudApplication Public classribbonapplication {@Bean    @LoadBalancedResttemplategetresttemplate(){return New resttemplate(); } Public Static void Main(string[] args) {springapplication.Run(Ribbonapplication.class, args); }}

Resources directory to add the configuration file Application.yml, where the Defaultzone URL is the actual registry Defaultzone address

server:  port: 8088spring:  application:    name: ribbon-hystrixeureka:  client:    serviceUrl:      defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/

Under the COM.CNBLOGS.HELLXZ package to create a new controller package and service package, the controller package to create a new Ribboncontroller, the code is as follows:

Package Com.cnblogs.hellxz.controller;import Com.cnblogs.hellxz.servcie.RibbonService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import Org.springframework.web.bind.annotation.RestController;/*** @Author: Hellxz* @Description: Ribbon consumer Controller* @Date: 2018/4/20 10:07 */@RestController Public classRibboncontroller {@AutowiredRibbonservice Service;@GetMapping("/hystrix/test") PublicStringHellohystrix(){//Call the service layer method        returnService.HelloService(); }}

Under Service package, create a new ribbonservice with the following code:

Package Com.cnblogs.hellxz.servcie;import Com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import org.springframework.beans.factory.annotation.Autowired;import Org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;/*** @Author: Hellxz* @Description: Ribbon service Layer* @Date: 2018/4/20 10:08 */@Service Public classRibbonservice {@Autowired    PrivateResttemplate resttemplate;@HystrixCommand(Fallbackmethod ="Hystrixfallback") PublicStringHelloService(){//Call the service Provider interface and return the Hello string as normal        returnResttemplate.getforentity("Http://hello-service/hello", String.class).GetBody(); }/*** Call service failure handling method     * @return"Error"     */     PublicStringHystrixfallback(){return "Error"; }}

Final structure of the project

Test

Start 注册中心 ---> 服务提供者 ---separately >刚新建的项目
Visit http://localhost:8088/hystrix/test
Because now the service is normal and returned hello ,

The service provider project is now closed, accessed again, returned error ,

Spring-cloud (vi) hystrix INTRODUCTION

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.