Spring_Cloud_Bus

Spring Cloud Bus可以将分布式系统的节点与轻量级消息代理链接,然后可以实现广播状态更改(例如配置更改)或广播其他管理指令。Spring Cloud Bus就像一个分布式执行器,用于扩展的Spring Boot应用程序,但也可以用作应用程序之间的通信通道。那么这里就涉及到了消息代理,目前流行的消息代理中间件有不少,Spring Cloud Bus支持RabbitMQ和Kafka,本文我们主要来看看RabbitMQ的基本使用。

工程创建

首先我们来创建一个普通的Spring Boot工程,然后添加如下依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

属性配置

接下来在application.properties中配置RabbitMQ的连接信息,如下:

1
2
3
4
5
6
spring.application.name=rabbitmq-hello
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=sang
spring.rabbitmq.password=123456
server.port=2009

这里我们分别配置了RabbitMQ的地址为localhost,端口为5672(注意这里没写错,web管理端端口是15672),用户名和密码则是我们刚刚创建出来的(也可以使用默认的guest)。

创建消息生产者

发送消息我们有一个现成的封装好的对象AmqpTemplate,通过AmqpTemplate我们可以直接向某一个消息队列发送消息,消息生产者的定义方式如下:

1
2
3
4
5
6
7
8
9
10
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String msg = "hello rabbitmq:"+new Date();
System.out.println("Sender:"+msg);
this.rabbitTemplate.convertAndSend("hello", msg);
}
}

注入AmqpTemplate,然后利用AmqpTemplate向一个名为hello的消息队列中发送消息。

创建消息消费者

1
2
3
4
5
6
7
8
@Component
@RabbitListener(queues = "hello")
public class Receiver {
@RabbitHandler
public void process(String msg) {
System.out.println("Receiver:"+msg);
}
}

@RabbitListener(queues = “hello”)注解表示该消息消费者监听hello这个消息队列,@RabbitHandler注解则表示process方法是用来处理接收到的消息的,我们这里收到消息后直接打印即可。

配置消息队列Bean

创建一个名为hello的消息队列。

1
2
3
4
5
6
7
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
}

测试

创建单元测试类,用来发送消息,如下:

1
2
3
4
5
6
7
8
9
10
11
12
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RabbitmqHelloApplication.class)
public class RabbitmqHelloApplicationTests {

@Autowired
private Sender sender;

@Test
public void contextLoads() {
sender.send();
}
}

这个表示程序已经创建了一个访问RabbitMQ的连接,此时在RabbitMQ的web管理面板中,我们也可以看到连接信息,如下:

Spring_Cloud_Netflix

一、Spring Cloud Netflix

该项目是Spring Cloud的核心子项目,是对Netflix公司一系列开源产品的封装。它为Spring Boot应用提供了自配置的整合,只需要通过一些简单的注解,就可以快速地在Spring Cloud的应用中使用起来。

它主要提供的模块包括:

服务发现注册(Eureka)
客户端负载均衡(Ribbon)
断路器(Hystrix)
智能路由(Zuul)

二、服务注册和服务发现

调用关系说明:

1.服务提供者在启动时,向注册中心注册自己提供的服务。
2.服务消费者在启动时,向注册中心订阅自己所需的服务。
3.注册中心返回服务提供者地址给消费者。
4.服务消费者从提供者地址中调用消费者。

注意! 下面的服务端指:注册中心,客户端指:提供者和消费者

三、如何使用Eureka进行服务注册和发现

创建“服务注册中心”

创建一个基础的Spring Boot工程,命名为eureka-server,并在pom.xml中引入需要的依赖内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

服务端添加配置, 在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:

1
2
3
4
5
6
spring.application.name=eureka-server
server.port=1001

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

3、服务端添加注解

通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的Spring Boot应用中添加这个注解就能开启此功能,比如下面的例子:

1
2
3
4
5
6
7
8
9
@EnableEurekaServer
@SpringBootApplication
public class Application {

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.web(true).run(args);
}
}

为了与后续要进行注册的服务区分,这里将服务注册中心的端口通过server.port属性设置为1001。启动工程后,访问:http://localhost:1001/,可以看到下面的页面,其中还没有发现任何服务。

创建“服务提供方”

下面我们创建提供服务的客户端,并向服务注册中心注册自己。本文我们主要介绍服务的注册与发现,所以我们不妨在服务提供方中尝试着提供一个接口来获取当前所有的服务信息。

首先,创建一个基本的Spring Boot应用。命名为eureka-client,在pom.xml中,加入如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<parent> 
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

其次,实现/dc请求处理接口,通过DiscoveryClient对象,在日志中打印出服务实例的相关内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
public class DcController {

@Autowired
DiscoveryClient discoveryClient;

@GetMapping("/dc")
public String dc() {
String services = "Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
}

}

