Configure the Camunda workflow engine cluster

To apply Camunda workflow in a production environment, it is usually necessary to configure multiple workflow engine instances to meet load sharing, disaster recovery and backup requirements. Here I will use nacos+nginx to realize service registration and load sharing of multiple workflow engines.

I use spring boot integration to start the Camunda workflow. For specific methods, please refer to my previous article, Springboot integrates Camunda process engine_gzroy's blog-CSDN blog_camunda's process engine operation mode , here I have made a little improvement, that is Add an API to the springboot project, which can read the port of the currently running workflow engine, so that after we set up load sharing, it can help us determine whether the traffic goes to a different engine.

According to the official website, camunda uses Jersey to implement the Rest API, so we also need to use this method to add new APIs.

Create a new config folder in the springboot project, and add a JerseyConfig.java file in it, the content is as follows:

package com.roy.camunda.config;

import javax.ws.rs.ApplicationPath;

import org.camunda.bpm.spring.boot.starter.rest.CamundaJerseyResourceConfig;
import org.springframework.stereotype.Component;

import com.roy.camunda.service.TestService;

@Component
@ApplicationPath("/engine-rest")
public class JerseyConfig extends CamundaJerseyResourceConfig {
  @Override
  protected void registerAdditionalResources() {
    register(TestService.class);
  }
}

This class extends CamundaJerseyRerouceConfig and registers a new API resource named TestService.

Create a new service folder, and create a new TestService.java in it, the content is as follows:

package com.roy.camunda.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
@Path("/test")
public class TestService {
    @Autowired
    Environment environment;

    @GET
    @Produces("text/plain")
    public String test() {
        return "The running engine port is:" + environment.getProperty("local.server.port");
    }
}

In the next step, we need to automatically register the service instance information of this workflow engine with nacos. Nacos is an open source software launched by Ali that implements the spring cloud microservice registration center and configuration center.

Modify our springboot project just now, and add the following configuration to the dependancyManagement in pom.xml:

<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-alibaba-dependencies</artifactId>
	<version>2.2.9.RELEASE</version>
	<type>pom</type>
	<scope>import</scope>
</dependency>

Add the following configuration in dependencies:

<dependency>
    <groupId>com.alibaba.boot</groupId>
	<artifactId>nacos-discovery-spring-boot-starter</artifactId>
	<version>0.2.12</version>
</dependency>

Add the following configuration to the application.yml file:

spring:
  application:
    name: camunda-service

nacos:
  discovery:
    auto-register: true
    register:
      ip: 127.0.0.1
      port: 8081
      enabled: true
      groupName: CAMUNDA
    server-addr: 127.0.0.1:8848

The configuration here corresponds to the configuration of the actual nacos server.

After completion, we run in two terminals separately

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8080 and mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081, start two camunda Engine instances, these two instances are running on local ports 8080 and 8081 respectively.

Afterwards, in the service list of the nacos console, we can see a service named camunda-service. There are two instances of the service, and the corresponding server IP and port can be seen in the service details.

Finally, it is set in Nginx. In the default file of sites-available, add a configuration:

upstream camunda {
    server 127.0.0.1:8081;
    server 127.0.0.1:8080;
}

Then add it to the server configuration

location /camunda/engine-rest {
    proxy_pass http://camunda/camunda/engine-rest;
}

Now, when we visit http://localhost/camunda/engine-rest/test, we can see that the port number that is currently running the camunda engine is returned. Repeatedly calling it many times, we can see that the port number will return 8080 or 8081. Therefore, it can be proved that the load sharing of the two engines has now been realized.

There is another way to verify load sharing, which is to add the following configuration to the application.yml of the Spring boot project:

logging.level.org.camunda.bpm:
  application: DEBUG
  engine.context: DEBUG

Then we start two workflow instances, and we can see that they will run separately in the two engines.

We can further improve it by using the Nginx plug-in to automatically call the nacos API, regularly query the IP and port number of the currently registered service, and automatically update the Nginx configuration to realize automatic service discovery and update.

Guess you like

Origin blog.csdn.net/gzroy/article/details/128243151