diff --git a/drools/.gitignore b/drools/.gitignore
new file mode 100644
index 0000000..549e00a
--- /dev/null
+++ b/drools/.gitignore
@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
diff --git a/drools/README.md b/drools/README.md
new file mode 100644
index 0000000..c912aec
--- /dev/null
+++ b/drools/README.md
@@ -0,0 +1,11 @@
+# demo-request-scope
+
+《基于ThreadLocal实现一个上下文管理组件》技术博客对应源码
+
+https://juejin.cn/post/7153287656624324638
+
+本文基于`ThreadLocal`原理,实现了一个上下文状态管理组件`Scope`,通过开启一个自定义的`Scope`,在`Scope`范围内,可以通过`Scope`各个方法读写数据;
+
+通过自定义线程池实现上下文状态数据的线程间传递;
+
+提出了一种基于`Filter`和`Scope`的`Request`粒度的上下文管理方案。
diff --git a/drools/pom.xml b/drools/pom.xml
new file mode 100644
index 0000000..acad05f
--- /dev/null
+++ b/drools/pom.xml
@@ -0,0 +1,82 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.7.4
+
+
+ com.example
+ demo-request-scope
+ 0.0.1-SNAPSHOT
+ demo-request-scope
+ Demo project for Spring Boot
+
+ 8
+
+
+
+
+
+
+ org.drools
+ drools-core
+ 7.59.0.Final
+
+
+ org.drools
+ drools-compiler
+ 7.59.0.Final
+
+
+ org.drools
+ drools-decisiontables
+ 7.59.0.Final
+
+
+
+ com.google.guava
+ guava
+ 30.1-jre
+
+
+
+ org.springframework.boot
+ spring-boot-starter-aop
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+ org.projectlombok
+ lombok
+ true
+
+
+ junit
+ junit
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/drools/src/main/java/com/example/demo/DemoRequestScopeApplication.java b/drools/src/main/java/com/example/demo/DemoRequestScopeApplication.java
new file mode 100644
index 0000000..4c0d58d
--- /dev/null
+++ b/drools/src/main/java/com/example/demo/DemoRequestScopeApplication.java
@@ -0,0 +1,13 @@
+package com.example.demo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class DemoRequestScopeApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoRequestScopeApplication.class, args);
+ }
+
+}
diff --git a/drools/src/main/java/com/example/demo/config/DroolsConfig.java b/drools/src/main/java/com/example/demo/config/DroolsConfig.java
new file mode 100644
index 0000000..8710281
--- /dev/null
+++ b/drools/src/main/java/com/example/demo/config/DroolsConfig.java
@@ -0,0 +1,29 @@
+package com.example.demo.config;
+
+
+import org.kie.api.KieServices;
+import org.kie.api.builder.KieBuilder;
+import org.kie.api.builder.KieFileSystem;
+import org.kie.api.builder.KieModule;
+import org.kie.api.runtime.KieContainer;
+import org.kie.internal.io.ResourceFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class DroolsConfig {
+ // 制定规则文件的路径
+ private static final String RULES_CUSTOMER_RULES_DRL = "rules/customer-discount.drl";
+ private static final KieServices kieServices = KieServices.Factory.get();
+
+ @Bean
+ public KieContainer kieContainer() {
+ KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
+ kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_CUSTOMER_RULES_DRL));
+ KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
+ kb.buildAll();
+ KieModule kieModule = kb.getKieModule();
+ KieContainer kieContainer = kieServices.newKieContainer(kieModule.getReleaseId());
+ return kieContainer;
+ }
+}
\ No newline at end of file
diff --git a/drools/src/main/java/com/example/demo/controller/OrderDiscountController.java b/drools/src/main/java/com/example/demo/controller/OrderDiscountController.java
new file mode 100644
index 0000000..5dbd14a
--- /dev/null
+++ b/drools/src/main/java/com/example/demo/controller/OrderDiscountController.java
@@ -0,0 +1,35 @@
+package com.example.demo.controller;
+
+
+import com.example.demo.request.OrderDiscount;
+import com.example.demo.request.OrderRequest;
+import com.example.demo.service.OrderDiscountService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Spring Boot 整合 规则引擎Drools,很强!
+ * *网上购物,
+ * * 需要根据不同的规则计算商品折扣,
+ * * 比如VIP客户增加5%的折扣,
+ * * 购买金额超过1000元的增加10%的折扣等,
+ * * 而且这些规则可能随时发生变化,
+ * * 甚至增加新的规则。
+ * * 我们可以通过规则引擎来实现,Drools 就是一个开源的业务规则引擎
+ */
+@RestController
+public class OrderDiscountController {
+
+ @Autowired
+ private OrderDiscountService orderDiscountService;
+
+ @PostMapping("/get-discount")
+ public ResponseEntity getDiscount(@RequestBody OrderRequest orderRequest) {
+ OrderDiscount discount = orderDiscountService.getDiscount(orderRequest);
+ return new ResponseEntity<>(discount, HttpStatus.OK);
+ }
+}
\ No newline at end of file
diff --git a/drools/src/main/java/com/example/demo/request/CustomerType.java b/drools/src/main/java/com/example/demo/request/CustomerType.java
new file mode 100644
index 0000000..2ddf07b
--- /dev/null
+++ b/drools/src/main/java/com/example/demo/request/CustomerType.java
@@ -0,0 +1,15 @@
+package com.example.demo.request;
+
+/**
+ * @className: CustomerType
+ * @author: woniu
+ * @date: 2023/4/25
+ **/
+public enum CustomerType {
+ LOYAL, NEW, DISSATISFIED;
+
+ public String getValue() {
+ return this.toString();
+ }
+
+}
diff --git a/drools/src/main/java/com/example/demo/request/OrderDiscount.java b/drools/src/main/java/com/example/demo/request/OrderDiscount.java
new file mode 100644
index 0000000..969660d
--- /dev/null
+++ b/drools/src/main/java/com/example/demo/request/OrderDiscount.java
@@ -0,0 +1,15 @@
+package com.example.demo.request;
+
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class OrderDiscount {
+
+ /**
+ * 折扣
+ */
+ private Integer discount = 0;
+}
diff --git a/drools/src/main/java/com/example/demo/request/OrderRequest.java b/drools/src/main/java/com/example/demo/request/OrderRequest.java
new file mode 100644
index 0000000..bcf76b8
--- /dev/null
+++ b/drools/src/main/java/com/example/demo/request/OrderRequest.java
@@ -0,0 +1,26 @@
+package com.example.demo.request;
+
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class OrderRequest {
+ /**
+ * 客户号
+ */
+ private String customerNumber;
+ /**
+ * 年龄
+ */
+ private Integer age;
+ /**
+ * 订单金额
+ */
+ private Integer amount;
+ /**
+ * 客户类型
+ */
+ private CustomerType customerType;
+}
\ No newline at end of file
diff --git a/drools/src/main/java/com/example/demo/service/OrderDiscountService.java b/drools/src/main/java/com/example/demo/service/OrderDiscountService.java
new file mode 100644
index 0000000..33d09a3
--- /dev/null
+++ b/drools/src/main/java/com/example/demo/service/OrderDiscountService.java
@@ -0,0 +1,30 @@
+package com.example.demo.service;
+
+import com.example.demo.request.OrderDiscount;
+import com.example.demo.request.OrderRequest;
+import org.kie.api.runtime.KieContainer;
+import org.kie.api.runtime.KieSession;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class OrderDiscountService {
+
+ @Autowired
+ private KieContainer kieContainer;
+
+ public OrderDiscount getDiscount(OrderRequest orderRequest) {
+ OrderDiscount orderDiscount = new OrderDiscount();
+ // 开启会话
+ KieSession kieSession = kieContainer.newKieSession();
+ // 设置折扣对象
+ kieSession.setGlobal("orderDiscount", orderDiscount);
+ // 设置订单对象
+ kieSession.insert(orderRequest);
+ // 触发规则
+ kieSession.fireAllRules();
+ // 中止会话
+ kieSession.dispose();
+ return orderDiscount;
+ }
+}
diff --git a/drools/src/main/resources/application.properties b/drools/src/main/resources/application.properties
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/drools/src/main/resources/application.properties
@@ -0,0 +1 @@
+
diff --git a/drools/src/main/resources/application.yml b/drools/src/main/resources/application.yml
new file mode 100644
index 0000000..1be49ec
--- /dev/null
+++ b/drools/src/main/resources/application.yml
@@ -0,0 +1,21 @@
+#spring:
+# redis:
+# database: 0
+# # Redis服务器地址 写你的ip
+# host: 192.168.56.10
+# # Redis服务器连接端口
+# port: 6379
+# # Redis服务器连接密码(默认为空)
+# password: 123456
+# # 连接池最大连接数(使用负值表示没有限制 类似于mysql的连接池
+# jedis:
+# pool:
+# max-active: 200
+# # 连接池最大阻塞等待时间(使用负值表示没有限制) 表示连接池的链接拿完了 现在去申请需要等待的时间
+# max-wait: -1
+# # 连接池中的最大空闲连接
+# max-idle: 10
+# # 连接池中的最小空闲连接
+# min-idle: 0
+# # 连接超时时间(毫秒) 去链接redis服务端
+# timeout: 6000
\ No newline at end of file
diff --git a/drools/src/main/resources/rules/customer-discount.drl b/drools/src/main/resources/rules/customer-discount.drl
new file mode 100644
index 0000000..6a9469f
--- /dev/null
+++ b/drools/src/main/resources/rules/customer-discount.drl
@@ -0,0 +1,38 @@
+package rules
+
+import com.example.demo.request.OrderRequest;
+import com.example.demo.request.CustomerType;
+
+global com.example.demo.request.OrderDiscount orderDiscount;
+
+dialect "mvel"
+
+// 规则1: 根据年龄判断
+rule "Age based discount"
+when
+// 当客户年龄在20岁以下或者50岁以上
+OrderRequest(age < 20 || age > 50)
+then
+// 则添加10%的折扣
+System.out.println("==========Adding 10% discount for Kids/ senior customer=============");
+orderDiscount.setDiscount(orderDiscount.getDiscount() + 10);
+end
+
+// 规则2: 根据客户类型的规则
+rule "Customer type based discount - Loyal customer"
+when
+// 当客户类型是LOYAL
+OrderRequest(customerType.getValue == "LOYAL")
+then
+// 则增加5%的折扣
+System.out.println("==========Adding 5% discount for LOYAL customer=============");
+orderDiscount.setDiscount(orderDiscount.getDiscount() + 5);
+end
+
+rule "Customer type based discount - others"
+when
+OrderRequest(customerType.getValue != "LOYAL")
+then
+System.out.println("==========Adding 3% discount for NEW or DISSATISFIED customer=============");
+orderDiscount.setDiscount(orderDiscount.getDiscount() + 3);
+end
diff --git a/drools/src/test/java/com/example/demo/testscope/TestMain.java b/drools/src/test/java/com/example/demo/testscope/TestMain.java
new file mode 100644
index 0000000..d068e13
--- /dev/null
+++ b/drools/src/test/java/com/example/demo/testscope/TestMain.java
@@ -0,0 +1,172 @@
+package com.example.demo.testscope;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import org.junit.jupiter.api.Test;
+
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+public class TestMain {
+
+//工作中常用的一些Stream流操作,多年实践总结
+
+ /**
+ * 演示map的用途:一对一转换
+ */
+ @Test
+ public void stringToIntMap() {
+ List ids = Arrays.asList("205", "105", "308", "469", "627", "193", "111");
+ // 使用流操作
+ List results = ids.stream()
+ .map(id -> {
+ User user = new User(Integer.valueOf(id));
+ return user;
+ })
+ .collect(Collectors.toList());
+ System.out.println(results);
+ }
+
+
+ /**
+ * 演示map的用途:一对多转换
+ */
+ @Test
+ public void stringToIntFlatmap() {
+ List sentences = Arrays.asList("hello world", "Jia Gou Wu Dao");
+ // 使用流操作
+ List results = sentences.stream()
+ .flatMap(sentence -> Arrays.stream(sentence.split(" ")))
+ .collect(Collectors.toList());
+ System.out.println(results);
+ }
+
+
+
+
+
+ /**
+ *filter、sorted、distinct、limit
+ */
+ @Test
+ public void testGetTargetUsers() {
+ List ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");
+ // 使用流操作
+ List results = ids.stream()
+ .filter(s -> s.length() > 2)
+ .distinct()
+ .map(Integer::valueOf)
+ .sorted(Comparator.comparingInt(o -> o))
+ .limit(3)
+ .map(id -> new User(id))
+ .collect(Collectors.toList());
+ System.out.println(results);
+ }
+
+
+
+
+ /**
+ * 简单结果终止方法
+ */
+ @Test
+ public void testSimpleStopOptions() {
+ List ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");
+ // 统计stream操作后剩余的元素个数
+ System.out.println(ids.stream().filter(s -> s.length() > 2).count());
+ // 判断是否有元素值等于205
+ System.out.println(ids.stream().filter(s -> s.length() > 2).anyMatch("205"::equals));
+ // findFirst操作
+ ids.stream().filter(s -> s.length() > 2)
+ .findFirst()
+ .ifPresent(s -> System.out.println("findFirst:" + s));
+ }
+
+
+
+
+
+ /**
+ * 一旦一个Stream被执行了终止操作之后,后续便不可以再读这个流执行其他的操作了,否则会报错
+ */
+ @Test
+ public void testHandleStreamAfterClosed() {
+ List ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");
+ Stream stream = ids.stream().filter(s -> s.length() > 2);
+ // 统计stream操作后剩余的元素个数
+ System.out.println(stream.count());
+ System.out.println("-----下面会报错-----");
+ // 判断是否有元素值等于205
+ try {
+ System.out.println(stream.anyMatch("205"::equals));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ System.out.println("-----上面会报错-----");
+ }
+
+
+ /**
+ * 生成集合
+ */
+ @Test
+ public void testCollectStopOptions() {
+ List ids = Arrays.asList(new User(17), new User(22), new User(23));
+ // collect成list
+ List collectList = ids.stream().filter(dept -> dept.getId() > 20)
+ .collect(Collectors.toList());
+ System.out.println("collectList:" + collectList);
+ // collect成Set
+ Set collectSet = ids.stream().filter(dept -> dept.getId() > 20)
+ .collect(Collectors.toSet());
+ System.out.println("collectSet:" + collectSet);
+ // collect成HashMap,key为id,value为Dept对象
+ Map collectMap = ids.stream().filter(dept -> dept.getId() > 20)
+ .collect(Collectors.toMap(com.example.demo.testscope.User::getId, dept -> dept));
+ System.out.println("collectMap:" + collectMap);
+ }
+
+
+
+
+ /**
+ * 生成拼接字符串
+ */
+ @Test
+ public void testCollectJoinStrings() {
+ List ids = Arrays.asList("205", "10", "308", "49", "627", "193", "111", "193");
+ String joinResult = ids.stream().collect(Collectors.joining(","));
+ System.out.println("拼接后:" + joinResult);
+ }
+
+
+
+
+
+ /**
+ * 数学运算
+ */
+ @Test
+ public void testNumberCalculate() {
+ List ids = Arrays.asList(10, 20, 30, 40, 50);
+ // 计算平均值
+ Double average = ids.stream().collect(Collectors.averagingInt(value -> value));
+ System.out.println("平均值:" + average);
+ // 数据统计信息
+ IntSummaryStatistics summary = ids.stream().collect(Collectors.summarizingInt(value -> value));
+ System.out.println("数据统计信息: " + summary);
+ }
+
+
+
+
+}
+
+
+@Data
+@AllArgsConstructor
+class User {
+ private int id;
+
+}
\ No newline at end of file