最后在应用主类中通过加上@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。

1
2
3
4
5
6
7
8
9
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(
ComputeServiceApplication.class)
.web(true).run(args);
}
}

我们在完成了服务内容的实现之后,再继续对application.properties做一些配置工作,具体如下:

提供者

1
2
3
spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。

启动该工程后,再次访问:http://localhost:1001/。可以如下图内容,我们定义的服务被成功注册了。

其中,方括号中的eureka-client就是通过Spring Cloud定义的DiscoveryClient接口在eureka的实现中获取到的所有服务清单。由于Spring Cloud在服务发现这一层做了非常好的抽象,所以,对于上面的程序,我们可以无缝的从eureka的服务治理体系切换到consul的服务治理体系中区。

Spring_Cloud_Config

一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。市面上开源的配置中心有很多,BAT每家都出过,360的QConf、淘宝的diamond、百度的disconf都是解决这类问题。国外也有很多开源的配置中心Apache的Apache Commons Configuration、owner、cfg4j等等。

在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

一个配置中心提供的核心功能

提供服务端和客户端支持
集中管理各环境的配置文件
配置文件修改之后,可以快速的生效
可以进行版本管理
支持大的并发查询
支持各种语言

Spring Cloud Config可以完美的支持以上所有的需求。

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例。

二、构建Config Server

创建一个spring-boot项目,取名为config-server,pom.xml中引入依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<!--表示为web工程-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--暴露各种指标 貌似是必须的 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

新建入口类BootApplication:

1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


@SpringBootApplication
@EnableConfigServer
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}

application.properties:

1
2
3
4
5
6
7
8
9
10
11
12
13
server.port=7010
spring.cloud.config.server.default-application-name=config-server

# 配置git仓库地址
spring.cloud.config.server.git.uri=https://github.com/s***w*/myspringcloudconfig
# 配置仓库路径
spring.cloud.config.server.git.search-paths=myconfigpath
# 配置仓库的分支
spring.cloud.config.label=master
# 访问git仓库的用户名
spring.cloud.config.server.git.username=xxxxoooo
# 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
spring.cloud.config.server.git.password=xxxxoooo

远程仓库https://github.com/shaweiwei/myspringcloudconfig/ 中有个文件config-client-dev.properties文件中有一个属性:

1
myww=myww version 2

启动程序:访问http://localhost:7010/myww/dev

证明配置服务中心可以从远程程序获取配置信息。

http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

三、构建一个config client

重新创建一个springboot项目,取名为config-client,其pom文件引入依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

其配置文件bootstrap.properties:

1
2
3
4
5
6
7
8
9
10
# 和git里的文件名对应
spring.application.name=config-client
# 远程仓库的分支
spring.cloud.config.label=master
# dev 开发环境配置文件 | test 测试环境 | pro 正式环境
# 和git里的文件名对应
spring.cloud.config.profile=dev
# 指明配置服务中心的网址
spring.cloud.config.uri= http://localhost:7010/
server.port=7020

程序的入口类,写一个API接口“/hi”,返回从配置中心读取的foo变量的值,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}

@Value("${myww}") // git配置文件里的key
String myww;

@RequestMapping(value = "/hi")
public String hi(){
return myww;
}

}

打开网址访问:http://localhost:7020/hi,网页显示:

myww version 2

这就说明,config-client从config-server获取了foo的属性,而config-server是从git仓库读取的,如图:

四、扩展

我们在进行一些小实验,手动修改service-config-dev.properties中配置信息提交到github,再次请求会看到还是用的旧的配置,这是为什么?因为spirngboot项目只有在启动的时候才会获取配置文件的值,修改github信息后,client端并没有再次去获取,所以导致这个问题。如何去解决这个问题呢?

spring_cloud_例子

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state).

Spring Cloud 为开发者提供了工具
用于快速构建通用模式的分布式系统
例如: 配置管理, 服务发现, 断路器, 智能路由, 微代理, 控制总线, 一次性token, 全局锁, 主节点选择, 分布式session, 集群状态

Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns.

使用样板代码就能快速实现分布式应用

They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.

在各种环境下都能良好运行

1
2
3
4
5
6
7
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

只需通过注解就能启动需要的功能

主要项目

Main Projects

Spring Cloud Config

Centralized external configuration management backed by a git repository. The configuration resources map directly to Spring Environment but could be used by non-Spring applications if desired.

分布式配置中心,

Spring Cloud Netflix

Integration with various Netflix OSS components (Eureka, Hystrix, Zuul, Archaius, etc.).

服务注册和发现, 客户端负载均衡, 断路器, 智能路由

Spring Cloud Bus

An event bus for linking services and service instances together with distributed messaging. Useful for propagating state changes across a cluster (e.g. config change events).

消息总线,

Spring Cloud Cloudfoundry

