zoukankan      html  css  js  c++  java
  • springCloud Hystrix 断路由

    第一步加入依赖:

      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-hystrix</artifactId>

        <version>1.2.6.RELEASE</version>

      </dependency>

    第二步:编写配置文件application.properties

      spring.application.name=ribbon-hystrix
      server.port=8787
      eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
      eureka.instance.metadata-map.zone=beijing
      spring.cloud.loadbalancer.retry.enabled=true

      #hystrix.command.default.execution.timeout.enabled=false
      hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

    第三步:编写启动类

    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;

    @SpringCloudApplication
    public class EurekaClientRibbonHystrixApplication {

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

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

    第四步:测试

      

    @RestController
    public class ConsumerController {
      @Autowired
      RestTemplate restTemplate;

      @Autowired
      DiscoveryClient client;

      @RequestMapping(value = "/add", method = RequestMethod.GET)
      @HystrixCommand(fallbackMethod = "fallback" )
      public String add() throws InterruptedException {

        return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
      }

      public String fallback(){
        return "error";
      }
    }

  • 相关阅读:
    while循环和do while循环的基本使用和区别
    less框架简介
    css关联选择器大致类型总结
    渐进增强和优雅降级
    行,行内元素与块级元素有什么不同?
    for循环的大概遍历运用
    JDBC连接mysql数据库并进行简单操作
    Java实现杨辉三角
    replaceAll() 方法
    java抽象类和接口的区别
  • 原文地址:https://www.cnblogs.com/gslblog/p/7171100.html
Copyright © 2011-2022 走看看