栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud之Hystrix及声明式服务调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud之Hystrix及声明式服务调用

SpringCloud之Hystrix及声明式服务调用

文章目录
  • SpringCloud之Hystrix及声明式服务调用
  • 一、项目准备
    • 1.pom文件(先集成在服务提供方)
    • 2.编写服务提供方启动类
    • 3.创建服务提供方测试类controller
    • 4.创建服务消费方进行测试
      • 1.pom文件
      • 2.yml文件
      • 3.启动类
      • 4.使用@FeignClient进行远程调用服务提供方
      • 5.创建消费方测试类controller
      • 6. **注:yml文件未设置feign超时间设置报错如下**


一、项目准备 1.pom文件(先集成在服务提供方)



  4.0.0

  com.yun
  cloud-provider
  1.0-SNAPSHOT

  cloud-provider
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    


    
      org.springframework.cloud
      spring-cloud-starter-netflix-hystrix
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    
  

  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Edgware.SR5
        pom
        import
      
    
  



2.编写服务提供方启动类
package com.yun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker //这里使用注解开启Hystrix
public class CloudProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudProviderApplication.class);
    }
}
3.创建服务提供方测试类controller
package com.yun;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/hello")
public class TestController {

    @GetMapping("/testOne")
    public String hello(){
        return "你好呀,Hystrix";
    }
    //使用 @HystrixCommand:   fallbackMethod出现熔断是调用方法快速返回。commandProperties进行参数的配置。
    @HystrixCommand(fallbackMethod = "requestFailed",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")})
    @GetMapping("/testTwo")
    public String timeout(){

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "来了就别走了!";
    }

    public String requestFailed(){
        return "请求失败!!!";
    }
}

因为这个时候的超时时间为5秒,所以返回正常。

下面我们将超时时间改为2秒,返回下图说明@HystrixCommand配置起作用了:

4.创建服务消费方进行测试 1.pom文件



  4.0.0

  com.yun
  cloud-consumer
  1.0-SNAPSHOT

  cloud-consumer
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    


    
      org.springframework.cloud
      spring-cloud-starter-netflix-hystrix
    

    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    

    
      org.springframework.cloud
      spring-cloud-starter-openfeign
    
  

  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Edgware.SR5
        pom
        import
      
    
  


2.yml文件
server:
  port: 7002

eureka:
  client:
    service-url:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
  instance:
    hostname: localhost

spring:
  application:
    name: cloud-consumer
feign: #这里一定要设置feign的超时时间配置,不设置会报错。
  hystrix:
    enbale: false #如果处理自身的容错就开启。开启方式与生产端不一样。
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 600000
3.启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;


@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix //服务熔断,降级
public class CloudConsumberApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudConsumberApplication.class);
    }
}
4.使用@FeignClient进行远程调用服务提供方
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;


@Component
//CLOUD-PROVIDER是提供方在服务中心注册的名称
@FeignClient(value = "CLOUD-PROVIDER")
public interface TestService {

    @GetMapping("/hello/testOne")
    public String hello();

    @GetMapping("/hello/testTwo")
    public String testTwo();
}
5.创建消费方测试类controller
package com.yun;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/cloud")
public class CloudConsumberController {

    //远程服务service
    @Autowired
    private TestService testService;

    //自身启动服务降级
    @HystrixCommand(fallbackMethod = "selfDowngrade",commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")})
    @RequestMapping("/test")
    public String test(){
        return testService.testTwo();
    }
    public String selfDowngrade(){
        return "自身降级服务启动";
    }
}


6. 注:yml文件未设置feign超时间设置报错如下

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号