spring cloud Hystrix 两种容错方式

这两种容错方式 分别实在服务端 和 调用端

第一种

服务端:
pom.xml

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

controller 这里做服务容错

package com.liumin.xyz.controller;

import com.liumin.xyz.entity.UserEntity;
import com.liumin.xyz.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

/**
 * @Author:lm
 * @Despriction:
 * @Date:2018/11/7 20:11
 */
@Controller
@EnableHystrixDashboard//这个注解要加上
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("getUser")
    @HystrixCommand(fallbackMethod = "getFallback")//失败调用的方法
    public UserEntity getUser() {
    	//从数据库查询数据
        UserEntity userEntity = userService.getUser();
        //模拟出错
        Random random = new Random();
        int var = random.nextInt(10);
        if (var % 2 == 1 ) {
            throw new RuntimeException();
        }
        return userEntity;
    }

	//失败调用的方法
	//注意此方法的参数 与getUser()的参数一致
    public UserEntity getFallback() {
        UserEntity userEntity = new UserEntity();
        userEntity.setId("66666");
        userEntity.setName("66666");
        userEntity.setSex("6666");
        return userEntity;
    }
}

然后http://127.0.0.1:8091/getUser 请求
两种结果
在这里插入图片描述

在这里插入图片描述

第二种

服务端代码基本不变

package com.liumin.xyz.controller;

import com.liumin.xyz.entity.UserEntity;
import com.liumin.xyz.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

/**
 * @Author:lm
 * @Despriction:
 * @Date:2018/11/7 20:11
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("getUser")

    public UserEntity getUser() {
        UserEntity userEntity = userService.getUser();
        Random random = new Random();
        int var = random.nextInt(10);
        if (var % 2 == 1 ) {
            throw new RuntimeException();
        }
        return userEntity;
    }
}

在fegin客服端声明接口

package com.liumin.xyz.fegin;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @Author:lm
 * @Despriction:
 * @Date:2018/11/8 16:07
 */
@FeignClient(name = "server-user",fallback = UserSericeFallback.class)//UserSericeFallback是处理异常的类
public interface UserService {

    /**
     * 获取用户
     * @return
     */
    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    String getUser();
}

package com.liumin.xyz.fegin;

import org.springframework.stereotype.Component;

/**
 * @Author:lm
 * @Despriction:这里处理异常
 * @Date:2018/11/16 14:34
 */
@Component
public class UserSericeFallback implements UserService {
    @Override
    public String getUser() {

        return "77777777777777777";
    }
}

pom.xml

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

application.yml

feign:
  hystrix:
    enabled : true

这种方式就是调用端来处理异常错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值