SpringCloud(六) Hystrix入門


前提

  • 一個可用的Eureka注冊中心(文中以之前博客中雙節點注冊中心,不重要)
  • 一個連接到這個注冊中心的服務提供者

快速入門

項目搭建

搭建一個新maven項目,artifactid為Ribbon-consum-hystrix,依賴是在ribbon-customer項目上加入hystrix依賴,這里直接給出,pom.xml配置如下:

<?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.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>
        <!--暴露各種指標-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--hystrix熔斷器-->
        <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>

新建包com.cnblogs.hellxz,新建應用入口類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熔斷器使用HelloWorld
 * @Date : 2018/4/20 10:03
 */
@SpringCloudApplication
public class RibbonApplication {

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

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

resources目錄下添加配置文件application.yml,其中defaultZone的url為實際注冊中心defaultZone地址

server:
  port: 8088

spring:
  application:
    name: ribbon-hystrix

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/

在com.cnblogs.hellxz包下分別新建controller包和service包,controller包中新建RibbonController,代碼如下:

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消費者controller
 * @Date : 2018/4/20 10:07
 */
@RestController
public class RibbonController {

    @Autowired
    RibbonService service;

    @GetMapping("/hystrix/test")
    public String helloHystrix(){
        //調用服務層方法
        return service.helloService();
    }

}

service包下新建RibbonService,代碼如下:

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服務層
 * @Date : 2018/4/20 10:08
 */
@Service
public class RibbonService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hystrixFallback")
    public String helloService(){
        //調用服務提供者接口,正常則返回hello字符串
        return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
    }

    /**
     * 調用服務失敗處理方法
     * @return “error"
     */
    public String hystrixFallback(){
        return "error";
    }
}

項目最后的結構如圖

測試

分別啟動 注冊中心--->服務提供者--->剛新建的項目
訪問http://localhost:8088/hystrix/test
因為現在服務是正常的,返回了hello,如圖

此時關閉服務提供者項目,再次訪問,返回了error,如圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM