SpringCloud入门(十九) ------使用 Hystrix 进行服务熔断

服务熔断与服务降级的区别:

服务熔断相当于保险丝,当服务因为某些原因频繁的调用失败的时候,就会开启断路器,将该服务熔断,等过一段时间,服务会尝试打开(半开状态),如果调用成功了,就再次开通服务。
服务熔断与服务降级的关系:
服务熔断之前必然会降级,不管是服务的宕机,运行异常还是超时,都会执行降级的方法(HystrixCommand注解)

Hystrix 进行服务熔断:

pom:

父pom管理的j依赖版本:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.6</mysql.version>
        <durid.version>1.1.11</durid.version>
        <mybatis.spring.boot.version>1.1.1</mybatis.spring.boot.version>
    </properties>

    <!-- 子模块继承后,提供作用:锁定版本+子模块不用写groupId 和 version -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${durid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>jym-springCloud-parent</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

hystrix 依赖:

		<!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

完整的pom依赖:

	<dependencies>
        <dependency>
            <groupId>com.jym.springCloud</groupId>
            <artifactId>jym-common-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId></dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>

yml:
server:
  port: 8003

spring:
  application:
    name: jym-provider-payment  # 微服务名称

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: jymServiceCloud-payment8002  #修改eureka 	Status中的内容

启动类:
@SpringBootApplication
@EnableHystrix
@EnableCircuitBreaker //启用服务降级
public class HystrixApplication8003 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixApplication8003.class,args);
    }
}
service类:如果在10秒内请求了10次并且失败率达到了百分之60,就会启用断路器
@Service
public class PaymentService  {

    /**
     *  意思是在10秒内 请求10次 失败率达到60  会启动断路器
     */
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name="circuitBreaker.enabled",value = "true"),   // 是否开启断路器
            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"), // 请求次数
            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"),  // 时间窗口期
            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60")  // 失败率达到多少跳闸
    })
    public String paymentCircuitBreaker(int id) {
        if(id < 0) {
            throw new RuntimeException("******不能为负数*******");
        }
        String number = UUID.randomUUID().toString().replace("-","");
        return number;
    }

    public String paymentCircuitBreaker_fallback (int id) {
        return "id 不能为负数,请稍后再试,id:"+id;
    }


}
controller:
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @GetMapping("/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") int id) {
        return paymentService.paymentCircuitBreaker(id);
    }

}

学习年限不足,知识过浅,说的不对请见谅。

世界上有10种人,一种是懂二进制的,一种是不懂二进制的。

发布了122 篇原创文章 · 获赞 77 · 访问量 101万+
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览