做钢材的网站有哪些,做英文版网站,淘宝买cdk自己做网站,wordpress怎么查找文件1.springcloud微服务架构搭建 之 《springboot自动装配Redis》
2.springcloud微服务架构搭建 之 《springboot集成nacos注册中心》
3.springcloud微服务架构搭建 之 《springboot自动装配ribbon》
4.springcloud微服务架构搭建 之 《springboot集成openFeign》 目录
1.项目…1.springcloud微服务架构搭建 之 《springboot自动装配Redis》
2.springcloud微服务架构搭建 之 《springboot集成nacos注册中心》
3.springcloud微服务架构搭建 之 《springboot自动装配ribbon》
4.springcloud微服务架构搭建 之 《springboot集成openFeign》 目录
1.项目引入hystirx 1.1.项目引入hystrix坐标配置 1.2.项目启动类启用断路器
2. HystrixCommand 注解 2.1 使用HystrixCommand 实现fallback 2.1.1 正常性测试 2.1.2 停掉cms服务模拟服务不可用 2.2 通过DefaultProperties实现全局fallback 2.2.1 停到cms服务模拟测试 2.3 通过自定义参数配置接口的熔断策略 3.开启feign集成hystrix 3.1 FeignClient 接口实现降级策略,FallbackFactory 3.1.1 服务正常测试 3.1.2 停掉api服务测试 3.1.3 继续模拟频繁调用接口则会触发熔断 4.全局Hystrix配置
5.参考 1.项目引入hystirx 1.1.项目引入hystrix坐标配置
!-- hystrix 配置 版本号2.1.3.RELEASE--
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix/artifactIdversion${spring.cloud.version}/version
/dependency 1.2.项目启动类启用断路器
package lilock.cn.user;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;SpringBootApplication(scanBasePackages {lilock.cn.*})
EnableConfigurationProperties
EnableDiscoveryClient
EnableFeignClients(basePackages {lilock.cn}) //启用feign调用
EnableCircuitBreaker // 启用断路器
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class,args);}
}2. HystrixCommand 注解
Hystrix提供了HystrixCommand用于配置关于Hystrix相关配置如回调方法、超时时间、熔断配置等。想要使用hystrix必须是用HystrixCommand注解
这种模式下的hystrix使用配置都是基于 HystrixCommand注解配置的我们通过在方法上添加 HystrixCommand 注解并配置注解的参数来实现配置。 2.1 使用HystrixCommand 实现fallback
/*** 接口熔断* return*/HystrixCommand(fallbackMethod testHystrixError)GetMapping(/testHystrix)public String testHystrix(){log.info(testHystrix 接口调用 apiCmsServiceFeignClient.getHello);String value apiCmsServiceFeignClient.getHello();return [testHystrix] 验证断路器 value;}public String testHystrixError(){log.info({} testHystrixError 调用失败,System.currentTimeMillis());return enable testHystrixError;}2.1.1 正常性测试 2.1.2 停掉cms服务模拟服务不可用 2.2 通过DefaultProperties实现全局fallback
时候一个类里面会有多个 Hystrix 方法每个方法都是类似配置的话会冗余很多代码这时候我们可以在类上使用 DefaultProperties 注解来给整个类的 Hystrix 方法设置一个默认降级方法特别标注的降级走特别标注的方法没有特别标注的降级走默认方法
RestController
RequestMapping(path /user)
Api(value 系统用户,tags {系统用户})
Slf4j
DefaultProperties(defaultFallback defaultFallBackMethod)
public class UserController {/**** 模拟全局配置fallback*/HystrixCommandGetMapping(/testHystrixTimeOutFallback)public BaseResult testHystrixTimeOutFallback(RequestParam(time) long time){BaseResultString value apiCmsHystrixServiceFeignClient.getHystrixTimeOut(time);log.info(testHystrixTimeOutFallback 接口调用 响应结果:{},value);try{Thread.sleep(time);}catch (Exception e){}return value;}public BaseResult defaultFallBackMethod(){String errorMsg System.currentTimeMillis() defaultFallBackMethod 调用失败触发熔断;log.error({},errorMsg);return BaseResult.faild(errorMsg);}
} 2.2.1 停到cms服务模拟测试 2.3 通过自定义参数配置接口的熔断策略
配置一个超时时间并且fallback走公共模式
/*** 模拟带参数走服务熔断* return*/HystrixCommand(commandProperties {HystrixProperty(name execution.isolation.thread.timeoutInMilliseconds,value 1500)})GetMapping(/testHystrixTimeDefault)public BaseResult testHystrixTimeDefault(long time){log.info(testHystrixTimeDefault 接口调用 apiCmsHystrixServiceFeignClient.testHystrixDefault);BaseResultString value apiCmsHystrixServiceFeignClient.getHystrix();try{Thread.sleep(time);}catch (Exception e){}return value;}public BaseResult defaultFallBackMethod(){String errorMsg System.currentTimeMillis() defaultFallBackMethod 调用失败触发熔断;log.error({},errorMsg);return BaseResult.faild(errorMsg);}
接口sleep 1000ms 时间小于降级1500ms时间正常返回 接口sleep 2000ms时间大于降级1500ms时间触发降级 3.开启feign集成hystrix
配置文件开启
feign:hystrix:enabled: true 3.1 FeignClient 接口实现降级策略,FallbackFactory
fallbackFactory的好处是可以统一配置
当然Api接口上也可以单独配置HystrixCommand
package lilock.cn.cms.api;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lilock.cn.cms.api.fallback.ApiCmsHystrixServiceFeignClientFallbackFactory;
import lilock.cn.common.resp.BaseResult;
import lilock.cn.common.ribbon.config.FeignConfig;
import lilock.cn.common.ribbon.constant.ApplicationServiceConstants;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;//FeignClient实现了FallbackFactory
FeignClient(value ApplicationServiceConstants.LILOCK_CMS_WEB,configuration {FeignConfig.class},fallbackFactory ApiCmsHystrixServiceFeignClientFallbackFactory.class)
public interface ApiCmsHystrixServiceFeignClient {GetMapping(/getHystrix)BaseResultString getHystrix();GetMapping(/getHystrixTimeOut)HystrixCommandBaseResultString getHystrixTimeOut(RequestParam long time);
}3.1.1 服务正常测试 3.1.2 停掉api服务测试
可以看到触发了fallbackFactory的服务降级服务降级之后还会继续调用下游api 3.1.3 继续模拟频繁调用接口则会触发熔断
当降级的数量达到一定的百分比之后接口就会触发熔断触发熔断之后不会继续调用下游api 4.全局Hystrix配置
hystrix:command:#全局默认配置default:execution:timeout:#是否给方法执行设置超时时间默认为true。一般我们不要改。enabled: trueisolation:#配置请求隔离的方式这里是默认的线程池方式。还有一种信号量的方式semaphore使用比较少。strategy: threadPoolsemaphore:maxConcurrentRequests: 1000thread:#方式执行的超时时间默认为1000毫秒在实际场景中需要根据情况设置timeoutInMilliseconds: 60000#发生超时时是否中断方法的执行默认值为true。不要改。interruptOnTimeout: true#是否在方法执行被取消时中断方法默认值为false。没有实际意义默认就好interruptOnCancel: false#熔断器相关配置##并发执行的最大线程数默认10coreSize: 200#说明是否允许线程池扩展到最大线程池数量默认为false。allowMaximumSizeToDivergeFromCoreSize: true#说明线程池中线程的最大数量默认值是10。此配置项单独配置时并不会生效需要启用allowMaximumSizeToDivergeFromCoreSizemaximumSize: 200#说明1作业队列的最大值默认值为-1。表示队列会使用SynchronousQueue此时值为0Hystrix不会向队列内存放作业。#说明2如果此值设置为一个正int型队列会使用一个固定size的LinkedBlockingQueue此时在核心线程池都忙碌的情况下会将作业暂时存放在此队列内但是超出此队列的请求依然会被拒绝maxQueueSize: 20000#设置队列拒绝请求的阀值默认为5。queueSizeRejectionThreshold: 30000circuitBreaker:#说明是否启动熔断器默认为true。我们使用Hystrix的目的就是为了熔断器不要改否则就不要引入Hystrix。enabled: true#说明1启用熔断器功能窗口时间内的最小请求数假设我们设置的窗口时间为10秒#说明2那么如果此时默认值为20的话那么即便10秒内有19个请求都失败也不会打开熔断器。#说明3此配置项需要根据接口的QPS进行计算值太小会有误打开熔断器的可能而如果值太大超出了时间窗口内的总请求数则熔断永远也不会被触发#说明4建议设置一般为QPS*窗口描述*60%requestVolumeThreshold: 3000#说明1熔断器被打开后所有的请求都会被快速失败掉但是何时恢复服务是一个问题。熔断器打开后Hystrix会在经过一段时间后就放行一条请求#说明2如果请求能够执行成功则说明此时服务可能已经恢复了正常那么熔断器会关闭相反执行失败则认为服务仍然不可用熔断器保持打开。#说明3所以此配置的作用是指定熔断器打开后多长时间内允许一次请求尝试执行官方默认配置为5秒。sleepWindowInMilliseconds: 5000#说明1:该配置是指在通过滑动窗口获取到当前时间段内Hystrix方法执行失败的几率后根据此配置来判断是否需要打开熔断器#说明2:这里官方的默认配置为50即窗口时间内超过50%的请求失败后就会打开熔断器将后续请求快速失败掉errorThresholdPercentage: 70#说明是否强制启用熔断器默认false没有什么场景需要这么配置忽略forceOpen: false#说明是否强制关闭熔断器默认false没有什么场景需要这么配置忽略forceClosed: false
5.参考
https://zhuanlan.zhihu.com/p/339535352
https://blog.csdn.net/weixin_40482816/article/details/119215962