BAEL-3636 - introduction to alibaba sentinel

This commit is contained in:
Amit Bhave 2021-01-02 14:49:45 +05:30
parent 94c6ee972b
commit 01f5b186a8
6 changed files with 120 additions and 0 deletions

View File

@ -44,6 +44,7 @@
<module>spring-cloud-circuit-breaker</module>
<module>spring-cloud-eureka-self-preservation</module>
<!--<module>spring-cloud-openfeign</module>--> <!-- Fixing in JAVA-2820 -->
<module>spring-cloud-sentinel</module>
</modules>
<build>

View File

@ -0,0 +1,35 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-sentinel</artifactId>
<name>spring-cloud-sentinel</name>
<url>http://maven.apache.org</url>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,11 @@
package org.spring.cloud.sentinel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SentinelApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelApplication.class, args);
}
}

View File

@ -0,0 +1,35 @@
package org.spring.cloud.sentinel.config;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class SentinelAspectConfiguration {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
@PostConstruct
public void initFlowRules() {
List<FlowRule> flowRules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
// Defined resource
flowRule.setResource("greeting");
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// number of requests that QPS can pass in a second
flowRule.setCount(1);
flowRules.add(flowRule);
FlowRuleManager.loadRules(flowRules);
}
}

View File

@ -0,0 +1,19 @@
package org.spring.cloud.sentinel.controller;
import org.spring.cloud.sentinel.service.GreetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@Autowired
private GreetingService greetingService;
@GetMapping("/hello")
public String getGreeting() {
return greetingService.getGreeting();
}
}

View File

@ -0,0 +1,19 @@
package org.spring.cloud.sentinel.service;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.stereotype.Service;
@Service
public class GreetingService {
@SentinelResource(value = "greeting", fallback = "getGreetingFallback")
public String getGreeting() {
return "Hello World!";
}
public String getGreetingFallback(Throwable e) {
e.printStackTrace();
return "Bye world!";
}
}