欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 中使用 Drools 規(guī)則引擎的完整步驟

 更新時(shí)間:2025年04月30日 10:33:12   作者:SteveCode.  
規(guī)則引擎主要用于將業(yè)務(wù)邏輯從應(yīng)用程序代碼中分離出來,提高系統(tǒng)的靈活性和可維護(hù)性,規(guī)則引擎通過預(yù)定義的規(guī)則來處理輸入數(shù)據(jù)并做出相應(yīng)的決策,從而實(shí)現(xiàn)業(yè)務(wù)邏輯的自動(dòng)化和動(dòng)態(tài)調(diào)整,本文給大家介紹Spring Boot中使用 Drools 規(guī)則引擎的指南,感興趣的朋友一起看看吧

規(guī)則引擎作用

規(guī)則引擎主要用于將業(yè)務(wù)邏輯從應(yīng)用程序代碼中分離出來,提高系統(tǒng)的靈活性和可維護(hù)性。規(guī)則引擎通過預(yù)定義的規(guī)則來處理輸入數(shù)據(jù)并做出相應(yīng)的決策,從而實(shí)現(xiàn)業(yè)務(wù)邏輯的自動(dòng)化和動(dòng)態(tài)調(diào)整。

例如

門店信息校驗(yàn):美團(tuán)點(diǎn)評(píng)在門店信息校驗(yàn)過程中使用規(guī)則引擎,對(duì)門店信息進(jìn)行質(zhì)量控制。規(guī)則包括分支條件、簡(jiǎn)單計(jì)算規(guī)則和業(yè)務(wù)定制計(jì)算規(guī)則等。通過規(guī)則引擎,門店信息校驗(yàn)過程變得更加高效和準(zhǔn)確。 業(yè)務(wù)場(chǎng)景說明

不同會(huì)員的折扣率不同

代碼結(jié)構(gòu)

集成 Drools(即規(guī)則引擎)到 Spring Boot 可以幫助你實(shí)現(xiàn)業(yè)務(wù)規(guī)則的動(dòng)態(tài)管理和執(zhí)行。下面我來簡(jiǎn)要說明一下業(yè)務(wù)場(chǎng)景和代碼實(shí)現(xiàn)的步驟:

1. 添加依賴

首先,需要在 pom.xml 文件中添加 Drools 的依賴:

 <!-- drools規(guī)則引擎 -->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>7.6.0.Final</version>
        </dependency>

2. 創(chuàng)建規(guī)則文件

在項(xiàng)目的src/main/resources目錄下創(chuàng)建一個(gè)名為rules的文件夾,并在其中創(chuàng)建一個(gè)名為sample.drl的規(guī)則文件:

package com.song;
import com.song.rulesobj.Customer
rule "ORDINARY-Customers"
when
    customer: Customer(type == "ORDINARY")
then
    customer.setDiscount(0.9*customer.getOriginalPrice()); // 9折  普通會(huì)員
end
rule "VIP-Customers"
when
    customer: Customer(type == "VIP")
then
    customer.setDiscount(0.6*customer.getOriginalPrice()); // 6折 VIP會(huì)員
end
rule "SVIP-Customers"
when
    customer: Customer(type == "SVIP")
then
    customer.setDiscount(0.4*customer.getOriginalPrice()); // 4折 SVIP會(huì)員
end

3. 定義實(shí)體類

創(chuàng)建一個(gè)簡(jiǎn)單的實(shí)體類 Customer,用于表示客戶信息:

package com.song.rulesobj;
import lombok.Data;
@Data
public class Customer {
    /**
     * 客戶類型
     */
    private String type;
    /**
     * 客戶訂單價(jià)格
     */
    private Double  originalPrice; // 訂單原始價(jià)格,即優(yōu)惠前的價(jià)格
    /**
     * 優(yōu)惠后最終結(jié)算價(jià)格
     */
    private Double discount;
}

4. 配置 Drools 規(guī)則引擎

在 Spring Boot 應(yīng)用程序中配置 Drools 規(guī)則引擎的 bean:

package com.song.conf;
import com.song.bean.DiscountBean;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DroolsConfig {
    @Bean
    public KieContainer kieContainer() {
        KieServices kieServices = KieServices.Factory.get();
        KieRepository kieRepository = kieServices.getRepository();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(kieServices.getResources().newClassPathResource("rules/sample.drl"));
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
    }
    @Bean
    public KieSession kieSession() {
        return kieContainer().newKieSession();
    }
    @Bean
    public DiscountBean discountBean() {
       return new  DiscountBean(kieSession());
    }
}

5. 應(yīng)用規(guī)則引擎

在業(yè)務(wù)代碼中使用注入的 KieSession 執(zhí)行規(guī)則:

/*******************************************************************************
 * Package: com.song.bean
 * Type:    DiscountService
 * Date:    2024-06-28 13:45
 *
 * Copyright (c) 2024 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.bean;
import com.song.rulesobj.Customer;
import org.kie.api.runtime.KieSession;
/**
 * 功能描述: 規(guī)則處理器封裝
 *
 * @author Songxianyang
 * @date 2024-06-28 13:45
 */
