SpringCloud学习(二)---Spring Cloud Alibaba-----前哨sentinel

Spring Cloud Alibaba provides a one-stop solution for distributed application development. It contains all the components required to develop distributed applications, making it easy for you to develop your applications using Spring Cloud.

With Spring Cloud Alibaba, you only need to add some annotations and a small amount of configurations to connect Spring Cloud applications to the distributed solutions of Alibaba, and build a distributed application system with Alibaba middleware.

Spring Cloud Alibaba为分布式应用程序开发提供了一站式解决方案。 它包含开发分布式应用程序所需的所有组件,使您可以轻松地使用Spring Cloud开发应用程序。

使用Spring Cloud Alibaba,您只需要添加一些注释和少量配置即可将Spring Cloud应用程序连接到Alibaba的分布式解决方案,并使用Alibaba中间件构建分布式应用程序系统。

也不知道是不是真的这么牛逼

Features

Spring Cloud

  • Flow control and service degradation:flow control, circuit breaking and system adaptive protection with Alibaba Sentinel

  • Service registration and discovery:instances can be registered with Alibaba Nacos and clients can discover the instances using Spring-managed beans. Supports Ribbon, the client side load-balancer via Spring Cloud Netflix

  • Distributed Configuration:using Alibaba Nacos as a data store

  • Event-driven:building highly scalable event-driven microservices connected with Spring Cloud Stream RocketMQ Binder

  • Message Bus: link nodes of a distributed system with Spring Cloud Bus RocketMQ

  • Distributed Transaction:support for distributed transaction solution with high performance and ease of use with Seata

  • Dubbo RPC:extend the communication protocols of Spring Cloud service-to-service calls by Apache Dubbo RPC

Spring Boot

All the Spring Boot Starters are maintained in Alibaba Cloud Spring Boot Project.

看到这明白了一点, 他的意思其实就是一些熟悉的技术的整合, 不过的确很多东西就是他的,所以说是一站式解决也不为过.

服务降级,限流 Alibaba Sentinel

服务发现,治理配置中心  Alibaba Nacos

消息: RocketMQ

事务:Seata

rpc: dubbo

Sentinel对标的应该就是hystrix

https://blog.csdn.net/m0_37542889/article/details/82428193

这篇文章说的真的非常好.喜欢的可以去看看原文

 

总结

最后用表格来进行对比总结:

 SentinelHystrix
隔离策略基于并发数线程池隔离/信号量隔离
熔断降级策略基于响应时间或失败比率基于失败比率
实时指标实现滑动窗口滑动窗口(基于 RxJava)
规则配置支持多种数据源支持多种数据源
扩展性多个扩展点插件的形式
基于注解的支持即将发布支持
调用链路信息支持同步调用不支持
限流基于 QPS / 并发数,支持基于调用关系的限流不支持
流量整形支持慢启动、匀速器模式不支持
系统负载保护支持不支持
实时监控 API各式各样较为简单
控制台开箱即用,可配置规则、查看秒级监控、机器发现等不完善
常见框架的适配Servlet、Spring Cloud、Dubbo、gRPC 等Servlet、Spring Cloud Netflix

 

-----------------------------------------------------------------------

这已经是三天后了,今天面试遇到个货说阿里巴巴的seata可以解决redis和mysql事务异步问题,我.......虽然觉得扯淡,但是我觉得我得回来把这一套玩应赶紧看看底层.(懂redis机制的人就应该明白不是不能通过两段式或者三段式解决redis事务,人家本来就是支持事务的,只是不能那么干)

https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html#_spring_cloud_alibaba_sentinel

我们基于这幅图看一下sentinel

5.1. Introduction of Sentinel

说了几件事情, 主要是以流量为切入点.双十一一直在用,还有就是比较容易扩展

5.2. How to Use Sentinel

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>

</dependency>

The following is a simple example of how to use Sentinel:

@SpringBootApplication
public class Application {

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

}

@RestController
public class TestController {

    @GetMapping(value = "/hello")
    @SentinelResource("hello")
    public String hello() {
        return "Hello Sentinel";
    }

}
@SpringBootApplication
public class Application {

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

}

@RestController
public class TestController {

    @GetMapping("/mono")
    @SentinelResource("hello")
    public Mono<String> mono() {
	return Mono.just("simple string")
			.transform(new SentinelReactorTransformer<>("otherResourceName"));
    }

}

仪表盘Dashboard

https://github.com/alibaba/Sentinel.git下载代码 进入Dashboard

  • Download the Dashboard project.

  • Run the following command to package the code into a FatJar: mvn clean package

Start the Dashboard

