spring cloud之hystrix

1.依賴(lài)引入

        <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.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
<!--hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
<!--hystrix儀表盤(pán)-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

2.配置

server:
  port: 6065

logging:
  level:
    com.d4c: debug

eureka:
  instance:
    prefer-ip-address: false
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://peer1:6001/eureka/,http://peer2:6002/eureka/
spring:
  cloud:
    loadbalancer:
      retry:
        enabled: true # 開(kāi)啟Spring Cloud的重試功能
  application:
    name: consumer-hystrix

ribbon:
  restclient:
    enabled: true  #沒(méi)有這個(gè)ribbon超時(shí)配置不會(huì)生效,是
  ReadTimeout: 1000
  ConnectTimeout: 300
  MaxAutoRetries: 1 #同一臺(tái)實(shí)例最大重試次數(shù),不包括首次調(diào)用
  MaxAutoRetriesNextServer: 1 #重試負(fù)載均衡其他的實(shí)例最大重試次數(shù),不包括首次調(diào)用
  #當(dāng)OkToRetryOnAllOperations設(shè)置為false時(shí),只會(huì)對(duì)get請(qǐng)求進(jìn)行重試。
  #如果設(shè)置為true,便會(huì)對(duì)所有的請(qǐng)求進(jìn)行重試,如果是put或post等寫(xiě)操作,如果服務(wù)器接口沒(méi)做冪等性,會(huì)產(chǎn)生不好的結(jié)果,所以O(shè)kToRetryOnAllOperations慎用。
  OkToRetryOnAllOperations: false  #是否所有操作都重試

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 6000 # 設(shè)置hystrix的超時(shí)時(shí)間為6000ms

3.

啟動(dòng)類(lèi)

@SpringBootApplication
@EnableDiscoveryClient//開(kāi)啟服務(wù)發(fā)現(xiàn)
@EnableCircuitBreaker//開(kāi)啟熔斷器
@EnableHystrixDashboard//開(kāi)啟hystrix儀表盤(pán)
public class ConsumerHystrixpplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerHystrixpplication.class, args);
    }
}

配置類(lèi)

@Configuration
public class CommonConfig {
    //方式二
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}

service

@Service
public class AccountConsumerService {

    @Resource
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public String hystrixCommand(Long id) throws Exception {
        String url = "http://account-demo/account/get/" + id;
        String forObject = restTemplate.getForObject(url, String.class);
        System.out.println("forObject = " + forObject);
        return forObject;
    }

    public String fallback(Long id) {
        return "I am fallback!";
    }
}

controller

@RestController
@RequestMapping("consumer")
public class AccountConsumerController {

    @Resource
    private AccountConsumerService accountConsumerService;

    @RequestMapping("fallback/{id}")
    public String fallback(@PathVariable Long id) throws Exception{
        String s = accountConsumerService.hystrixCommand(id);
        return s;
    }
}

參考 參考Spring Cloud官方文檔第13、14、15章

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容