ribbon服务
1.pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.配置
创建CommandForIndex类
public class CommandForIndex extends HystrixCommand<Object> {
private final RestTemplate template;
private String id;
public CommandForIndex(String id, RestTemplate restTemplate) {
// java代码配置, 只针对这个命令
super(Setter
// 必填项,用于统计,指定命令分组名
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Hello-Group"))
// 熔断配置依赖名称,默认值command实现类的类名(比如这个就是CommandForIndex),如果是服务调用,则写具体的接口名
.andCommandKey(HystrixCommandKey.Factory.asKey("ConsumerController"))
// 线程池配置依赖名称,默认值HystrixCommandGroupKey的名称
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("User-ThreadPool"))
// command 熔断相关参数配置
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
// 配置隔离方式:默认采用线程池隔离。(还有信号量隔离方式)
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
// 超时时间500毫秒
.withExecutionTimeoutInMilliseconds(500)
// 信号量隔离的模式下,最大的请求数。和线程池大小的意义一样
// .withExecutionIsolationSemaphoreMaxConcurrentRequests(2)
// 熔断时间(熔断开启后,各5秒后进入半开启状态,试探是否恢复正常)
// .withCircuitBreakerSleepWindowInMilliseconds(5000)
)
// 设置线程池参数
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
// 线程池大小
.withCoreSize(1)
//允许最大的缓冲区大小
// .withMaxQueueSize(2)
));
// super(HystrixCommandGroupKey.Factory.asKey("User-command"),100);
this.id = id;
this.template = restTemplate;
}
@Override
protected Object run() throws Exception {
System.out.println("###command#######" + Thread.currentThread().toString());
Object result = template.getForObject("http://helloclient/user?id="+id+"",Object.class);
System.out.println(
"###command结束#######" + Thread.currentThread().toString() + ">><>>>执行结果:" + result.toString());
return result;
}
@Override
protected Object getFallback() {
System.out.println("###降级啦####" + Thread.currentThread().toString());
return "出錯了,我降級了";
//降级的处理:
//1.返回一个固定的值
//2.去查询缓存
//3.调用一个备用接口
}
}
测试
修改Controller层
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@GetMapping("")
public Object index(@RequestParam("id")String id){
//localhost:8001和http://helloclient等同 ,在配置文件中有相关配置
//return restTemplate.getForObject("http://localhost:8001/user?id="+id+"",Object.class);
//return restTemplate.getForObject("http://helloclient/user?id="+id+"",Object.class);
return new CommandForIndex(id,restTemplate).execute();
}
@GetMapping("/get-teacher")
public Object getTeacher(){
//return restTemplate.getForEntity("http://localhost:8001",Teacher.class);
return restTemplate.getForEntity("http://helloclient",Teacher.class);
}
public Object callTimeoutFallback(){
return "查询超时啦,我降级了。";
}
}
修改hello-demo和ribbon服务的application.yml,将服务注册到注册中心上去
server:
port: 8092
###定义实例名
spring:
application:
name: spring-cloud-ribbon-consumer
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10000/eureka/
修改hello-demo服务的controller层,将第一个hello-demo请求接口sleep一秒钟
@GetMapping("user")
public Object getUser(@Param("id")String id) throws InterruptedException {
System.out.println("客户端1返回了请求");
Thread.sleep(1000L);
return helloService.get(id);
}
启动服务
服务请求超时,服务降级
再次请求
请求成功
示例代码:
码云