第八节 Dubbo的熔断

亮子 2021-08-31 18:00:36 17191 0 0 0

1、添加依赖

        <!--熔断依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

2、启动类添加注解

package com.shenmazong.serveruserconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@SpringBootApplication
@EnableHystrix
public class ServerUserConsumerApplication {

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

}

3、增加熔断函数

package com.shenmazong.serveruserconsumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.shenmazong.serveruserapi.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 军哥
 * @project server-dubbo-demo
 * @date 2020/5/5 9:51 PM
 * http://www.shenmazong.com
 */
@Slf4j
@RestController
public class UserController {

    @Reference(version = "${user.service.version}", loadbalance = "roundrobin")
    private UserService userService;

    public String fallBackHi() {
        return "熔断了....";
    }

    @GetMapping(value = "/hi")
    @HystrixCommand(fallbackMethod = "fallBackHi")
    public String sayHi() {
        String msg = userService.sayHi();
        System.out.println(msg);
        return msg;
    }
}