如何在Springboot中使用Urule?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)公司長(zhǎng)期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為旬陽企業(yè)提供專業(yè)的做網(wǎng)站、網(wǎng)站制作,旬陽網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
一、Urule-Server端:
1.1、 基于maven的SpringBoot基本環(huán)境搭建請(qǐng)參考SpringBoot教程
1.2、引入U(xiǎn)rule相關(guān)依賴,urule-console-pro,開源版本可到https://search.maven.org
中心搜索,依賴如下:
org.springframework.boot spring-boot-starter-web com.bstek.urule urule-console-pro 2.1.0 org.slf4j slf4j-jdk14 javax.servlet servlet-api 2.5 provided org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 com.alibaba druid 1.0.9 MySQL mysql-connector-java
1.3、配置文件:兩個(gè),appplication.yml , application.properties
appplication.yml,配置數(shù)據(jù)庫信息(我們把urule項(xiàng)目存到數(shù)據(jù)庫中)
server: port: 8081 spring: application: name: UruleServer datasource: name: datasource jdbc-url: jdbc:mysql://127.0.0.1:3306/urule?useUnicode=true&characterEncoding=utf-8 username: root password: 666666 # 使用druid數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20
注意,我這此刻DataSource下不jdbc-url而不是url。根據(jù)SpringBoot版本自行調(diào)整
application.properties,配置項(xiàng)目?jī)?chǔ)存位置
#若為本地環(huán)境需配置此路徑 #urule.repository.dir=F:/EclipsePractice/03_SpringCloud/repo4rule #若為數(shù)據(jù)庫,配置此項(xiàng),兩項(xiàng)均不配則系統(tǒng)指定默認(rèn)地址 urule.repository.databasetype=mysql urule.repository.datasourcename=datasource ignore-unresolvable=true order=1
1.4、初始化bean
datesource
@Configuration public class configuration { @Bean public PropertySourcesPlaceholderConfigurer propertySourceLoader() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(true); configurer.setOrder(1); return configurer; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource datasource() { return DataSourceBuilder.create().build(); } }
serverlet
@Component public class URuleServletRegistration { @Bean public ServletRegistrationBeanregisterURuleServlet() { return new ServletRegistrationBean(new URuleServlet(), new String[] { "/urule/*" }); } }
1.5、啟動(dòng)類:
@SpringBootApplication @ImportResource({"classpath:urule-console-context.xml"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
二、客戶端調(diào)用:
2.1、配置類
application.yml server: port: 8090 spring: application: name: UruleClient datasource: name: datasource url: jdbc:mysql://127.0.0.1:3306/myland?useUnicode=true&characterEncoding=utf-8 username: root password: 666666 # 使用druid數(shù)據(jù)源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 urule: ###服務(wù)端發(fā)現(xiàn)地址 resporityServerUrl: http://localhost:8081 ###knowledgeUpdateCycle為0時(shí),不是檢查緩存,每次都從服務(wù)端拉取,為1時(shí),會(huì)先查找緩存 knowledgeUpdateCycle: 1
2.2、初始化bean
@Configuration public class RuleConfig { @Bean public PropertySourcesPlaceholderConfigurer propertySourceLoader() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setIgnoreUnresolvablePlaceholders(true); configurer.setOrder(1); return configurer; } } @Component public class URuleServletRegistration { //此Servlet用于接收Urule服務(wù)端發(fā)布的知識(shí)包,使用開源版本時(shí)刪除或者注釋這個(gè)bean @Bean public ServletRegistrationBean registerURuleServlet(){ return new ServletRegistrationBean(new KnowledgePackageReceiverServlet(),"/knowledgepackagereceiver"); } }
2.3、controller:
@RestController public class TestController { @RequestMapping("/rule") public String getRara(@RequestParam String data)throws IOException{ KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID); //參數(shù),Urule項(xiàng)目名/知識(shí)包名 KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("letasa/pare"); KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage); Integer integer = Integer.valueOf(data); Mapparam = new HashMap(); //參數(shù),var,傳入?yún)?shù),和參數(shù)庫中定義一致 param.put("var", integer); session.fireRules(param); //result,返回參數(shù),和參數(shù)庫中定義一致 Integer result = (Integer) session.getParameter("result"); return String.valueOf(result); } }
2.4、啟動(dòng)類
@SpringBootApplication @ImportResource({"classpath:urule-core-context.xml"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Urule項(xiàng)目配置
參數(shù)庫
規(guī)則
springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡(jiǎn)化配置文件。
關(guān)于如何在Springboot中使用Urule問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。