com.github.lxchinesszz:turnoff-spring-boot-starter

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.github.lxchinesszz
ArtifactId

ArtifactId

turnoff-spring-boot-starter
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Source Code Management

Source Code Management

https://github.com/lxchinesszz/spring-turnoff

Download turnoff-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.lxchinesszz/turnoff-spring-boot-starter/ -->
<dependency>
    <groupId>com.github.lxchinesszz</groupId>
    <artifactId>turnoff-spring-boot-starter</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.lxchinesszz/turnoff-spring-boot-starter/
implementation 'com.github.lxchinesszz:turnoff-spring-boot-starter:1.0.1'
// https://jarcasting.com/artifacts/com.github.lxchinesszz/turnoff-spring-boot-starter/
implementation ("com.github.lxchinesszz:turnoff-spring-boot-starter:1.0.1")
'com.github.lxchinesszz:turnoff-spring-boot-starter:jar:1.0.1'
<dependency org="com.github.lxchinesszz" name="turnoff-spring-boot-starter" rev="1.0.1">
  <artifact name="turnoff-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.lxchinesszz', module='turnoff-spring-boot-starter', version='1.0.1')
)
libraryDependencies += "com.github.lxchinesszz" % "turnoff-spring-boot-starter" % "1.0.1"
[com.github.lxchinesszz/turnoff-spring-boot-starter "1.0.1"]

Dependencies

provided (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.1.1.RELEASE

Project Modules

There are no modules declared in this project.

在现代微服务框架中如: SpringCloud,为了阻止服务雪崩,引入了一种熔断的措施, 即: 为可能出现错误的接口方法,提供一种备用方法,当出现错误就执行备用方法来返回结果。 小编认为这种方法是非常优雅的,虽然这种逻辑,也可以在方法中通过代码逻辑来实现, 但是熔断的处理方式会更加简化这种的处理,使我们的熔断方法看起来更加的优化。

Turnoff 就是简化代码而产生的,通过类似于Hystrix的注解方法,来实现相同的功能,Turnoff主要是利用BeanPostProcessor后置处理器来完成,代码非常简单,只依赖Spring,无论是Dubbo分布式还是SpringCloud分布式,还是单体应用都可以使用。

声明: 如需定制新功能,或者关于项目有好的想法,可以联系我。

目录

  • 配置方法
    • SpringBoot自动化配置
    • Spring配置
  • 使用方法
    • TurnoffCommand代码展示
  • 实现原理
    • BeanPostProcessor初始化后置处理器
    • Aop切面代理
    • 自动化配置

配置方法

SpringBoot自动化配置

  <!--熔断-->
        <dependency>
            <groupId>com.github.lxchinesszz</groupId>
            <artifactId>turnoff-spring-boot-starter</artifactId>
            <version>1.0.1</version>
        </dependency>

Spring配置

<bean class="org.turnoff.processor.CircuitBreakerBeanPostProcessor">
</bean>

使用方法

TurnoffCommand代码展示

@Service
public class UserServiceImpl  {


  @TurnoffCommand(fallbackMethod = "getBreakUserName")
  public String getUserName(String name) {
    throw new RuntimeException();
  }

  public String getBreakUserName(String name) {
    return "Mock用户id:" + name;
  }

}

实现原理

BeanPostProcessor初始化后置处理器

CircuitBreakerBeanPostProcessor实现了BeanPostProcessor的初始化后置处理器,和Ordered优先级最低执行。当Spring中bean在完成创建最后为其生成代理对象,在代理对象中执行TurnoffCommand逻辑。此方法不会影响Spring管控的Bean的生命周期。

Aop切面代理

TurnoffProxyFactory负责生成代理对象,代理分为Jdk代理和Cglib代理,这部分主要利用Spring中自带工具ProxyFactory来创建。此部分逻辑在小编的头条号上存在。

自动化配置

所谓自动化配置是利用SpringBoot的autoconfigure进行实现。之前写过详细的文章来实战SpringBoot的自动化配置,想仔细了解的,可以参考下文。 SpringBoot可插拔开箱即用之组件开发

Versions

Version
1.0.1