首页
归档
留言
广告合作
友链
美女主播
Search
1
博瑞GE车机升级/降级
5,173 阅读
2
修改elementUI中el-table树形结构图标
4,540 阅读
3
Mac打印机设置黑白打印
4,535 阅读
4
Mac客户端添加腾讯企业邮箱方法
4,372 阅读
5
intelliJ Idea 2022.2.X破解
4,092 阅读
Java
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
登录
/
注册
Search
标签搜索
Spring Boot
Java
Spring Cloud
Mac
mybatis
WordPress
Nacos
Spring Cloud Alibaba
Mybatis-Plus
jQuery
Java Script
asp.net
微信小程序
Sentinel
UniApp
MySQL
asp.net core
IntelliJ IDEA
Jpa
树莓派
Laughing
累计撰写
576
篇文章
累计收到
1,425
条评论
首页
栏目
Java
HarmonyOS Next
Web前端
微信开发
开发辅助
App开发
数据库
随笔日记
页面
归档
留言
广告合作
友链
美女主播
搜索到
2
篇与
的结果
2021-07-28
Spring Cloud Alibaba sentinel的安装与配置
sentinel简介Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。Sentinel 具有以下特性:丰富的应用场景:Sentinel承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。sentinel与hystrix区别提到服务的限流、熔断,就不得不提大名鼎鼎的豪猪哥(hystrix)。只是这货目前停更了。随着Spring Cloud 2020发布,hystix组件也被移除了。Sentinel 基本概念资源资源是 Sentinel 的关键概念。它可以是 Java 应用程序中的任何内容,例如,由应用程序提供的服务,或由应用程序调用的其它应用提供的服务,甚至可以是一段代码。在接下来的文档中,我们都会用资源来描述代码块。只要通过 Sentinel API 定义的代码,就是资源,能够被 Sentinel 保护起来。大部分情况下,可以使用方法签名,URL,甚至服务名称作为资源名来标示资源。规则围绕资源的实时状态设定的规则,可以包括流量控制规则、熔断降级规则以及系统保护规则。所有规则可以动态实时调整。Sentinel 是如何工作的Sentinel 的主要工作机制如下:对主流框架提供适配或者显示的 API,来定义需要保护的资源,并提供设施对资源进行实时统计和调用链路分析。根据预设的规则,结合对资源的实时统计信息,对流量进行控制。同时,Sentinel 提供开放的接口,方便您定义及改变规则。Sentinel 提供实时的监控系统,方便您快速了解目前系统的状态。sentinel安装下载按钮官方下载地址运行官方下载下来的是一个jar包,默认端口是8080执行以下命令运行即可java -jar -Dserver.port=9000 /root/sentinel/sentinel-dashboard-1.8.2.jar如果想后台运行,可以执行以下命令wsl -u root nohup java -jar -Dserver.port=9000 /root/sentinel/sentinel-dashboard-1.8.2.jar >log.out 2>1 &默认用户名、密码都是sentinel
2021年07月28日
1,059 阅读
0 评论
1 点赞
2021-07-18
使用Hystrix实现微服务的熔断处理
Hystrix是Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止级联失效,从而提升系统的可用性与容错性。Hystrix基本使用添加依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.8.RELEASE</version> </dependency>为启动类增加@EnableHystrix注解,从而为项目启用断路器支持@SpringBootApplication @EnableFeignClients @EnableHystrix public class MicroserviceConsumerMovieApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceConsumerMovieApplication.class, args); } }修改方法,增加容错能力 @GetMapping("/user/{id}") @HystrixCommand(fallbackMethod = "findByIdFallback", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"), @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "10000") }, threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "1"), @HystrixProperty(name = "maxQueueSize", value = "10") }) public User findById(@PathVariable Long id) { return userFeignClient.findById(id); } public User findByIdFallback(Long id, Throwable throwable) { log.info(throwable.getMessage()); User user = new User(); user.setName("默认用户"); return user; }测试我们断开此时的服务提供者,访问方法,可以看到进入了容错方法,并获取到了异常信息。feign集成hystrix[alt type="success"]关于soring Cloud版本提示[/alt]一定要注意Spring Cloud版本问题,Spring Cloud 2020 后,开启feign的hystrix支持需要在配置文件配置如下feign: circuitbreaker: enabled: true早期版本的Spring Cloud需要配置如下feign: hystrix: enabled: true为feign添加回退通过fallback添加回退通过fallback添加回退,不能获取回退原因。@FeignClient(name = "microservice-provider-user",fallback = UserFeignClientCallback.class) public interface UserFeignClient { /** * 使用Feign自带的注解 @RequestLine * @param id * @return */ @RequestLine("GET /{id}") User findById(@Param("id") Long id); } @Slf4j @Component class UserFeignClientCallback implements UserFeignClient{ /** * 使用Feign自带的注解 @RequestLine * * @param id * @return */ @Override public User findById(Long id) { User user = new User(); user.setName("默认用户"); return user; } }通过fallbackFactory添加回退fallbackFactory是fallback的升级版本,能够获取到回退原因。@FeignClient(name = "microservice-provider-user",fallbackFactory = UserFeignClientCallbackFactory.class) public interface UserFeignClient { /** * 使用Feign自带的注解 @RequestLine * @param id * @return */ @RequestLine("GET /{id}") User findById(@Param("id") Long id); } @Slf4j @Component class UserFeignClientCallbackFactory implements FallbackFactory<UserFeignClient> { @Override public UserFeignClient create(Throwable cause) { return new UserFeignClient() { @Override public User findById(Long id) { log.info(cause.getMessage()); User user = new User(); user.setName("默认用户"); return user; } }; } }[alt type="success"]温馨提示[/alt]日志最好放到各个fallback方法中,而不要放到create方法中,否则在引用启动时,会打印日志。feign项目中Hystrix的监控首先需要添加依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>启动类添加@EnableHystrix@SpringBootApplication @EnableFeignClients @EnableHystrix public class MicroserviceConsumerMovieApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceConsumerMovieApplication.class, args); } }修改配置文件,暴漏地址management: endpoints: web: exposure: include: hystrix.stream测试打开地址http://localhost:8081/actuator/hystrix.stream使用hystrix-dashboard可视化监控项目我们刚才项目监控时,返回的都是Json数据,通过hystrix-dashboard我们可以实现可视化的监控。添加hystrix-dashboard依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> <version>2.2.8.RELEASE</version> </dependency>在启动类添加@EnableHystrixDashboard注解@SpringBootApplication @EnableFeignClients @EnableHystrix @EnableHystrixDashboard public class MicroserviceConsumerMovieApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceConsumerMovieApplication.class, args); } }修改配置文件配置文件添加hystrix: dashboard: proxy-stream-allow-list: "*"如果不添加,监控会提示Unable to connect to Command Metric Stream.测试打开http://localhost:8081/hystrix,监控地址输入http://localhost:8081/actuator/hystrix.stream,title随便输入即可。点击Monitor Stream,然后访问任意的接口,系统输出界面如下
2021年07月18日
1,002 阅读
0 评论
1 点赞