您好,登錄后才能下訂單哦!
如何在Springboot中使用Urule?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一、Urule-Server端:
1.1、 基于maven的SpringBoot基本環境搭建請參考SpringBoot教程
1.2、引入Urule相關依賴,urule-console-pro,開源版本可到https://search.maven.org
中心搜索,依賴如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.bstek.urule</groupId> <artifactId>urule-console-pro</artifactId> <version>2.1.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
1.3、配置文件:兩個,appplication.yml , application.properties
appplication.yml,配置數據庫信息(我們把urule項目存到數據庫中)
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數據源 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。根據SpringBoot版本自行調整
application.properties,配置項目儲存位置
#若為本地環境需配置此路徑 #urule.repository.dir=F:/EclipsePractice/03_SpringCloud/repo4rule #若為數據庫,配置此項,兩項均不配則系統指定默認地址 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 ServletRegistrationBean<HttpServlet> registerURuleServlet() { return new ServletRegistrationBean(new URuleServlet(), new String[] { "/urule/*" }); } }
1.5、啟動類:
@SpringBootApplication @ImportResource({"classpath:urule-console-context.xml"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
二、客戶端調用:
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數據源 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: ###服務端發現地址 resporityServerUrl: http://localhost:8081 ###knowledgeUpdateCycle為0時,不是檢查緩存,每次都從服務端拉取,為1時,會先查找緩存 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服務端發布的知識包,使用開源版本時刪除或者注釋這個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); //參數,Urule項目名/知識包名 KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("letasa/pare"); KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage); Integer integer = Integer.valueOf(data); Map<String, Object> param = new HashMap(); //參數,var,傳入參數,和參數庫中定義一致 param.put("var", integer); session.fireRules(param); //result,返回參數,和參數庫中定義一致 Integer result = (Integer) session.getParameter("result"); return String.valueOf(result); } }
2.4、啟動類
@SpringBootApplication @ImportResource({"classpath:urule-core-context.xml"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Urule項目配置
參數庫
規則
springboot一種全新的編程規范,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程,SpringBoot也是一個服務于框架的框架,服務范圍是簡化配置文件。
關于如何在Springboot中使用Urule問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。