Sentinel dashboard is a standard SpringBoot application, and you can run the JAR file in the Spring Boot mode.

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

If there is conflict with the 8080 port, you can use -Dserver.port=new port to define a new port.

5.2.2. Configure the Dashboard

application.yml

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080

The port number specified in spring.cloud.sentinel.transport.port will start an HTTP Server on the corresponding server of the application, and this server will interact with the Sentinel dashboard. For example, if a rate limiting rule is added in the Sentinel dashboard, the the rule data will be pushed to and recieved by the HTTP Server, which in turn registers the rule to Sentinel.

For more information about Sentinel dashboard, please refer to Sentinel Dashboard.

--------------------------------------

然后就没了....留下了我在风中凌乱

https://blog.csdn.net/lzb348110175/article/details/107634024

1.@SentinelResource 属性介绍

(红色标注属性为常用属性)

属性名是否必填说明
value资源名称 。(必填项,需要通过 value 值找到对应的规则进行配置)
entryTypeentry类型,标记流量的方向,取值IN/OUT,默认是OUT
blockHandler处理BlockException的函数名称(可以理解为对Sentinel的配置进行方法兜底)。函数要求:
1.必须是 public 修饰
2.返回类型与原方法一致
3. 参数类型需要和原方法相匹配,并在最后加 BlockException 类型的参数。
4. 默认需和原方法在同一个类中。若希望使用其他类的函数,可配置 blockHandlerClass ,并指定blockHandlerClass里面的方法。
blockHandlerClass存放blockHandler的类
对应的处理函数必须 public static 修饰,否则无法解析,其他要求:同blockHandler。
fallback用于在抛出异常的时候提供fallback处理逻辑(可以理解为对Java异常情况方法兜底)
fallback函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。函数要求:
1.返回类型与原方法一致
2.参数类型需要和原方法相匹配,Sentinel 1.6开始,也可在方法最后加 Throwable 类型的参数。
3.默认需和原方法在同一个类中。若希望使用其他类的函数,可配置 fallbackClass ,并指定fallbackClass里面的方法。
fallbackClass存放fallback的类
对应的处理函数必须static修饰,否则无法解析,其他要求:同fallback。
defaultFallback用于通用的 fallback 逻辑
默认 fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,以fallback为准。函数要求:
1.返回类型与原方法一致
2.方法参数列表为空,或者有一个 Throwable 类型的参数。
3.默认需要和原方法在同一个类中。若希望使用其他类的函数,可配置 fallbackClass ,并指定 fallbackClass 里面的方法。
exceptionsToIgnore指定排除掉哪些异常。
排除的异常不会计入异常统计,也不会进入fallback逻辑,而是原样抛出。
exceptionsToTrace需要trace的异常

----------------------------------------------有点乱哈---

package com.daishu.resource.server.test;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/testController")
public class TestController {
    /**
     * value:资源名称 。
     *
     * entryType:entry类型,标记流量的方向,取值IN/OUT,默认是OUT
     *
     *  blockHandler:处理BlockException的函数名称(可以理解为对Sentinel的配置进行方法兜底)。函数要求:
     * 1.必须是 public 修饰
     * 2.返回类型与原方法一致
     * 3. 参数类型需要和原方法相匹配,并在最后加 BlockException 类型的参数。
     * 4. 默认需和原方法在同一个类中。若希望使用其他类的函数,可配置 blockHandlerClass ,并指定blockHandlerClass里面的方法。
     *
     *  blockHandlerClass存放blockHandler的类。
     * 对应的处理函数必须 public static 修饰,否则无法解析,其他要求:同blockHandler。
     *  用于在抛出异常的时候提供fallback处理逻辑(可以理解为对Java异常情况方法兜底)。
     *
     * fallback函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。函数要求:
     * 1.返回类型与原方法一致
     * 2.参数类型需要和原方法相匹配,Sentinel 1.6开始,也可在方法最后加 Throwable 类型的参数。
     * 3.默认需和原方法在同一个类中。若希望使用其他类的函数,可配置 fallbackClass ,并指定fallbackClass里面的方法。
     *  存放fallback的类。
     * 对应的处理函数必须static修饰,否则无法解析,其他要求:同fallback。
     *
     * defaultFallback 用于通用的 fallback 逻辑。
     * 默认 fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,以fallback为准。函数要求:
     * 1.返回类型与原方法一致
     * 2.方法参数列表为空,或者有一个 Throwable 类型的参数。
     * 3.默认需要和原方法在同一个类中。若希望使用其他类的函数,可配置 fallbackClass ,并指定 fallbackClass 里面的方法。
     *
     * exceptionsToIgnore  指定排除掉哪些异常。
     * 排除的异常不会计入异常统计,也不会进入fallback逻辑,而是原样抛出。
     * exceptionsToTrace 需要trace的异常
     *
     *
     * @return
     */
    @RequestMapping(value = "/hello")
    @SentinelResource( value = "hello",blockHandler = "exceptionHandler", fallback = "fallback" )
    @ApiParam("@SentinelResource批注用于标识资源是否受速率限制或降级。 在上面的示例中,注释的“ hello”属性引用资源名称。")
    public String hello(Integer id) {
        return "Hello Sentinel";
    }
    // 降级处理
    public String fallback(Integer id) {
        return "服务降级,id="+id ;
    }
    // 异常处理
    public String exceptionHandler(Integer id, BlockException be) {
        be.printStackTrace();
        return "服务抛异常,id="+id ;
    }
}