public class DiscountBean {
    private KieSession kieSession;
    public DiscountBean(KieSession kieSession) {
        this.kieSession = kieSession;
    }
    public void applyDiscount(Customer customer) {
        kieSession.insert(customer); // 插入客戶對(duì)象到規(guī)則引擎中
        kieSession.fireAllRules(); // 執(zhí)行規(guī)則
        // 客戶對(duì)象已經(jīng)被更新,包含了計(jì)算出的折扣
        System.out.println("客戶訂單價(jià)格"+customer.getOriginalPrice()+"客戶折扣類型: " + customer.getType() + ", 優(yōu)惠后最終結(jié)算價(jià)格: " + customer.getDiscount());
    }
}

6. 測(cè)試規(guī)則引擎

編寫一個(gè)簡(jiǎn)單的測(cè)試類來驗(yàn)證規(guī)則引擎是否按預(yù)期工作:

package com.song.web;
import com.song.bean.DiscountBean;
import com.song.common.annotation.ResponseInfoSkin;
import com.song.rulesobj.Customer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "規(guī)則引擎")
@ResponseInfoSkin
public class DemoController {
    @Autowired
    private DiscountBean discountBean;
    @PostMapping("/discount")
    @ApiOperation("打折")
    public Double discount(@RequestBody Customer customer) {
        // 規(guī)則處理器封裝
        discountBean.applyDiscount(customer);
        return customer.getDiscount();
    }
}

測(cè)試截圖

總結(jié)

通過上述步驟,你可以將 Drools 規(guī)則引擎集成到 Spring Boot 應(yīng)用程序中,并使用規(guī)則文件動(dòng)態(tài)管理業(yè)務(wù)規(guī)則,實(shí)現(xiàn)不同客戶類型的動(dòng)態(tài)折扣計(jì)算。這種方式可以使得業(yè)務(wù)規(guī)則更易于維護(hù)和修改,同時(shí)與應(yīng)用程序解耦,提高了靈活性和可維護(hù)性。

源碼

到此這篇關(guān)于Spring Boot 中使用 Drools 規(guī)則引擎的完整指南的文章就介紹到這了,更多相關(guān)Spring Boot Drools 規(guī)則引擎內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 老生常談java中的fail-fast機(jī)制

    老生常談java中的fail-fast機(jī)制

    下面小編就為大家?guī)硪黄仙U刯ava中的fail-fast機(jī)制。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟

    java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟

    本文主要介紹了java實(shí)現(xiàn)ssh連接服務(wù)器的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • Java中的vector類使用示例小結(jié)

    Java中的vector類使用示例小結(jié)

    Vector與ArrayList的實(shí)現(xiàn)基本相似,同樣是基于動(dòng)態(tài)數(shù)組,同樣是需要擴(kuò)容,下面舉了三個(gè)簡(jiǎn)短的例子來幫助大家理解vertor:
    2016-05-05
  • SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題

    SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題

    這篇文章主要介紹了SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SpringAop自定義切面注解、自定義過濾器及ThreadLocal詳解

    SpringAop自定義切面注解、自定義過濾器及ThreadLocal詳解

    這篇文章主要介紹了SpringAop自定義切面注解、自定義過濾器及ThreadLocal詳解,Aspect(切面)通常是一個(gè)類,里面可以定義切入點(diǎn)和通知(切面 = 切點(diǎn)+通知),execution()是最常用的切點(diǎn)函數(shù),需要的朋友可以參考下
    2024-01-01
  • SpringBoot整合MongoDB流程詳解

    SpringBoot整合MongoDB流程詳解

    這篇文章主要介紹了SpringBoot整合MongoDB流程詳解,MongoDB是一種面向文檔的數(shù)據(jù)庫管理系統(tǒng),它是一個(gè)介于關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫之間的產(chǎn)品,MongoDB支持一種類似JSON的BSON數(shù)據(jù)格式,既可以存儲(chǔ)簡(jiǎn)單的數(shù)據(jù)格式,也可以存儲(chǔ)復(fù)雜的數(shù)據(jù)類型,需要的朋友可以參考下
    2024-01-01
  • MyBatis中example.createCriteria()方法的具體使用

    MyBatis中example.createCriteria()方法的具體使用

    本文詳細(xì)介紹了MyBatis的Example工具的使用方法,包括鏈?zhǔn)秸{(diào)用指定字段、設(shè)置查詢條件、支持多種查詢方式等,還介紹了mapper的crud方法、and/or方法的使用,以及如何進(jìn)行多條件和多重條件查詢,感興趣的可以了解一下
    2024-10-10
  • Spring Jms 模塊案例講解

    Spring Jms 模塊案例講解

    本文詳細(xì)介紹了Spring-JMS模塊,包括其核心功能和作用,通過ActiveMQ作為消息代理,提供了一個(gè)基于XML配置的完整示例,幫助開發(fā)者快速掌握Spring-JMS的使用方式,感興趣的朋友一起看看吧
    2025-02-02
  • java中json-diff簡(jiǎn)單使用及對(duì)象是否一致詳解

    java中json-diff簡(jiǎn)單使用及對(duì)象是否一致詳解

    這篇文章主要為大家介紹了java中json-diff簡(jiǎn)單使用及對(duì)象是否一致對(duì)比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • JAVA實(shí)現(xiàn)DOC轉(zhuǎn)PDF的示例代碼

    JAVA實(shí)現(xiàn)DOC轉(zhuǎn)PDF的示例代碼

    Word作為目前主流的文本編輯軟件之一,功能十分強(qiáng)大,但是在傳輸?shù)臅r(shí)候不穩(wěn)定,那么如何從DOC轉(zhuǎn)PDF,本文就來介紹一下,感興趣的可以了解一下
    2021-08-08

最新評(píng)論