–|eureka-registry --注册中心
–|hystrix-service – 调用用户服务
–|user-service --用户服务
server:
port: 9002
spring:
application:
name: hystrix-service
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: true #默认值是true,是否将自己注册到注册中心
fetch-registry: true #默认值是true,是否从注册中心拉取服务
service-url: #注册中信对外暴露的注册中心地址
defaultZone: http://localhost:8761/eureka/
feign:
hystrix:
enabled: true #在Feign中开启Hystrix
server:
port: 9001
spring:
application:
name: user-service
datasource: # 数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: true #默认值是true,是否将自己注册到注册中心
fetch-registry: false # 默认值是true,是否从注册中心拉取服务
service-url: #注册中信对外暴露的注册中心地址
defaultZone: http://localhost:8761/eureka/
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
@SpringBootApplication
@EnableCircuitBreaker
@MapperScan("user.service.provider.mapper")
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserMapper userMapper;
/*
* 查询所有用户信息
* 当该方法出现异常等信息,会返回兜底数据,
* 不会直接将错误信息返回,导致服务不可用
*/
@GetMapping(value = "/selectUserInfo")
@HystrixCommand(fallbackMethod = "fallbackMethod")
public List<UserInfo> selectUserInfo() {
//int i = 1/0;
return userMapper.selectAll();
}
/**
* 兜底数据
*/
public List<UserInfo> fallbackMethod(){
List<UserInfo> list = new ArrayList<>();
UserInfo userInfo = new UserInfo();
userInfo.setName("兜底数据");
userInfo.setAge(20);
userInfo.setAddress("兜底数据");
list.add(userInfo);
return list;
}
}
@HystrixCommand(fallbackMethod = "hystrixMethod",commandProperties = {
//是否开启断路器
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
//请求次数,默认20次
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
//时间窗口期,默认5s
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),
//失败率达到多少后跳闸,默认50%
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
})
@GetMapping(value = "/selectUserInfoByName")
public UserInfo selectUserInfoByName(@RequestParam("name") String name){
UserInfo userInfo = userMapper.selectUserInfoByName(name);
if (null == userInfo){
throw new RuntimeException("用户信息为空");
}
return userInfo;
}
/**
* 兜底数据
*/
public UserInfo hystrixMethod(String name){
UserInfo userInfo = new UserInfo();
userInfo.setName(name + "托底用户");
userInfo.setAge(20);
userInfo.setAddress("杭州市");
return userInfo;
}
@HystrixCollapser(
batchMethod = "selectUserByListId",
scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
collapserProperties = {
//隔离多久会进行请求合并,默认10ms
@HystrixProperty(name = "timerDelayInMilliseconds", value = "5000"),
//最大请求条数
@HystrixProperty(name = "maxRequestsInBatch", value = "2")
})
@GetMapping(value = "/selectUserInfoById")
public UserInfo selectUserInfoById(@RequestParam("id") Integer id) {
return userMapper.selectUserInfoById(id);
}
/**
* 处理合并后的请求方法
*/
@HystrixCommand
public List<UserInfo> selectUserByListId(List<Integer> ids) {
System.out.println("请求合并入参:" + ids);
ArrayList<UserInfo> list = new ArrayList<>();
for (Integer id : ids) {
UserInfo userInfo = userMapper.selectUserInfoById(id);
list.add(userInfo);
}
System.out.println("请求合并处理结果:" + list);
return list;
}
feign:
hystrix:
enabled: true #在Feign中开启Hystrix
@FeignClient(name = "user-service", fallback = UserFallBackService.class)
public interface UserServiceApi {
@GetMapping(value = "user/selectUserInfo")
List<UserInfo> selectUserInfo();
@GetMapping(value = "user/selectUserInfoByName")
UserInfo selectUserInfoByName(@RequestParam("name") String name);
@GetMapping(value = "user/selectUserInfoById")
UserInfo selectUserInfoById(@RequestParam("id") Integer id);
}
@Component
public class UserFallBackService implements UserServiceApi{
@Override
public List<UserInfo> selectUserInfo() {
ArrayList<UserInfo> userInfos = new ArrayList<>();
UserInfo userInfo = new UserInfo();
userInfo.setName("托底用户");
userInfo.setAge(20);
userInfo.setAddress("杭州市");
userInfos.add(userInfo);
return userInfos;
}
@Override
public UserInfo selectUserInfoByName(String name) {
UserInfo userInfo = new UserInfo();
userInfo.setName(name + "托底用户");
userInfo.setAge(20);
userInfo.setAddress("杭州市");
return userInfo;
}
@Override
public UserInfo selectUserInfoById(Integer id) {
UserInfo userInfo = new UserInfo();
userInfo.setName("托底用户");
userInfo.setAge(20);
userInfo.setAddress("杭州市");
return userInfo;
}