• 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,如图

  • 相关阅读:
    ubuntu 用shell脚本实现将当前文件夹下全部文件夹中的某一类文件复制到同一文件夹下
    读书笔记-2java虚拟机的可达性算法与finalize方法
    find the longest of the shortest (hdu 1595 SPFA+枚举)
    杭电 2176 取(m堆)石子游戏(博弈)
    MVC框架的优缺点
    Wireshark-TCP协议分析(包结构以及连接的建立和释放)
    Ubuntu安装教程--Win7系统中含100M保留分区
    eclipse新建android项目出现非常多错误
    关于简单的加密和解密算法
    在一台server上部署多个Tomcat
  • 原文地址:https://www.cnblogs.com/hellxz/p/8889017.html
Copyright © 2020-2023  润新知