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 "暂时无法访问";
        }
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章