我写了这么一个类, 然后打了仪表盘的包, 我只能说打包这个事情没那么easy, 想方便的伙伴可以去下载现成的资源..我象征性的收点东西.^_^.

https://download.csdn.net/download/habazhu1110/13125322

--------------准备工作完成,我们先启动控制台

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard   -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=123456 -jar sentinel-dashboard.jar

然后发现这玩意只能存储5分钟,想要持久化数据需要自己实现接口,然后我就开心了......好了我们继续.

配置自己项目的yml

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080

启动项目

然后去配置限流,大家可以参照这篇文章.

https://blog.csdn.net/zhongzewei/article/details/107490842

比较下来说一下我得感受这东西比较灵活, 使用于电商场景,可以随时配置熔断而且各种级别可用, 但是不太适合传统的项目类型,传统的项目或者是非电商那种不可预期规模的项目其实也没有一个熔断场景.这种东西你搞一个配置页面真心未必实用.所以实战上还真的未必就比hystirx好用.主要还是看你的项目架构设计和实际的操作场景.我只能说仁者见仁智者见智!!!!!!熔断场景多的sentine绝对好用

然后我继续往下看------------终于觉得我的不是水文

5.8. Circuit Breaker: Spring Cloud Circuit Breaker With Sentinel & Configuring Sentinel Circuit Breakers

5.8.1. Default Configuration

To provide a default configuration for all of your circuit breakers create a Customizer bean that is passed a SentinelCircuitBreakerFactory or ReactiveSentinelCircuitBreakerFactory. The configureDefault method can be used to provide a default configuration.

@Bean
public Customizer<SentinelCircuitBreakerFactory> defaultCustomizer() {
	return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id)
			.build());
}

You can choose to provide default circuit breaking rules via SentinelConfigBuilder#rules(rules). You can also choose to load circuit breaking rules later elsewhere using DegradeRuleManager.loadRules(rules) API of Sentinel, or via Sentinel dashboard.

Reactive Example

@Bean
public Customizer<ReactiveSentinelCircuitBreakerFactory> defaultCustomizer() {
	return factory -> factory.configureDefault(id -> new SentinelConfigBuilder(id)
			.build());
}

5.8.2. Specific Circuit Breaker Configuration

Similarly to providing a default configuration, you can create a Customizer bean this is passed a SentinelCircuitBreakerFactory.

@Bean
public Customizer<SentinelCircuitBreakerFactory> slowCustomizer() {
	String slowId = "slow";
	List<DegradeRule> rules = Collections.singletonList(
		new DegradeRule(slowId).setGrade(RuleConstant.DEGRADE_GRADE_RT)
			.setCount(100)
			.setTimeWindow(10)
		);
	return factory -> factory.configure(builder -> builder.rules(rules), slowId);
}

Reactive Example

@Bean
public Customizer<ReactiveSentinelCircuitBreakerFactory> customizer() {
	List<DegradeRule> rules = Collections.singletonList(
		new DegradeRule().setGrade(RuleConstant.DEGRADE_GRADE_RT)
			.setCount(100)
			.setTimeWindow(10)
		);
	return factory -> factory.configure(builder -> builder.rules(rules), "foo", "bar");
}

原来这货除了控制台还可以用DegradeRuleManager支持

https://blog.csdn.net/tang_jian_dong/article/details/86759714

然后发现2.1.3的demo不行 SentinelCircuitBreakerFactory这个类没有,用到2.2.1

然后报错, 果然因为springboot的版本不对,结合成本暂时也就告一段落 了.

继续往下看的时候什么ans, oss都是收费的,愿意看的就自己看看吧.总而言之,两个字,失望吧.还是希望国内能够坚持java的开源体系和spring的插拔,

 

 

 

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值