springcloud集成camunda

750 阅读2分钟

一、什么是camunda

Camunda是一个流程引擎,用于管理业务流程和工作流。它可以帮助企业将业务流程转变为自动化工作流,使其更加高效、可靠和可控。Camunda是一个开源的流程引擎,采用Java语言编写,可以轻松地与其他系统进行集成和部署。它具有可扩展性、灵活性和可定制性,以满足各种不同的业务需求。Camunda不仅提供了API和工具来管理流程,还提供了一系列Web界面,使用户可以通过图形化界面观察和管理流程。Camunda还提供了详细的文档和社区支持,使开发人员更容易地使用它来构建流程应用程序。

二、开整

先贴上官方文档地址: camunda-cn.shaochenfeng.com/introductio…

1、我这边是基于gradle来构建项目的,首先创建一个gradle项目然后引入依赖

dependencies {  
compile 'org.camunda.bpm.springboot:camunda-bpm-spring-boot-starter-rest:7.16.0'  

compile 'mysql:mysql-connector-java'  
compile 'org.springframework.boot:spring-boot-starter-jdbc'  
compile 'com.alibaba:druid-spring-boot-starter:1.2.16'  
compile 'org.springframework.boot:spring-boot-starter-actuator'  
compile 'org.springframework.boot:spring-boot-starter-web'  
compile 'org.springframework.cloud:spring-cloud-starter-config'  
compile ('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') {  
exclude group: 'javax.ws.rs' , module: 'jsr311-api'  
}  
compile 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'  
compile 'org.springframework.cloud:spring-cloud-starter-netflix-ribbon'  
  
}

这里引入了 spring cloud的一些相关组件,eurka,config,hystrix,ribbon等,根据实际情况酌情引入。

2、配置

spring:  
    application:  
        name: camunda-api
    profiles:  
        active: dev
    main:  
        allow-bean-definition-overriding: true
    datasource:  
        url: jdbc:mysql://ip:port/camunda_api?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&allowMultiQueries=true  
        username: root 
        password: root  
        hikari:  
            idle-timeout: 30000  
            maximum-pool-size: 8
eureka:  
    client:  
        service-url:  
            defaultZone: http://localhost:8810/eureka 
        fetch-registry: true  
        register-with-eureka: true  
        registryFetchIntervalSeconds: 5  
        healthcheck:  
        enabled: true  
    instance:  
        prefer-ip-address: true

3、camunda默认的id生成规则为uuid ,如果要替换则需要自定义一个id生成器。这里我们使用雪花id生成器

public class SnowflakeIdGenerator implements IdGenerator {

private static final long WORKER_ID_BITS = 10L;  
private static final long SEQUENCE_BITS = 12L;  
private static final long MAX_WORKER_ID = (1L << WORKER_ID_BITS) - 1L;  
private static final long MAX_SEQUENCE = (1L << SEQUENCE_BITS) - 1L;  
private static final long CUSTOM_EPOCH = 1609459200000L; // 2021-01-01 00:00:00  
private final long workerId;  
private long sequence = 0L;  
private long lastTimestamp = -1L;

public SnowflakeIdGenerator() {  
    this.workerId = createWorkerId();  
}  

private long createWorkerId() {  
    long workerId;  
    try {  
    StringBuilder sb = new StringBuilder();  
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();  
    while (networkInterfaces.hasMoreElements()) {  
    NetworkInterface networkInterface = networkInterfaces.nextElement();  
    byte[] macAddress = networkInterface.getHardwareAddress();  
    if (macAddress != null) {  
    for (byte b : macAddress) {  
    sb.append(String.format("%02X", b));  
    }  
    }  
    }  
    workerId = sb.toString().hashCode() & MAX_WORKER_ID;  
    } catch (Exception ex) {  
    workerId = (new SecureRandom().nextInt((int) MAX_WORKER_ID));  
    }  
    return workerId;  
}



@Override  
public synchronized String getNextId() {  
long currentTimestamp = getTimestamp();  
if (currentTimestamp < lastTimestamp) {  
throw new IllegalStateException("Clock moved backwards. Refusing to generate id");  
}  
if (currentTimestamp == lastTimestamp) {  
sequence = (sequence + 1) & MAX_SEQUENCE;  
if (sequence == 0L) {  
currentTimestamp = getNextTimestamp(currentTimestamp);  
}  
} else {  
sequence = 0L;  
}  
lastTimestamp = currentTimestamp;  
long timestamp = (currentTimestamp - CUSTOM_EPOCH) << (WORKER_ID_BITS + SEQUENCE_BITS);  
long workerId = this.workerId << SEQUENCE_BITS;  
long sequence = this.sequence;  
return String.valueOf(timestamp | workerId | sequence);  
}




private long getNextTimestamp(long currentTimestamp) {  
long timestamp = getTimestamp();  
while (timestamp <= currentTimestamp) {  
timestamp = getTimestamp();  
}  
return timestamp;  
}  
private long getTimestamp() {  
return Instant.now().toEpochMilli();  
}

至此可以直接启动该项目,一个camunda的应用就搭起来了。如果是微服务则可以使用feign 来方便服务与服务之间的调用

image.png