SpringCloud(5)-使用斷路器

由於網絡原因或者自身的原因,服務並不能保證100%可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求涌入,Servlet容器的線程資源會被消耗完畢,導致服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的“雪崩”效應。

爲了解決這個問題,業界提出了斷路器模型

斷路器的使用

  1. pom.xml的配置依賴

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    
  2. application.properties的配置

    server.port=85
    spring.application.name=consume
    eureka.client.service-url.defaultZone=http://master:81/eureka/
    feign.hystrix.enabled=true
    
  3. 啓動類

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.hystrix.EnableHystrix;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableFeignClients
    @EnableDiscoveryClient
    @EnableHystrix
    @EnableHystrixDashboard
    public class EurekaHelloConsumeApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaHelloConsumeApplication.class, args);
        }
    }
    
  4. Feign請求的接口類

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @FeignClient(value = "provider",fallback = FeignServiceImpl.class)
    public interface FegionService {
    
        @RequestMapping(method = RequestMethod.GET)
         String feignHello(@RequestParam(value = "name") String name);
    }
    
  5. 在fegin中使用,寫一個feginServiceImpl繼承接口FegionService

    import org.springframework.stereotype.Component;
    
    @Component
    public class FeignServiceImpl implements FegionService {
        @Override
        public String feignHello(String name) {
            return "暫時無法訪問";
        }
    }
    
  6. 在Ribbon中使用斷路器

    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    @Service
    public class ConsumeService {
        @Autowired
        private RestTemplate restTemplate;
        @HystrixCommand(fallbackMethod = "consumeHelloError")
        public String consumeHello(){
            return restTemplate.getForObject("http://provider?name=唐陳",String.class);
        }
        public String consumeHelloError(){
            return "暫時無法訪問";
        }
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章