Integrates your application with Pivotal Cloud Foundry. Provides a service discovery implementation and also makes it easy to implement SSO and OAuth2 protected resources.

集成 Pivotal Cloud Foundry 云服务

Spring Cloud Open Service Broker

Provides a starting point for building a service broker that implements the Open Service Broker API.

用于构建实现Open Service Broker API的Spring Boot应用程序的框架

Spring Cloud Cluster

Leadership election and common stateful patterns with an abstraction and implementation for Zookeeper, Redis, Hazelcast, Consul.

集群功能, 抽象了 Zookeeper , redis, hazelcast, consul

Spring Cloud Consul

Service discovery and configuration management with Hashicorp Consul.

服务治理

Spring Cloud Security

Provides support for load-balanced OAuth2 rest client and authentication header relays in a Zuul proxy.

负载均衡, 服务授权

Spring Cloud Sleuth

Distributed tracing for Spring Cloud applications, compatible with Zipkin, HTrace and log-based (e.g. ELK) tracing.

服务跟踪

Spring Cloud Data Flow

A cloud-native orchestration service for composable microservice applications on modern runtimes. Easy-to-use DSL, drag-and-drop GUI, and REST-APIs together simplifies the overall orchestration of microservice based data pipelines.

大数据处理

Spring Cloud Stream

A lightweight event-driven microservices framework to quickly build applications that can connect to external systems. Simple declarative model to send and receive messages using Apache Kafka or RabbitMQ between Spring Boot apps.

消息驱动

Spring Cloud Stream App Starters

Spring Cloud Stream App Starters are Spring Boot based Spring Integration applications that provide integration with external systems.

Spring Cloud Task

A short-lived microservices framework to quickly build applications that perform finite amounts of data processing. Simple declarative for adding both functional and non-functional features to Spring Boot apps.

定时任务

Spring Cloud Task App Starters

Spring Cloud Task App Starters are Spring Boot applications that may be any process including Spring Batch jobs that do not run forever, and they end/stop after a finite period of data processing.

Spring Cloud Zookeeper

Service discovery and configuration management with Apache Zookeeper.

自动配置 zookeeper

Spring Cloud AWS

Easy integration with hosted Amazon Web Services. It offers a convenient way to interact with AWS provided services using well-known Spring idioms and APIs, such as the messaging or caching API. Developers can build their application around the hosted services without having to care about infrastructure or maintenance.

自动配置 aws 服务

Spring Cloud Connectors

Makes it easy for PaaS applications in a variety of platforms to connect to backend services like databases and message brokers (the project formerly known as “Spring Cloud”).

云服务连接自动配置

Spring Cloud Starters

Spring Boot-style starter projects to ease dependency management for consumers of Spring Cloud. (Discontinued as a project and merged with the other projects after Angel.SR2.)

Spring Cloud CLI

Spring Boot CLI plugin for creating Spring Cloud component applications quickly in Groovy

方便创建应用

Spring Cloud Contract

Spring Cloud Contract is an umbrella project holding solutions that help users in successfully implementing the Consumer Driven Contracts approach.

消费者驱动契约

Spring Cloud Gateway

Spring Cloud Gateway is an intelligent and programmable router based on Project Reactor.

网关, 替代 zuul

Spring Cloud OpenFeign

Spring Cloud OpenFeign provides integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.

OpenFeign 自动配置

Spring Cloud Pipelines

Spring Cloud Pipelines provides an opinionated deployment pipeline with steps to ensure that your application can be deployed in zero downtime fashion and easilly rolled back of something goes wrong.

Spring Cloud Function

Spring Cloud Function promotes the implementation of business logic via functions. It supports a uniform programming model across serverless providers, as well as the ability to run standalone (locally or in a PaaS).

activiti 对 mybatis 的使用解析

要点

  • 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的
  • activit 的配置核心是 ProcessEngineConfigurationImpl

创建 mybatis SqlSessionFactory

org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl#initSqlSessionFactory

读取 xml 配置文件

1
2
3
inputStream = getMyBatisXmlConfigurationSteam();
Configuration configuration = initMybatisConfiguration(environment, reader, properties);
sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

用于应用查询的 mapping 文件

文件路径: Activiti\activiti-engine\src\main\resources\org\activiti\db\mapping

activiti 的 createxxxQuery

比如 runtimeService 的 createExecutionQuery,
返回 ExecutionQuery 的 派生类对象 ExecutionQueryImpl 对象

query 实现了 execute 接口,

executeList 方法

List<?> executions = commandContext.getExecutionEntityManager().findExecutionsByQueryCriteria(this, page);

ExecutionEntityManager 负责调用mybatis 的接口

getDbSqlSession().selectList(“selectExecutionsByQueryCriteria”, executionQuery, page);

DbSqlSession 包装了对 mybatis sqlSession 的调用

List loadedObjects = sqlSession.selectList(statement, parameter);

