设为首页 加入收藏

TOP

Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战(二)
2019-09-14 00:53:56 】 浏览:130
Tags:Spring Cloud Alibaba Sentinel 分布式 系统 流量 卫兵 进阶 实战
mplate") public String helloByRestTemplate() { return restTemplate.getForObject("http://spring-cloud-provider-server/hello/", String.class); } }

Sentinel已经对做了整合,我们使用Feign的地方无需额外的注解。同时,@FeignClient注解中的所有属性,Sentinel都做了兼容。

启动主类Ch122ConsumerServerApplication.java如下:

代码清单:Alibaba/sentinel-springcloud-high/consumer_server/src/main/java/com/springcloud/consumer_server/ConsumerServerApplication.java
***

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Ch122ConsumerServerApplication {

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

    @Bean
    @LoadBalanced
    @SentinelRestTemplate
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

在使用RestTemplate的时候需要增加@SentinelRestTemplate来开启Sentinel对RestTemplate的支持。

1.4 测试

启动工程provider_server和consumer_server,provider_server修改启动配置,启动两个实例,打开浏览器访问:http://localhost:9000/helloByFeign 和 http://localhost:9000/helloByRestTemplate ,刷新几次,可以看到页面交替显示Hello, port is: 8000Hello, port is: 8001,说明目前负载均衡正常,现在查看Sentinel控制台,如图:

1.5 流量控制测试

这时选择左侧的簇点流控,点击流控,如图:

这里我们配置一个最简单的规则,配置QPS限制为1,点击新增,如图:

这里解释一下什么是QPS,简单来说QPS是一个每秒访问数,这里我们测试时需要重复快速刷新http://localhost:9000/helloByFeign 和 http://localhost:9000/helloByRestTemplate ,在刷新的过程中,我们可以看到页面会显示错误信息,如:Blocked by Sentinel (flow limiting),说明我们配置Sentinel已经限流成功,这时我们再看一下Sentinel的控制台,可以看到我们刚才访问的成功和限流的数量,如图:

2. 服务降级

在上一小结,我们介绍了Feign和RestTemplate整合Sentinel使用,并且在Sentinel控制台上做了QPS限流,并且限流成功,限流成功后,默认情况下,Sentinel对控制资源的限流处理是直接抛出异常。在没有合理的业务承接或者前端对接情况下可以这样,但是正常情况为了更好的用户业务,都会实现一些被限流之后的特殊处理,我们不希望展示一个生硬的报错。这一小节,我们介绍一下服务降级处理。

2.1 创建子工程consumer_fallback

Feign服务降级类HelloRemoteFallBack.java如下:

代码清单:Alibaba/sentinel-springcloud-high/consumer_fallback/src/main/java/com/springcloud/consumer_fallback/fallback/HelloRemoteFallBack.java
***

@Component
public class HelloRemoteFallBack implements HelloRemote {
    @Override
    public String hello() {
        return "Feign FallBack Msg";
    }
}

相对应的,这里需要在HelloRemote.java上做一部分配置,使得限流后,触发服务降级执行我们的服务降级类,代码如下:

代码清单:ch12_2/ch12_2_consumer_fallback/src/main/java/com/springcloud/book/ch12_2_consumer_fallback/remote/HelloRemote.java
***

@FeignClient(name = "spring-cloud-provider-server", fallback = HelloRemoteFallBack.class)
public interface HelloRemote {
    @GetMapping("/hello")
    String hello();
}

fallback = HelloRemoteFallBack.class指定服务降级的处理类为HelloRemoteFallBack.class

RestTemplate服务降级工具类ExceptionUtil.java如下:

代码清单:Alibaba/sentinel-springcloud-high/consumer_fallback/src/main/java/com/springcloud/consumer_fallback/remote/HelloRemote.java
***

public class ExceptionUtil {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionUtil.class);

    public static SentinelClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        logger.error(ex.getMessage(), ex);
        return new SentinelClientHttpResponse("RestTemplate FallBack Msg");
    }
}

这里同样需要修改RestTemplate注册成为Bean的地方,使得RestTemplate触发服务降级以后代码执行我们为它写的处理类,Ch122ConsumerFallbackApplication.java代码如下:

代码清单:Alibaba/sentinel-spring

首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇2019最新版Java零基础入门视频教.. 下一篇多线程编程学习九(并发工具类).

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目