Spring Cloud Finchley版本Demo笔记,服务通信RestTemplate中使用Hystrix进行服务降级和超时设置

本文demo是在Spring Cloud Finchley版本Demo笔记,服务之间的通信RestTemplate和Feign及负载均衡基础上搭建的

一、在consumer模块pom.xml加上熔断依赖spring-cloud-starter-netflix-hystrix

<?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>
    <parent>
        <groupId>com.cobra</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.cobra</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>consumer</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--使用feign添加依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!--熔断依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、RestTemplate中熔断的使用

1、在注入RestTemplate的类加上注解@EnableHystrix,使熔断生效

package com.cobra.consumer.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: Baron
 * @Description:
 * @Date: Created in 2019/3/12 17:18
 */
@Configuration
@EnableHystrix
public class RestTemplateConfig {

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

2、在使用RestTemplate的方法上加上@HystrixCommand(fallbackMethod = "fallback"),fallbackMethod 赋上方法值同时写上方法:

package com.cobra.consumer.controller;

import com.cobra.consumer.client.ProviderClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: Baron
 * @Description:
 * @Date: Created in 2019/3/12 15:57
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 测试通讯
     * @return
     */
    @RequestMapping("/connect")
    @HystrixCommand(fallbackMethod = "fallback")
    public String connect(){

        String result =restTemplate.getForObject("http://provider/provider/get", String.class);

        return "connect to " + result;
    }

    public String fallback() {
        return "RestTemplate发生熔断了!!!";
    }

}

3、测试,启动eureka和consumer,发送请求得到下面结果,说明熔断生效:

二、超时配置

1、在consumer配置文件上加上超时配置,这里设置6秒的超时时间:

hystrix:
  command:
    #默认全部方法
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 6000

三、验证

1、改造ProviderController,加入Thread.sleep(5000);

package com.cobra.provider.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Baron
 * @Description:
 * @Date: Created in 2019/3/12 16:02
 */
@RestController
@RequestMapping("/provider")
@Slf4j
public class ProviderController {

    @GetMapping("/get")
    public String get() {
        log.info("visit getProvider!");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "i am getProvider";
    }

    @PostMapping("/post")
    public String post() {
        log.info("visit postProvider!");
        return "i am postProvider";
    }

}

2、测试,启动eureka、provider、consumer,发送请求得到:

3、将yml文件中的超时配置去掉,重启,发送请求,发生熔断,如下图,说明配置有效:

4、将配置文件中的default改为connect(类上的映射路径):

hystrix:
  command:
    connect:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 6000

5、重启测试,超时时间配置依然有效:

6、在ConsumerController添加新的映射方法:

    /**
     * 测试通讯1
     * @return
     */
    @RequestMapping("/connect1")
    @HystrixCommand(fallbackMethod = "fallback")
    public String connect1(){

        String result =restTemplate.getForObject("http://provider/provider/get", String.class);

        return "connect to " + result;
    }

7、重新启动,访问localhost:8082/consumer/connect1:

8、小结:需要对指定方法进行超时设置,将default改为方法上的映射名即可,否则对所有类内所有的方法有效

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值