Sentinel高级 sentinel和springCloud整合
减少开发的复杂度,对大部分的主流框架,例如:Web Servlet、Dubbo、Spring Cloud、gRPC、Spring WebFlux、Reactor等做了适配。只需要引入对应应用的以来即可方便地整合Sentinel。
如果要实现SpringCloud和Sentinel的整合,可以通过引入Spring Cloud Alibaba Sentinel来更方便得整合Sentinel。
Spring Cloud Alibaba是阿里巴巴集团提供的,致力于提供微服务开发的一站式解决方案。Spring Cloud Alibaba默认为Sentinel整合Servlet、RestTemplate、FeignClient和Spring WebFlux、Sentinel在Spring Cloud生态中,不仅补全了hystrix在Servlet和RestTemplate这一块的空白,而且完全兼容hystrix在FeignClient种限流降级的用法,并且支持运用时灵活地配置和调整限流降级规则。
需求使用SpringCloud + Sentinel实现访问:8080/ann路径的流量控制。
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id("org.springframework.boot") version "2.3.7.RELEASE" id("io.spring.dependency-management") version "1.0.10.RELEASE" kotlin("jvm") version "1.3.72" kotlin("plugin.spring") version "1.3.72" java } group = "xyz.ytfs" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_1_8 repositories { mavenCentral() } extra["springCloudAlibabaVersion"] = "2.2.2.RELEASE" dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") testImplementation("org.springframework.boot:spring-boot-starter-test") { exclude(group = "org.junit.vintage", module = "junit-vintage-engine") } } dependencyManagement { imports { mavenBom("com.alibaba.cloud:spring-cloud-alibaba-dependencies:${property("springCloudAlibabaVersion")}") } } tasks.withType<Test> { useJUnitPlatform() } tasks.withType<KotlinCompile> { kotlinOptions { freeCompilerArgs = listOf("-Xjsr305=strict") jvmTarget = "1.8" } } @SentinelResource(value = "spring_cloud_sentinel_test", blockHandler = "exceptionHandler") @GetMapping("ann") fun springCloudSentinelTest(): String { return "hello Spring-Cloud-Sentinel_test" } fun exceptionHandler(bx: BlockException): String { return "系统繁忙,请稍后重试" } Sentinel对Feign的支持Sentinel适配了Feign组件,如果想使用,除了引入spring-cloud-starter-alibaba-sentinel的依赖外还需要2个步骤:
配置文件打开Sentinel对Feign的支持:feign.sentinel.enabled=true
加入spring-cloud-starter-openfeign依赖Sentinel starter中的自动化配置类生效
需求实现sentinel_feign_client微服务通过Feign访问sentinel_feign_provider微服务的流量控制
创建spring-cloud-parent父工程依赖文件
extra["springCloudVersion"] = "Hoxton.SR9" extra["springCloudAlibabaVersion"] = "2.2.2.RELEASE" group = "xyz.ytfs" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_1_8 allprojects { repositories { maven(url = "http://maven.aliyun.com/nexus/content/groups/public/") mavenCentral() maven { url = uri("https://repo.spring.io/snapshot") } maven { url = uri("https://repo.spring.io/milestone") } } } dependencies { implementation("org.springframework.boot:spring-boot-starter") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") testImplementation("org.springframework.boot:spring-boot-starter-test") { exclude(group = "org.junit.vintage", module = "junit-vintage-engine") } } 创建eureka-server注册中心子工程依赖添加
dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server") testImplementation("org.springframework.boot:spring-boot-starter-test") { exclude(group = "org.junit.vintage", module = "junit-vintage-engine") } } dependencyManagement { imports { mavenBom("com.alibaba.cloud:spring-cloud-alibaba-dependencies:${property("springCloudAlibabaVersion")}") mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}") } }启动类和配置文件的修改
@EnableEurekaServer //在启动类上添加此注解,表示开启eureka注册中心服务 @SpringBootApplication class EurekaServerApplication fun main(args: Array<String>) { runApplication<EurekaServerApplication>(*args) } # 应用名称 spring.application.name=eureka-server server.port=8060 #eureka配置 eureka.client.service-url.defaultZone=http://127.0.0.1:8060/eureka #不拉去服务 eureka.client.fetch-registry=false #不注册自己 eureka.client.register-with-eureka=false 创建sentinel-feign-client
添加依赖
implementation("com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client") implementation("org.springframework.cloud:spring-cloud-starter-openfeign") testImplementation("org.springframework.boot:spring-boot-starter-test") { exclude(group = "org.junit.vintage", module = "junit-vintage-engine") }
创建代理的个接口
@FeignClient(value="sentinel-feign-provider", fallback = FallBackService::class) interface ProviderClient { @GetMapping("hello") fun hello(): String }
创建controller
@RestController class TestController(val providerClient: ProviderClient) { @GetMapping("hello") fun hello(): String{ return this.providerClient.hello() } }
创建降级相应示例
@Service /** * 实现代理接口 **/ class FallBackService : ProviderClient { override fun hello(): String { return "系统繁忙,请稍后重试" } }