Build SPL Microservices with Spring Cloud
Build SPL Microservices with Spring Cloud
— esProc includes supports for Spring data source
Introduction
esProc has a rich set of data source configuration classes that can meet most configuration requirements. Yet, when trying to dock a native data source into esProc, we find that a special interface is needed.
The Spring data source factory:
com.scudata.common.SpringDBSessionFactory.java,
And a general-purpose data source factory are thus created.
com.scudata.common.DataSourceSessionFactory.java。
Each of them can implement the ISessionFactory interface. Just Invoke their unique static method create to generate ISessionFactory and pass it to the esProc runtime environment. This essay explains the role of com.scudata.common.SpringDBSessionFactory.java for building microservices based on Spring Cloud.
The general-purpose data source factory com.scudata.common.DataSourceSessionFactory.java is irrelevant to Spring, but it can be also used to add the data source factory to the esProc environment. See 【The link is creating…】.
At the same time, RaqReport adds support to Spring data source. See 【The link is creating…】.
If you are familiar with Spring Cloud, just jump to Ⅰ/b)/iii.
Ⅰ Testing project description
a) Build Spring Cloud microservices
i Create three Maven projects. One of them is the microservice registration center, like a Eureka registration center.
1. register-server, as shown in the following screenshot:
2. Import dependencies:
3. Configure application.yml, the registration center:
4. Create Spring Boot startup class RegisterApplication.java
Add @EnableEurekaServer to make the project the registration center after startup
ii Create Provider, a Spring Boot web project
1. splservice, as shown in the following screenshot. The project imports dependencies for many modules, involving springboot-parent, spring-cloud, web and Tomcat-related ones, spring-jdbc/regist ds, and system lib. They are covered in pom.xml.
2. Configure bootstrap.yml, which is located in src\main\resources\bootstrap.yml. The file contains web configuration, Spring and Datasoruce configurations and registration center configuration. The Datasource configuration should be modified as needed.
3. Create ProviderApplication.java
Note: add @EnableDiscoveryClient to enable the class to register at the registration center.
4. Complete the project creation and write logics for subsequent actions (as the following steps show).
iii. Create another Spring Boot web project Consumer.
1. consumer, as shown in the following screenshot. The project imports dependencies for springboot-parent, spring-cloud, consumer/feign, web and Tomcat-related ones, and system lib.
2. Configure bootstrap.yml, which is located in src\main\resources\bootstrap.yml. The file contains web configuration, Spring application configuration and registration center configuration.
3. Create ConsumerApplication.java
Note: add @EnableDiscoveryClient to enable the class to register at the registration center.
4. Complete the project creation and write logics for subsequent actions (as the following steps show).
b) Provider defines the service provider that configures data source, passes the configuration to esProc environment and enables to use esProc.
i ProviderApplication.java
Add component scanning @ComponentScan({”com.scudata.”,”com.test.”})
Which can be found in the above screenshot.
ii Create Datasource
Create com.test.config.MultiDsBeanConfig.java. You can view its detailed logic in the projects source code. The Spring datasource appears in the project after the creation of this class. Note that the string in @Bean is the datasource object name or id in Spring, which will be used in the subsequent steps. Two data sources are configured in the following screenshot.
iii Generate SpringDBSessionFactory
Create com.test.listener.AppInitListener.java. The class, similar to a monitor, needs to be called back after run method is started for the convenience of creating SpringDBSessionFactory. At the invocation of SpringDBSessionFactory.create(springDatasourcId,type), the first parameter is the datasource id mentioned in the above step and the second one is the DBType defined in the product. In our sample, we create SpringDBSessionFactory for both data sources. Testers need to write source code and configure id, type and other information as required.
iv Computing service
Create com.test.controller.SplExecuterController.java. You can view its detailed logic in the projects source code. This class simulates invoking the esProc script file, connect to the database and output query result. Controller method can be invoked through the web path. In our sample, we write two methods that correspond to two scripts for the two data sources. As this is just a sample, we use the fixed absolute path that needs to be modified as needed. The test suite offers two scripts containing =connect(“Spring datasoruce id”) and c.query(sql). Testers need to modify them as needed. Both scripts return byte[], which is a sequence-lized Sequence query result to be passed to Consumer.
c) Consumer is the consumer of a service that can invoke Provider remotely and obtain the computing result.
i Add @EnableFeignClients to ConsumerApplication.java for invoking microservices using Feign
ii Invoke microservices
Create com.test.consumer.SplFeignClient.java. You can view its detailed logic in the projects source code. This interface is for invoking Provider. It comments methods with URLs Provider can access and returns a result of sequence-lized Sequence Provider computation.
iii Entry of Consumer
Create com.test.consumer.controller.ConsumeController.java. You can view its detailed logic in the projects source code. This class is the web access entry of Consumer project. It defines url routing. We can access methods in this class through these urls, invoke services of the remote Provider project, handle the returned byte[] data and move to the next step operation as needed. The example displays the result on page:
Ⅱ Test
a) The test requires Java and Maven environments.
b) All three projects are web services. The local test host is localhost and ports occupied in the example are 8761, 8090 and 8091. The configuration file needs to be modified if there is a process conflict.
c) The data source configuration in Provider should be modified as needed.
d) The example uses hsql and mysql as data sources. If other databases will be used in the test, types defined in com.test.listener.AppInitListener.java in source code’s Provider need to be modified correspondingly.
e) Start register-server.
f) Start Provider and Consumer. No specific order is required for them.
g) Access localhost:8091/consumer/page/result/hsql to view result of querying script spring.splx.
h) Access localhost:8091/consumer/page/result/mysql to view result of querying script spring2.splx.
Ⅲ Common error
The error appears because the application registration hasn’t completed yet. Just refresh the page afterwhile or check the registration center: localhost:8761.
Ⅳ Test
Download the application’s source code zip file
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version