zoukankan      html  css  js  c++  java
  • SpringCould-------使用Hystrix 实现断路器进行服务容错保护

    消费

    <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.sam</groupId>
        <artifactId>hello-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <javaVersion>1.8</javaVersion>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR6</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
        <dependencies>
            <!-- 引入eureka 客户端依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!-- 引入ribbon 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.6.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 引入hystrix 依赖 ,用来实现服务容错保护-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-hystrix</artifactId>
            </dependency>
        </dependencies>
    </project>
    
           

    启动类:

    /*@SpringBootApplication + 
    @EnableDiscoveryClient+ 
    @EnableCircuitBreaker
      =  @SpringCloudApplication */
                        
                         
    @SpringCloudApplication
    public class CustomerApplication {
        //@Bean 应用在方法上,用来将方法返回值设为为bean
        @Bean
        @LoadBalanced  //@LoadBalanced实现负载均衡
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
        
        public static void main(String[] args) {
            SpringApplication.run(CustomerApplication.class, args);
        }
    
    }

    service:

    @Service
    public class ConsumerService {
        
        @Autowired
        RestTemplate restTemplate;
        
    
        @HystrixCommand(fallbackMethod = "errorMsg")
        public String consumer() {
           
            return restTemplate.getForObject("http://hello-service/hello", String.class);
        }
        
           /** 
            * 由 @HystrixCommand(fallbackMethod = "errorMsg")
            * 可知 在起多个服务时,如果其中一个服务宕机,则会调用 errorMsg()
            * 方法; 相比在不加@EnableCircuitBreaker断路器,的包500d的错
            */
           
        public String errorMsg() {
            return "error!!!";
        }
    }

    controller:

    @RestController
    public class ConsumerController {
    
        //这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
        /*@Autowired
        RestTemplate restTemplate;*/
        
        @Autowired
        ConsumerService consumerService;
    
        @RequestMapping("/consumer")
        public String helloConsumer() {
            //调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port
          //  return restTemplate.getForObject("http://hello-service/hello", String.class);
            return consumerService.consumer();
        }
    }
  • 相关阅读:
    scp 跨服务器传数据
    Mongo启动失败解决方案
    centos7 NET模式配置虚拟机
    centos7虚拟机配置桥接模式
    Linux centos7 查看cpu 磁盘 内存使用情况
    centos7修改时间和时区
    fiddler培训
    docker学习笔记
    docker-ce安装官翻
    Nginx+Tomcat简单负载均衡
  • 原文地址:https://www.cnblogs.com/leeego-123/p/10535335.html
Copyright © 2011-2022 走看看