getxxxEntityManager 是 session的子类

xxxEntityManager 将请求转到session, activiti 默认创建了需要的 xxxEntityManager

commandContext.getExecutionEntityManager()

historicTaskInstanceQueryImpl

查询列表,
org.activiti.engine.impl.HistoricTaskInstanceQueryImpl#executeList

if (includeTaskLocalVariables || includeProcessVariables) {
  tasks = commandContext
      .getHistoricTaskInstanceEntityManager()
      .findHistoricTaskInstancesAndVariablesByQueryCriteria(this);
} else {
  tasks = commandContext
      .getHistoricTaskInstanceEntityManager()
      .findHistoricTaskInstancesByQueryCriteria(this);
}

org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntityManager#findHistoricTaskInstancesByQueryCriteria

getDbSqlSession().selectList("selectHistoricTaskInstancesByQueryCriteria", historicTaskInstanceQuery);

org.activiti.engine.impl.db.DbSqlSession#selectList(java.lang.String, org.activiti.engine.impl.db.ListQueryParameterObject)

selectListWithRawParameter(statement, parameter, parameter.getFirstResult(), parameter.getMaxResults());
statement = dbSqlSessionFactory.mapStatement(statement);    
List loadedObjects = sqlSession.selectList(statement, parameter);

org.activiti.engine.impl.db.DbSqlSessionFactory#mapStatement

org.apache.ibatis.session.SqlSession#selectList(java.lang.String, java.lang.Object)

activiti 是如何初始化 mybatis 的

观察 :
DbSqlSession,
DbSqlSessionFactory,

自动配置

// 可以配置的 activiti 参数
org.activiti.spring.boot.ActivitiProperties

private List<String> customMybatisMappers;  // 用户自己的 mybatis mapper
private List<String> customMybatisXMLMappers;  // 用户自己的 mybatis xml mapper 

org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration#processEngine

org.activiti.spring.boot.AbstractProcessEngineConfiguration#springProcessEngineBean
ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
processEngineFactoryBean.setProcessEngineConfiguration(configuration);

org.activiti.spring.ProcessEngineFactoryBean#getObject
this.processEngine = processEngineConfiguration.buildProcessEngine();

org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl#init

org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl#initDataSource

protected void init() {
    initConfigurators();
    configuratorsBeforeInit();
    initProcessDiagramGenerator();
    initHistoryLevel();
    initExpressionManager();
    initDataSource();
    initVariableTypes();
    initBeans();
    initFormEngines();
    initFormTypes();
    initScriptingEngines();
    initClock();
    initBusinessCalendarManager();
    initCommandContextFactory();
    initTransactionContextFactory();
    initCommandExecutors();
    initServices();
    initIdGenerator();
    initDeployers();
    initJobHandlers();
    initJobExecutor();
    initAsyncExecutor();
    initTransactionFactory();
    initSqlSessionFactory();
    initSessionFactories();
    initJpa();
    initDelegateInterceptor();
    initEventHandlers();
    initFailedJobCommandFactory();
    initEventDispatcher();
    initProcessValidator();
    initDatabaseEventLogging();
    configuratorsAfterInit();
  }

org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl#initSqlSessionFactory

inputStream = getMyBatisXmlConfigurationSteam();
Environment environment = new Environment("default", transactionFactory, dataSource);
Reader reader = new InputStreamReader(inputStream);

Configuration configuration = initMybatisConfiguration(environment, reader, properties);
sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl#initSessionFactories

org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl#initCustomMybatisMappers

org.activiti.engine.impl.cmd.ExecuteCustomSqlCmd#execute
Mapper mapper = commandContext.getDbSqlSession().getSqlSession().getMapper(mapperClass);
return customSqlExecution.execute(mapper);

activiti-springboot使用自定义mybatis-mapper

在application.properties新增自定义配置

spring.activiti.customMybatisXMLMappers=com/codecraft/dao/ProcInstMapper.xml

spring.activiti.customMybatisMappers=com.codecraft.dao.ProcInstMapper

使用方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public List<ProcInstVo> getAllProcInsts(){

return managementService.executeCustomSql(new AbstractCustomSqlExecution<ProcInstMapper,List<ProcInstVo>>(ProcInstMapper.class) {

@Override

public List<ProcInstVo> execute(ProcInstMapper procInstMapper) {

return procInstMapper.selectAllProcInsts();

}

});

}
```


```java
ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = cfg.buildProcessEngine();
ManagementService managementService = processEngine.getManagementService();

// 获取指定表的数据
TablePage tablePage = managementService.createTablePageQuery()
.tableName(managementService.getTableName(ProcessDefinitionEntity.class))
.listPage(0,100);
List<Map<String,Object>> rowsList = tablePage.getRows();
for (Map<String,Object> row : rowsList){
logger.info("row={}",row);
}