这两种容错方式 分别实在服务端 和 调用端
第一种
服务端:
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
这种方式就是调用端来处理异常错误